Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
stringApi.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/functional>
9#include <ext/memory>
10#include <ext/algorithm>
11#include <ext/istream>
12#include <ext/typeinfo>
13
14#include <alib/list>
15#include <alib/map>
16#include <alib/string>
17
18#include <object/Object.h>
19#include <object/AnyObject.h>
20
22
23namespace core {
24
25template < typename T >
26struct stringApi;
27
28template < >
29struct stringApi < object::Object > {
30public:
31 class GroupReader {
32 public:
33 virtual object::Object parse ( ext::istream & input ) = 0;
34
35 virtual ~GroupReader ( ) = default;
36 };
37
38private:
39 static ext::list < std::pair < std::function < bool ( ext::istream & ) >, std::unique_ptr < GroupReader > > > & parseFunctions ( );
40
41 template < class Type >
42 class ReaderRegister : public GroupReader {
43 public:
44 ~ReaderRegister ( ) override = default;
45
46 object::Object parse ( ext::istream & input ) override {
47 return object::Object ( stringApi < Type >::parse ( input ) );
48 }
49 };
50
51public:
52 class GroupWriter {
53 public:
54 virtual void compose ( ext::ostream & output, const object::Object & group ) = 0;
55
56 virtual ~GroupWriter ( ) = default;
57 };
58
59private:
60 static ext::map < std::string, std::unique_ptr < GroupWriter > > & composeFunctions ( );
61
62 template < class Type >
63 class WriterRegister : public GroupWriter {
64 public:
65 ~WriterRegister ( ) override = default;
66
67 void compose ( ext::ostream & output, const object::Object & group ) override {
68 stringApi < Type >::compose ( output, static_cast < const object::AnyObject < Type > & > ( group.getData ( ) ).getData ( ) );
69 }
70 };
71
72public:
73 static void unregisterStringReader ( ext::list < std::pair < std::function < bool ( ext::istream & ) >, std::unique_ptr < GroupReader > > >::const_iterator iter );
74
75 static ext::list < std::pair < std::function < bool ( ext::istream & ) >, std::unique_ptr < GroupReader > > >::const_iterator registerStringReader ( std::function < bool ( ext::istream & ) > first, std::unique_ptr < GroupReader > entry );
76
77 template < class Type >
78 static ext::list < std::pair < std::function < bool ( ext::istream & ) >, std::unique_ptr < GroupReader > > >::const_iterator registerStringReader ( ) {
79 return registerStringReader ( stringApi < Type >::first, std::unique_ptr < GroupReader > ( new ReaderRegister < Type > ( ) ) );
80 }
81
82 static void unregisterStringWriter ( const std::string & type, const std::string & typeName );
83
84 template < class Type >
85 static void unregisterStringWriter ( ) {
86 unregisterStringWriter ( ext::to_string < object::AnyObject < Type > > ( ), ext::to_string < Type > ( ) );
87 }
88
89 static void registerStringWriter ( std::string type, const std::string & typeName, std::unique_ptr < GroupWriter > entry );
90
91 template < class Type >
92 static void registerStringWriter ( ) {
93 registerStringWriter ( ext::to_string < object::AnyObject < Type > > ( ), ext::to_string < Type > ( ), std::unique_ptr < GroupWriter > ( new WriterRegister < Type > ( ) ) );
94 }
95
96 static object::Object parse ( ext::istream & input );
97
98 static bool first ( ext::istream & input );
99
100 static void compose ( ext::ostream & output, const object::Object & data );
101};
102
103} /* namespace core */
104
virtual object::Object parse(ext::istream &input)=0
virtual void compose(ext::ostream &output, const object::Object &group)=0
Definition: istream.h:32
Class extending the list class from the standard library. Original reason is to allow printing of the...
Definition: list.hpp:44
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: ostream.h:14
Represents an adaptor of any type to a class in type hierarchy of objects in the algorithms library.
Definition: AnyObject.h:37
Definition: Object.h:16
const AnyObjectBase & getData() const
Definition: Object.h:142
Definition: normalize.hpp:10
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
Definition: AnyObject.h:28
static ext::list< std::pair< std::function< bool(ext::istream &)>, std::unique_ptr< GroupReader > > >::const_iterator registerStringReader()
Definition: stringApi.hpp:78
static void unregisterStringWriter()
Definition: stringApi.hpp:85
static void registerStringWriter()
Definition: stringApi.hpp:92
Definition: stringApi.hpp:26