Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
XmlDataFactory.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/string>
9#include <alib/deque>
10#include <alib/measure>
11
12#include <sax/Token.h>
15#include <core/xmlApi.hpp>
17
18namespace factory {
19
24public:
25
26 class fromFile {
27 const std::string & filename;
28
29 public:
30 fromFile ( const std::string & file ) : filename ( file ) {
31 }
32
38 template < class T >
39 operator T ( ) {
40 return fromTokens ( sax::SaxParseInterface::parseFile ( filename ) );
41 }
42 };
43
44 class fromString {
45 const std::string & string;
46
47 public:
48 fromString ( const std::string & str ) : string ( str ) {
49 }
50
56 template < class T >
57 operator T ( ) {
59 }
60 };
61
62 class fromStdin {
63 public:
68 template < class T >
69 operator T ( ) {
71 }
72 };
73
74 class fromStream {
75 ext::istream & in;
76
77 public:
78 fromStream ( ext::istream & i ) : in ( i ) {
79 }
80
85 template < class T >
86 operator T ( ) {
88
89 }
90 };
91
92 class fromTokens {
94
95 public:
96 fromTokens ( ext::deque < sax::Token > && toks ) : tokens ( std::move ( toks ) ) {
97 }
98
104 template < class T >
105 operator T ( ) {
106 core::xmlApiInputContext context ( tokens.begin ( ) );
107
108 if ( context == tokens.end ( ) ) throw exception::CommonException ( "Empty tokens list" );
109
111 T res = core::xmlApi < T >::parse ( context );
113
114 if ( context != tokens.end ( ) ) throw exception::CommonException ( "Unexpeted tokens at the end of the xml" );
115
116 return res;
117 }
118 };
119
125 template < class T >
126 static bool first ( const ext::deque < sax::Token > & tokens ) {
127 if( tokens.empty ( ) ) throw exception::CommonException ( "Empty tokens list" );
128
129 return core::xmlApi < T >::first ( tokens.begin ( ) );
130 }
131
137 template < class T >
138 static void toFile ( const T & data, const std::string & filename ) {
139 ext::deque < sax::Token > tokens = toTokens < T > ( data );
140 sax::SaxComposeInterface::composeFile ( filename, tokens );
141 }
142
148 template < class T >
149 static std::string toString ( const T & data ) {
150 ext::deque < sax::Token > tokens = toTokens < T > ( data );
152 }
153
158 template < class T >
159 static void toStdout ( const T & data ) {
160 ext::deque < sax::Token > tokens = toTokens < T > ( data );
162 }
163
168 template < class T >
169 static void toStream ( const T & data, ext::ostream & out ) {
170 ext::deque < sax::Token > tokens = toTokens < T > ( data );
172 }
173
179 template < class T >
180 static ext::deque < sax::Token > toTokens ( const T & data ) {
182
184 core::xmlApi < T >::compose ( context, data );
186
187 ext::deque < sax::Token > res = std::move ( context ); // FIXME present only not to get conflicting varnings from g++ and clang++ if "return context;" is used
188 return res;
189 }
190
191};
192
193template < >
194inline XmlDataFactory::fromTokens::operator measurements::MeasurementResults ( ) {
195 core::xmlApiInputContext context ( tokens.begin ( ) );
196
197 if ( context == tokens.end ( ) ) throw exception::CommonException ( "Empty tokens list" );
198
200
201 if ( context != tokens.end ( ) ) throw exception::CommonException ( "Unexpeted tokens at the end of the xml" );
202
203 return res;
204}
205
206template < >
209
211
212 ext::deque < sax::Token > res = std::move ( context ); // FIXME present only not to get conflicting varnings from g++ and clang++ if "return context;" is used
213 return res;
214}
215
216} /* namespace factory */
217
Definition: xmlApi.hpp:29
Definition: xmlApi.hpp:42
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: deque.hpp:100
auto end() &
Inherited behavior of end for non-const instance.
Definition: deque.hpp:130
Definition: istream.h:32
Definition: ostream.h:14
Definition: XmlDataFactory.hpp:26
fromFile(const std::string &file)
Definition: XmlDataFactory.hpp:30
Definition: XmlDataFactory.hpp:62
Definition: XmlDataFactory.hpp:74
fromStream(ext::istream &i)
Definition: XmlDataFactory.hpp:78
Definition: XmlDataFactory.hpp:44
fromString(const std::string &str)
Definition: XmlDataFactory.hpp:48
Definition: XmlDataFactory.hpp:92
fromTokens(ext::deque< sax::Token > &&toks)
Definition: XmlDataFactory.hpp:96
Definition: XmlDataFactory.hpp:23
static void toFile(const T &data, const std::string &filename)
Definition: XmlDataFactory.hpp:138
static bool first(const ext::deque< sax::Token > &tokens)
Definition: XmlDataFactory.hpp:126
static ext::deque< sax::Token > toTokens(const T &data)
Definition: XmlDataFactory.hpp:180
static std::string toString(const T &data)
Definition: XmlDataFactory.hpp:149
static void toStream(const T &data, ext::ostream &out)
Definition: XmlDataFactory.hpp:169
static void toStdout(const T &data)
Definition: XmlDataFactory.hpp:159
static void composeFile(const std::string &filename, const ext::deque< Token > &in)
Definition: SaxComposeInterface.cpp:37
static void composeStream(ext::ostream &out, const ext::deque< Token > &in)
Definition: SaxComposeInterface.cpp:49
static void composeMemory(std::string &xmlOut, const ext::deque< Token > &in)
Definition: SaxComposeInterface.cpp:20
static void composeStdout(const ext::deque< Token > &in)
Definition: SaxComposeInterface.cpp:45
static void parseFile(const std::string &filename, ext::deque< Token > &out)
Definition: SaxParseInterface.cpp:45
static void parseMemory(const std::string &xmlIn, ext::deque< Token > &out)
Definition: SaxParseInterface.cpp:23
static void parseStream(ext::istream &in, ext::deque< Token > &out)
Definition: SaxParseInterface.cpp:73
static ext::deque< Token > parseStdin()
Definition: SaxParseInterface.cpp:69
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
Definition: NormalizeFactory.hpp:13
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14
void end()
Definition: measurements.cpp:19
Definition: FordFulkerson.hpp:16
Definition: RandomStringFactory.cpp:12
Definition: xmlApi.hpp:27
Definition: MeasurementResults.hpp:26