root/library/bdm/base/user_info.cpp @ 493

Revision 493, 10.6 kB (checked in by vbarta, 15 years ago)

fixed UIREGISTER

  • Property svn:eol-style set to native
Line 
1//
2// C++ Implementation: user_info.cpp
3//
4// Description: UI (user info) class for loading/saving objects from/to configuration files.
5//
6//
7// Author: smidl <smidl@utia.cas.cz>, (C) 2009
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13#include "user_info.h"
14
15namespace bdm {
16///////////////////////////// Class UIFile /////////////////////////////////////////////
17
18UIFile::UIFile() {
19}
20
21UIFile::UIFile ( const string &file_name ) {
22        try {
23                readFile ( file_name.c_str() );
24                // this flag has to be set AFTER each file load, that is why it is right here
25                setAutoConvert ( true );
26        } catch ( FileIOException f ) {
27                it_error ( "UI error: file " + file_name + " not found." );
28        } catch ( ParseException& P ) {
29                stringstream msg;
30                msg << "UI error: parsing error """ << P.getError() << """ in file " << file_name << " on line " <<  P.getLine() << ".";
31                it_error ( msg.str() );
32        }
33}
34
35
36void UIFile::save ( const string &file_name ) {
37        try {
38                writeFile ( file_name.c_str() );
39        } catch ( FileIOException f ) {
40                it_error ( "UI error: file " + file_name + " is inacessible." );
41        }
42}
43
44UIFile::operator Setting&() {
45        return getRoot();
46}
47
48///////////////////////////// Class UI::MappedUI /////////////////////////////////////////////
49
50UI::MappedUI::StringToUIMap& UI::MappedUI::mapped_strings() {
51        // this way it is ensured that there is only one instance of StringTpUIMap, and
52        // what is more, this declaration leaves its allocation/deallocation on the compiler
53        static StringToUIMap var;
54        return var;
55}
56
57UI::MappedUI::TypeInfoToStringMap& UI::MappedUI::mapped_type_infos() {
58        // this way it is ensured that there is only one instance of TypeInfoToStringMap, and
59        // what is more, this declaration leaves its allocation/deallocation on the compiler
60        static TypeInfoToStringMap var;
61        return var;
62}
63
64void UI::MappedUI::add_class ( const string &class_name, const type_info * const class_type_info, const UI* const ui ) {
65        pair< StringToUIMap::iterator, bool> inres =
66                mapped_strings().insert (
67                        StringToUIMap::value_type( class_name, ui ) );
68        if ( inres.second ) {
69                mapped_type_infos().insert (
70                        TypeInfoToStringMap::value_type (
71                                class_type_info, class_name ) );
72        }
73}
74
75void UI::MappedUI::unregistered_class_error ( const string &unregistered_class_name ) {
76        stringstream msg;
77        msg << "UI error: class " + unregistered_class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." << endl;
78
79        if ( mapped_strings().size() ) {
80                StringToUIMap::const_iterator iter = mapped_strings().begin();
81                msg << "These classes are already registered: " << iter->first;
82                for ( iter++; iter != mapped_strings().end(); iter++ )
83                        msg << ", " << iter->first;
84                msg << "." << endl;
85        } else
86                msg << "There is not any registered class yet!" << endl;
87
88        it_error ( msg.str() );
89}
90
91const UI& UI::MappedUI::retrieve_ui ( const string &class_name ) {
92        StringToUIMap::const_iterator iter = mapped_strings().find ( class_name );
93        if ( iter == mapped_strings().end() )
94                unregistered_class_error ( class_name );
95
96        return *iter->second;
97}
98
99const string& UI::MappedUI::retrieve_class_name ( const type_info * const class_type_info ) {
100        TypeInfoToStringMap::const_iterator iter = mapped_type_infos().find ( class_type_info );
101        if ( iter == mapped_type_infos().end() )
102                unregistered_class_error ( "with RTTI name " + string ( class_type_info->name() ) );
103        return iter->second;
104}
105
106///////////////////////////// Class SettingResolver /////////////////////////////////////////////
107
108SettingResolver::SettingResolver ( const Setting &potential_link )
109                : result ( initialize_reference ( file, potential_link ) ) {
110}
111
112const Setting& SettingResolver::initialize_reference ( UIFile *&file, const Setting &potential_link ) {
113        file = NULL;
114
115        if ( potential_link.getType() !=  Setting::TypeString )
116                return potential_link;
117
118        string link = ( const char* ) potential_link;
119        size_t aerobase = link.find ( '@' );
120
121        const Setting *result;
122        if ( aerobase != string::npos ) {
123                string file_name = link.substr ( aerobase + 1, link.length() );
124                file = new UIFile ( file_name );
125                result = & ( Setting& ) ( *file );
126                link = link.substr ( 0, aerobase );
127        } else {
128                result = &potential_link;
129                while ( !result->isRoot() )
130                        result = &result->getParent();
131        }
132
133        if ( !result->exists ( link ) )
134                throw UIException ( "linked setting was not found", potential_link );
135
136        return ( *result ) [link];
137}
138
139SettingResolver::~SettingResolver() {
140        if ( file ) delete file;
141}
142
143///////////////////////////// Class UI /////////////////////////////////////////////
144
145void UI::assert_type ( const Setting &element, Setting::Type type ) {
146        if ( element.getType() != type )
147                throw UIException ( "wrong setting type", element );
148}
149
150const Setting& UI::to_child_setting ( const Setting &element, const int index ) {
151        if ( !element.isList() )
152                throw UIException ( "only TypeList elements could be indexed by integers", element );
153
154        return element[index];
155}
156
157const Setting& UI::to_child_setting ( const Setting &element, const string &name ) {
158        if ( !element.isGroup() )
159                throw UIException ( "only TypeGroup elements could be indexed by strings", element );
160
161        return element[name];
162}
163
164void UI::save ( const int &integer, Setting &element, const string &name ) {
165        Setting &set = ( name == "" ) ? element.add ( Setting::TypeInt )
166                       : element.add ( name, Setting::TypeInt );
167        set = integer;
168}
169
170void UI::save ( const double &real, Setting &element, const string &name ) {
171        Setting &set = ( name == "" ) ? element.add ( Setting::TypeFloat )
172                       : element.add ( name, Setting::TypeFloat );
173        set = real;
174}
175
176void UI::save ( const string &str, Setting &element, const string &name ) {
177        Setting &set = ( name == "" ) ? element.add ( Setting::TypeString )
178                       : element.add ( name, Setting::TypeString );
179        set = str;
180}
181
182void UI::save ( const mat &matrix, Setting &element, const string &name ) {
183        Setting &set = ( name == "" ) ? element.add ( Setting::TypeList )
184                       : element.add ( name, Setting::TypeList );
185
186        Setting &cols = set.add ( Setting::TypeInt );
187        cols = matrix.cols();
188
189        Setting &rows = set.add ( Setting::TypeInt );
190        rows = matrix.rows();
191
192        Setting &elements = set.add ( Setting::TypeArray );
193
194        // build matrix row-wise
195        for ( int i = 0; i < matrix.rows(); i++ )
196                for ( int j = 0; j < matrix.cols(); j++ ) {
197                        Setting &new_field = elements.add ( Setting::TypeFloat );
198                        new_field = matrix ( i, j );
199                }
200}
201
202void UI::save ( const ivec &vector, Setting &element, const string &name ) {
203        Setting &set = ( name == "" ) ? element.add ( Setting::TypeArray )
204                       : element.add ( name, Setting::TypeArray );
205        for ( int i = 0; i < vector.length(); i++ ) {
206                Setting &new_field = set.add ( Setting::TypeInt );
207                new_field = vector ( i );
208        }
209}
210
211void UI::save ( const vec &vector, Setting &element, const string &name ) {
212        Setting &set = ( name == "" ) ? element.add ( Setting::TypeArray )
213                       : element.add ( name, Setting::TypeArray );
214        for ( int i = 0; i < vector.length(); i++ ) {
215                Setting &new_field = set.add ( Setting::TypeFloat );
216                new_field = vector ( i );
217        }
218}
219
220void UI::from_setting ( mat& matrix, const Setting &element ) {
221        const SettingResolver link ( element );
222
223        if ( link.result.isNumber() ) {
224                matrix.set_size ( 1, 1 );
225                matrix ( 0, 0 ) = link.result;
226                return;
227        }
228
229        if ( link.result.isList() ) {
230                int data_offset;
231
232                if ( link.result.getLength() == 3 )
233                        data_offset = 0;
234                else if ( link.result.getLength() == 4 ) {
235                        assert_type ( link.result[0], Setting::TypeString );
236                        const char* elem1 = ( const char* ) link.result[0];
237                        if ( ( strcmp ( elem1, "matrix" ) ) )
238                                throw UIException ( "the setting supposed to represent a matrix element has wrong syntax", link.result );
239
240                        data_offset = 1;
241                } else
242                        throw UIException ( "the setting supposed to represent a matrix element has wrong syntax", link.result );
243
244                Setting &rows_setting = link.result[0 + data_offset];
245                Setting &cols_setting = link.result[1 + data_offset];
246                Setting &elements = link.result[2 + data_offset];
247
248                assert_type ( cols_setting, Setting::TypeInt );
249                assert_type ( rows_setting, Setting::TypeInt );
250                assert_type ( elements, Setting::TypeArray );
251
252                int cols = cols_setting;
253                int rows = rows_setting;
254
255                if ( cols < 0 || rows < 0 )
256                        throw UIException ( "the dimensions of a matrix has to be non-negative", link.result );
257
258                if ( elements.getLength() != cols * rows )
259                        throw UIException ( "the count of the matrix elements is incompatible with matrix dimension", elements );
260
261                matrix.set_size ( rows, cols );
262
263                if ( cols == 0 || rows == 0 )
264                        return;
265
266                if ( !elements[0].isNumber() )
267                        throw UIException ( "matrix elements have to be numbers", elements[0] );
268
269                // build matrix row-wise
270                int k = 0;
271                for ( int i = 0; i < rows; i++ )
272                        for ( int j = 0; j < cols; j++ )
273                                matrix ( i, j ) = elements[k++];
274                return;
275        }
276
277        throw UIException ( "only numeric types or TypeList are supported as matrix values", link.result );
278}
279
280void UI::from_setting ( vec &vector, const Setting &element ) {
281        const SettingResolver link ( element );
282
283        if ( link.result.isNumber() ) {
284                vector.set_length ( 1 );
285                vector ( 0 ) = link.result;
286                return;
287        }
288
289        if ( link.result.isList() ) {
290                mat matrix;
291                from_setting ( matrix, link.result );
292
293                if ( matrix.cols() != 1 && matrix.rows() != 1 )
294                        throw UIException ( "the vector length is invalid, it seems to be rather a matrix", link.result );
295
296                int len = matrix.rows() * matrix.cols();
297                vector.set_length ( len );
298                if ( len == 0 ) return;
299
300                if ( matrix.cols() == 1 )
301                        for ( int i = 0; i < len; i++ )
302                                vector ( i ) = matrix ( i, 0 );
303                else
304                        for ( int i = 0; i < len; i++ )
305                                vector ( i ) = matrix ( 0, i );
306                return;
307        }
308
309        if ( link.result.isArray() ) {
310                int len = link.result.getLength();
311                vector.set_length ( len );
312                if ( len == 0 ) return;
313
314                if ( !link.result[0].isNumber() )
315                        throw UIException ( "a vector element has to be a number", link.result[0] );
316
317                for ( int i = 0; i < len; i++ )
318                        vector ( i ) = link.result[i];
319
320                return;
321        }
322
323        throw UIException ( "only numeric types, TypeArray or TypeList are supported as vector values", link.result );
324}
325
326void UI::from_setting ( ivec &vector, const Setting &element ) {
327        vec double_vector;
328        from_setting ( double_vector, element );
329        int len = double_vector.length();
330        vector.set_length ( len );
331        for ( int i = 0; i < len; i++ )
332                vector ( i ) = ( int ) double_vector ( i );
333}
334
335void UI::from_setting ( string &str, const Setting &element ) {
336        assert_type ( element, Setting::TypeString );
337        str = ( const char* ) element;
338}
339
340void UI::from_setting ( int &integer, const Setting &element ) {
341//      assert_type ( element, Setting::TypeInt );
342        integer = element;
343}
344
345void UI::from_setting ( double &real, const Setting &element ) {
346//      assert_type ( element, Setting::TypeFloat );
347        real = element;
348}
349
350}
Note: See TracBrowser for help on using the browser.