1 | #include "libconfig/libconfig.h++" |
---|
2 | #include <itpp/itbase.h> |
---|
3 | |
---|
4 | #ifndef UIBUILD |
---|
5 | #define UIBUILD |
---|
6 | |
---|
7 | using namespace libconfig; |
---|
8 | using namespace itpp; |
---|
9 | using namespace std; |
---|
10 | |
---|
11 | #define CHECK_UITYPE(S,Type) it_assert_debug(S.getType()==Setting::Type, string("Wrong input path \"")+string(S.getPath())+string("\"")); |
---|
12 | #define UIREGISTER(UI) UI* UI##_global_instance = new UI(); |
---|
13 | |
---|
14 | ////////// GLOBAL VAriables |
---|
15 | |
---|
16 | class UIbuilder; |
---|
17 | //! Internal structure mapping strings to UIBuilder objects |
---|
18 | typedef map<const string, const UIbuilder*> UImap; |
---|
19 | extern UImap __uimap__; |
---|
20 | |
---|
21 | class UIFile : public Config{ |
---|
22 | public: |
---|
23 | UIFile(const char * fname):Config(){ |
---|
24 | try{Config::readFile(fname);} |
---|
25 | catch (ParseException& P) { |
---|
26 | char msg[200]; |
---|
27 | sprintf(msg,"Error in file %s on line %d.", fname, P.getLine()); |
---|
28 | it_error(msg); |
---|
29 | } |
---|
30 | catch (FileIOException f) {it_error("File " + string(fname) + " not found");} |
---|
31 | } |
---|
32 | }; |
---|
33 | |
---|
34 | /*!\brief Builds computational object from a UserInfo structure |
---|
35 | |
---|
36 | Return value is a pointer to the created object (memory management issue?) |
---|
37 | */ |
---|
38 | class UIbuilder { |
---|
39 | protected: |
---|
40 | static const UIbuilder* theinstance; |
---|
41 | vec getvec ( Setting& S ) { |
---|
42 | CHECK_UITYPE(S,TypeArray); |
---|
43 | vec tmp; |
---|
44 | tmp.set_size ( S.getLength() ); |
---|
45 | for ( int i=0;i<S.getLength();i++ ) { |
---|
46 | switch (S[i].getType()) { |
---|
47 | case Setting::TypeFloat : |
---|
48 | tmp[i]=double(S[i]);break; |
---|
49 | case Setting::TypeInt : |
---|
50 | tmp[i]=int(S[i]);break; |
---|
51 | case Setting::TypeBoolean : |
---|
52 | tmp[i]=bool(S[i]);break; |
---|
53 | default: it_error("libconfig error?"); |
---|
54 | } |
---|
55 | } |
---|
56 | return tmp; |
---|
57 | }; |
---|
58 | vec getivec ( Setting& S ) { |
---|
59 | CHECK_UITYPE(S,TypeArray); |
---|
60 | vec tmp; |
---|
61 | tmp.set_size ( S.getLength() ); |
---|
62 | for ( int i=0;i<S.getLength();i++ ) { |
---|
63 | switch (S[i].getType()) { |
---|
64 | case Setting::TypeFloat : |
---|
65 | tmp[i]=double(S[i]);break; |
---|
66 | case Setting::TypeInt : |
---|
67 | tmp[i]=int(S[i]);break; |
---|
68 | case Setting::TypeBoolean : |
---|
69 | tmp[i]=bool(S[i]);break; |
---|
70 | default: it_error("libconfig error?"); |
---|
71 | } |
---|
72 | } |
---|
73 | return tmp; |
---|
74 | }; |
---|
75 | public: |
---|
76 | UIbuilder(const string &typ){__uimap__.insert(make_pair(typ,this));} |
---|
77 | virtual void build(Setting &S, void** result) const =0; |
---|
78 | }; |
---|
79 | |
---|
80 | class UIexternal:public UIbuilder{ |
---|
81 | public: |
---|
82 | UIexternal():UIbuilder("external"){} |
---|
83 | void build(Setting &S, void** result) const; |
---|
84 | }; |
---|
85 | |
---|
86 | class UIinternal:public UIbuilder{ |
---|
87 | public: |
---|
88 | UIinternal():UIbuilder("internal"){} |
---|
89 | void build(Setting &S, void** result) const; |
---|
90 | }; |
---|
91 | |
---|
92 | //! Prototype of a UI builder. Return value is by the second argument since it allows some type of type checking. |
---|
93 | template<class T> |
---|
94 | void UIbuild(Setting &S, T** ret){ |
---|
95 | CHECK_UITYPE(S,TypeGroup); |
---|
96 | T* tmp; |
---|
97 | // Check if field "type" is present, if not it is not a valid UI |
---|
98 | it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); |
---|
99 | |
---|
100 | const string typ=S["type"]; |
---|
101 | // Find "type" in list of registred UI builders |
---|
102 | UImap::const_iterator iter = __uimap__.find( typ ); |
---|
103 | if( iter == __uimap__.end()) { |
---|
104 | it_error("UI of type \"" + typ + "\" is not registered!"); |
---|
105 | } |
---|
106 | else { |
---|
107 | const UIbuilder* is= iter->second; |
---|
108 | //BUILD the result |
---|
109 | is->build(S,reinterpret_cast<void**>(&tmp)); |
---|
110 | } |
---|
111 | // make assignment |
---|
112 | *ret=tmp; |
---|
113 | }; |
---|
114 | |
---|
115 | |
---|
116 | Setting& UILoad(const char* fname); |
---|
117 | |
---|
118 | #endif UIBUILD |
---|