root/bdm/user_info.cpp @ 377

Revision 377, 11.0 kB (checked in by mido, 15 years ago)

1) globalni prejmenovani Setting &root na Setting &set
2) smazani par zastaralych adresaru
3) oprava warningu v doc\local
4) prejmenovani SettingsResolver? na SettingResolver? a drobne vylepseni funkcnosti
5) odstranena duplikace kodu v user_info.cpp

  • Property svn:eol-style set to native
Line 
1//
2// C++ Implementation: itpp_ext
3//
4// Description:
5//
6//
7// Author: smidl <smidl@utia.cas.cz>, (C) 2008
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13#include "user_info.h"
14
15namespace bdm
16{
17///////////////////////// UI FILE /////////////////////////////////////////////
18
19UI_File::UI_File ()
20{
21    setAutoConvert( true );
22}
23
24//! loads root element from a file
25UI_File::UI_File ( const string &file_name )
26{
27    try
28    {
29        readFile( file_name.c_str()  );
30        setAutoConvert( true );
31    }
32    catch ( FileIOException f )
33    {
34        it_error ( "UI error: file " + file_name + " not found." );
35    }
36    catch ( ParseException& P )
37    {
38        stringstream msg;
39        msg << "UI error: parsing error """ << P.getError() << """ in file " << file_name << " on line " <<  P.getLine() << ".";
40        it_error ( msg.str() );
41    }
42}
43
44
45//! save UserInfo to the file (typically with an XML extension)
46void UI_File::save(  const string &file_name )
47{
48    try
49    {
50        writeFile ( file_name.c_str()  );
51    }
52    catch ( FileIOException f )
53    {
54        it_error( "UI error: file " + file_name + " is inacessible." );
55    }
56}
57
58UI_File::operator Setting&()
59{
60    return getRoot();
61}
62///////////////////////// INTERNAL MAPPED_UI /////////////////////////////////////////////
63
64UI::Mapped_UI::String_To_UI_Map& UI::Mapped_UI::mapped_strings()
65{
66    static String_To_UI_Map var;
67    return var;
68}
69
70UI::Mapped_UI::Type_Info_To_String_Map& UI::Mapped_UI::mapped_type_infos()
71{
72    static Type_Info_To_String_Map var;
73    return var;
74}
75
76void UI::Mapped_UI::add_class( const string &class_name, const type_info * const class_type_info, const UI* const ui )
77{
78    pair< const string, const UI* const > new_pair = make_pair( class_name, ui );
79    mapped_strings().insert( new_pair );
80    mapped_type_infos().insert( make_pair( class_type_info, new_pair.first ) );
81}
82
83const UI& UI::Mapped_UI::retrieve_ui( const string &class_name )
84{
85    String_To_UI_Map::const_iterator iter = mapped_strings().find( class_name );
86    if ( iter == mapped_strings().end())
87                // TODO dat sem vypis seznamu registrovanych trid
88        it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." );
89    return *iter->second;
90}
91
92const string& UI::Mapped_UI::retrieve_class_name( const type_info * const class_type_info )
93{
94    Type_Info_To_String_Map::const_iterator iter = mapped_type_infos().find( class_type_info );
95    if ( iter == mapped_type_infos().end())
96        it_error ( "UI error: class with RTTI name " + string(class_type_info->name()) + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." );
97    return iter->second;
98}
99
100///////////////////////// INTERNAL LINK EXPANDER /////////////////////////////////////////////
101
102UI::SettingResolver::SettingResolver( const Setting &potential_link ): result( initialize_reference( file, potential_link ) )
103{
104}
105
106const Setting& UI::SettingResolver::initialize_reference(UI_File *&file, const Setting &potential_link)
107{
108    if ( potential_link.getType() !=  Setting::TypeString )
109            return potential_link;
110       
111        string link = (const char*) potential_link;
112    size_t aerobase = link.find('@');
113
114        const Setting *result;
115    if ( aerobase != string::npos )
116    {
117        string file_name = link.substr( aerobase + 1, link.length() );
118        file = new UI_File( file_name );
119        result = &(Setting&)(*file);
120        link = link.substr( 0, aerobase );
121    }
122    else
123        {
124                file = NULL;
125                result = &potential_link;
126        while ( !result->isRoot() )
127            result = &result->getParent();
128        }
129
130    if ( !result->exists( link ) )
131        ui_error( "linked Setting was not found", potential_link );
132
133    return (*result)[link];
134}
135
136UI::SettingResolver::~SettingResolver()
137{
138    if ( file ) delete file;
139}
140
141///////////////////////// UI /////////////////////////////////////////////
142
143void UI::ui_error( string message, const Setting &element )
144{
145    stringstream error_message;
146    error_message << "UI error: " << message << "! Check path """ << element.getPath() << """, source line " << element.getSourceLine() << ".";
147    it_error ( error_message.str() );
148}
149
150//! This methods - kvuli ukladani pole stringu, dat jen privatne?
151void UI::save( const string &str, Setting &element )
152{
153    Setting &set = element.add( Setting::TypeString );
154    set = str;
155}
156
157void UI::save( const mat &matrix, Setting &element, const string &name)
158{
159
160    Setting &set = (name == "") ? element.add( Setting::TypeList )
161                    : element.add( name, Setting::TypeList );
162
163    Setting &cols = set.add( Setting::TypeInt );
164    cols = matrix.cols();
165
166    Setting &rows = set.add( Setting::TypeInt );
167    rows = matrix.rows();
168
169    Setting &elements = set.add( Setting::TypeArray );
170
171    // build matrix row-wise
172    for ( int i=0; i<matrix.rows(); i++ )
173        for ( int j=0; j<matrix.cols(); j++)
174        {
175            Setting &new_field = elements.add(Setting::TypeFloat);
176            new_field = matrix(i,j);
177        }
178}
179
180
181//! This methods tries to save a integer vector
182void UI::save( const ivec &vector, Setting &element, const string &name)
183{
184
185    Setting &set = (name == "") ? element.add( Setting::TypeArray )
186                    : element.add( name, Setting::TypeArray );
187    for ( int i=0; i<vector.length(); i++ )
188    {
189        Setting &new_field = set.add(Setting::TypeInt);
190        new_field = vector(i);
191    }
192}
193
194
195//! This methods tries to save a double vector
196void UI::save( const vec &vector, Setting &element, const string &name)
197{
198    Setting &set = (name == "") ? element.add( Setting::TypeArray )
199                    : element.add( name, Setting::TypeArray );
200    for ( int i=0; i<vector.length(); i++ )
201    {
202                Setting &new_field = set.add(Setting::TypeFloat);
203        new_field = vector(i);
204    }
205}
206
207
208//! This methods tries to build a new double matrix
209
210void UI::from_setting( mat& matrix, const Setting &element )
211{
212    const SettingResolver link( element );
213
214    if ( link.result.isNumber() )
215    {
216        matrix.set_size( 1, 1 );
217        matrix(0,0) = link.result;
218        return;
219    }
220
221    if ( link.result.isList() )
222    {
223        if ( link.result.getLength() != 3 )
224            ui_error( "the setting supposed to represent a matrix element has wrong syntax", link.result );
225
226        Setting &rows_setting = link.result[0];
227        Setting &cols_setting = link.result[1];
228        Setting &elements = link.result[2];
229
230        ASSERT_UITYPE(cols_setting,TypeInt);
231        ASSERT_UITYPE(rows_setting,TypeInt);
232        ASSERT_UITYPE(elements,TypeArray);
233
234        int cols = cols_setting;
235        int rows = rows_setting;
236
237        if ( cols < 0 | rows < 0 )
238            ui_error( "the dimensions of a matrix has to be non-negative", link.result );
239
240        if ( elements.getLength() != cols * rows )
241            ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements );
242
243        matrix.set_size( rows, cols );
244
245        if ( cols == 0 || rows == 0 )
246            return;
247
248        if ( !elements[0].isNumber() )
249            ui_error( "matrix elements have to be numbers", elements[0] );
250
251        // build matrix row-wise
252        int k = 0;
253        for ( int i=0; i<rows; i++ )
254            for ( int j=0; j<cols; j++)
255                matrix(i,j) = elements[k++];
256        return;
257    }
258
259    ui_error( "only numeric types or TypeList are supported as matrix values", link.result );
260}
261
262//! This methods tries to build a new integer vector
263void UI::from_setting( ivec &vector, const Setting &element )
264{
265    const SettingResolver link( element );
266
267    if ( link.result.isNumber() )
268    {
269        ASSERT_UITYPE(link.result,TypeInt);
270        vector.set_length( 1 );
271        vector(0) = link.result;
272        return;
273    }
274
275    if ( link.result.isList() )
276    {
277                mat matrix;
278                from_setting( matrix, link.result );
279
280        if ( matrix.cols() != 1 & matrix.rows() !=1)
281                        ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result );
282
283        int len = matrix.rows() * matrix.cols();
284        vector.set_length ( len );
285        if ( len == 0 ) return;
286
287                Setting &elements = link.result[2];
288        ASSERT_UITYPE(elements[0],TypeInt);
289
290
291        if ( matrix.cols() == 1 )
292                        for ( int i=0; i<len; i++ )
293                                vector(i) = matrix(i,1);
294                else
295                        for ( int i=0; i<len; i++ )
296                                vector(i) = matrix(1,i);
297        return;
298
299    }
300
301    if ( link.result.isArray() )
302    {
303        int len = link.result.getLength();
304        vector.set_length( len );
305        if ( len == 0 ) return;
306
307        ASSERT_UITYPE(link.result[0],TypeInt);
308        for ( int i=0; i < len; i++ )
309            vector(i) = link.result[i];
310        return;
311    }
312
313    ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result );
314}
315
316//! This methods tries to build a new double vector
317void UI::from_setting( vec &vector, const Setting &element )
318{
319    const SettingResolver link( element );
320
321    if ( link.result.isNumber() )
322    {
323        vector.set_length( 1 );
324        vector(0) = link.result;
325        return;
326    }
327
328    if ( link.result.isList() )
329    {
330                mat matrix;
331                from_setting( matrix, link.result );
332
333        if ( matrix.cols() != 1 & matrix.rows() !=1)
334                        ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result );
335
336        int len = matrix.rows() * matrix.cols();
337        vector.set_length ( len );
338        if ( len == 0 ) return;
339
340        if ( matrix.cols() == 1 )
341                        for ( int i=0; i<len; i++ )
342                                vector(i) = matrix(i,1);
343                else
344                        for ( int i=0; i<len; i++ )
345                                vector(i) = matrix(1,i);
346        return;
347    }
348
349    if ( link.result.isArray() )
350    {
351        int len = link.result.getLength();
352        vector.set_length( len );
353        if ( len == 0 ) return;
354
355        if ( !link.result[0].isNumber())
356            ui_error("a vector element has to be a number", link.result[0]);
357
358        for ( int i=0; i < len; i++ ) 
359            vector(i) = link.result[i];
360
361                return;
362    }
363
364    ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result );
365}
366
367
368void UI::from_setting( string &str, const Setting &element )
369{
370    ASSERT_UITYPE(element,TypeString);
371    str = (const char*) element;
372}
373
374
375///////////////////////// UI FILE /////////////////////////////////////////////
376//! This methods tries to save an instance of type T (or some of its descendant types)
377//! and build DOM tree accordingly. Then, it creates a new DOMNode named according class_name
378//! and connecti it to the passed Setting as a new child node.
379const Setting& UI::to_child_setting( const Setting &element, const int index )
380{
381    if ( !element.isList())
382        ui_error( "only TypeList elements could be indexed by integers", element );
383
384    if ( element.getLength() <= index )
385        ui_error( "there is not any child with index " + index, element );
386
387    return element[index];
388}
389
390const Setting& UI::to_child_setting( const Setting &element, const string &name )
391{
392    ASSERT_UITYPE(element,TypeGroup);
393    if ( !element.exists( name ) )
394        ui_error( "there is not any child named """ + name, element );
395    return element[name];
396}
397
398
399}
Note: See TracBrowser for help on using the browser.