Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RawDataFactory.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/fstream>
9#include <ext/sstream>
10
11#include <alib/set>
13#include <global/GlobalData.h>
14
15#include <core/rawApi.hpp>
16
17namespace factory {
18
20public:
21 class fromFile {
22 const std::string & filename;
23
24 public:
25 explicit fromFile ( const std::string & file ) : filename ( file ) {
26 }
27
28 template < class T >
29 operator T ( ) {
30 ext::ifstream fileStream ( filename );
31
32 return fromStream ( fileStream );
33 }
34 };
35
36 class fromRaw {
37 const std::string & string;
38
39 public:
40 explicit fromRaw ( const std::string & str ) : string ( str ) {
41 }
42
43 template < class T >
44 operator T ( ) {
45 ext::istringstream stringStream ( string );
46
47 return fromStream ( stringStream );
48 }
49 };
50
51 class fromString {
52 const std::string & string;
53
54 public:
55 explicit fromString ( const std::string & str ) : string ( str ) {
56 }
57
58 template < class T >
59 operator T ( ) {
60 ext::istringstream stringStream ( string );
61
62 return fromStream ( stringStream );
63 }
64 };
65
66 class fromStdin {
67 public:
68 template < class T >
69 operator T ( ) {
71 }
72 };
73
74 class fromStream {
75 ext::istream & in;
76
77 public:
78 explicit fromStream ( ext::istream & i ) : in ( i ) {
79 }
80
81 template < class T >
82 operator T ( ) {
83 if ( in.peek ( ) == EOF )
84 throw exception::CommonException ( "Empty stream" );
85
87
88 while ( isspace ( in.peek ( ) ) )
89 in.get ( );
90
91 if ( in.peek ( ) != EOF )
92 throw exception::CommonException ( "Unexpected characters at the end of the stream" );
93
94 return res;
95 }
96 };
97
98 template < class T >
99 static void toFile ( const T & data, const std::string & filename) {
100 ext::ofstream fileStream ( filename );
101 toStream < T > ( data, fileStream );
102 }
103
104 template < class T >
105 static std::string toString ( const T & data ) {
106 ext::ostringstream stringStream;
107 toStream < T > ( data, stringStream );
108 return stringStream.str ( );
109 }
110
111 template < class T >
112 static void toStdout ( const T & data ) {
113 toStream < T > ( data, common::Streams::out.get ( ) );
114 }
115
116 template < class T >
117 static void toStream ( const T & data, ext::ostream & out ) {
118 core::rawApi < T >::compose ( out, data );
119 }
120};
121
122} /* namespace factory */
123
static ext::reference_wrapper< ext::ostream > out
Standard output stream. Mapped to descriptor 1.
Definition: GlobalData.h:66
static ext::reference_wrapper< ext::istream > in
Standard input stream. Mapped to descriptor 0.
Definition: GlobalData.h:60
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: fstream.h:68
Definition: istream.h:32
int peek()
Definition: istream.cpp:41
int get()
Definition: istream.cpp:45
Definition: sstream.h:36
Definition: fstream.h:15
Definition: ostream.h:14
Definition: sstream.h:15
std::string str() const &
Definition: sstream.cpp:29
Definition: RawDataFactory.hpp:21
fromFile(const std::string &file)
Definition: RawDataFactory.hpp:25
Definition: RawDataFactory.hpp:36
fromRaw(const std::string &str)
Definition: RawDataFactory.hpp:40
Definition: RawDataFactory.hpp:66
Definition: RawDataFactory.hpp:74
fromStream(ext::istream &i)
Definition: RawDataFactory.hpp:78
Definition: RawDataFactory.hpp:51
fromString(const std::string &str)
Definition: RawDataFactory.hpp:55
Definition: RawDataFactory.hpp:19
static void toStdout(const T &data)
Definition: RawDataFactory.hpp:112
static std::string toString(const T &data)
Definition: RawDataFactory.hpp:105
static void toStream(const T &data, ext::ostream &out)
Definition: RawDataFactory.hpp:117
static void toFile(const T &data, const std::string &filename)
Definition: RawDataFactory.hpp:99
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
bool isspace(int ch)
isspace method.
Definition: string.hpp:279
Definition: NormalizeFactory.hpp:13
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
Definition: RandomStringFactory.cpp:12
Definition: rawApi.hpp:11