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

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

possibly broken?

  • 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        file = NULL;
109
110    if ( potential_link.getType() !=  Setting::TypeString )
111            return potential_link;
112       
113        string link = (const char*) potential_link;
114    size_t aerobase = link.find('@');
115
116        const Setting *result;
117    if ( aerobase != string::npos )
118    {
119        string file_name = link.substr( aerobase + 1, link.length() );
120        file = new UI_File( file_name );
121        result = &(Setting&)(*file);
122        link = link.substr( 0, aerobase );
123    }
124    else
125        {
126                result = &potential_link;
127        while ( !result->isRoot() )
128            result = &result->getParent();
129        }
130
131    if ( !result->exists( link ) )
132        ui_error( "linked Setting was not found", potential_link );
133
134    return (*result)[link];
135}
136
137UI::SettingResolver::~SettingResolver()
138{
139    if ( file ) delete file;
140}
141
142///////////////////////// UI /////////////////////////////////////////////
143
144void UI::ui_error( string message, const Setting &element )
145{
146    stringstream error_message;
147    error_message << "UI error: " << message << "! Check path """ << element.getPath() << """, source line " << element.getSourceLine() << ".";
148    it_error ( error_message.str() );
149}
150
151//! This methods - kvuli ukladani pole stringu, dat jen privatne?
152void UI::save( const string &str, Setting &element )
153{
154    Setting &set = element.add( Setting::TypeString );
155    set = str;
156}
157
158void UI::save( const mat &matrix, Setting &element, const string &name)
159{
160
161    Setting &set = (name == "") ? element.add( Setting::TypeList )
162                    : element.add( name, Setting::TypeList );
163
164    Setting &cols = set.add( Setting::TypeInt );
165    cols = matrix.cols();
166
167    Setting &rows = set.add( Setting::TypeInt );
168    rows = matrix.rows();
169
170    Setting &elements = set.add( Setting::TypeArray );
171
172    // build matrix row-wise
173    for ( int i=0; i<matrix.rows(); i++ )
174        for ( int j=0; j<matrix.cols(); j++)
175        {
176            Setting &new_field = elements.add(Setting::TypeFloat);
177            new_field = matrix(i,j);
178        }
179}
180
181
182//! This methods tries to save a integer vector
183void UI::save( const ivec &vector, Setting &element, const string &name)
184{
185
186    Setting &set = (name == "") ? element.add( Setting::TypeArray )
187                    : element.add( name, Setting::TypeArray );
188    for ( int i=0; i<vector.length(); i++ )
189    {
190        Setting &new_field = set.add(Setting::TypeInt);
191        new_field = vector(i);
192    }
193}
194
195
196//! This methods tries to save a double vector
197void UI::save( const vec &vector, Setting &element, const string &name)
198{
199    Setting &set = (name == "") ? element.add( Setting::TypeArray )
200                    : element.add( name, Setting::TypeArray );
201    for ( int i=0; i<vector.length(); i++ )
202    {
203                Setting &new_field = set.add(Setting::TypeFloat);
204        new_field = vector(i);
205    }
206}
207
208
209//! This methods tries to build a new double matrix
210
211void UI::from_setting( mat& matrix, const Setting &element )
212{
213    const SettingResolver link( element );
214
215    if ( link.result.isNumber() )
216    {
217        matrix.set_size( 1, 1 );
218        matrix(0,0) = link.result;
219        return;
220    }
221
222    if ( link.result.isList() )
223    {
224        if ( link.result.getLength() != 3 )
225            ui_error( "the setting supposed to represent a matrix element has wrong syntax", link.result );
226
227        Setting &rows_setting = link.result[0];
228        Setting &cols_setting = link.result[1];
229        Setting &elements = link.result[2];
230
231        ASSERT_UITYPE(cols_setting,TypeInt);
232        ASSERT_UITYPE(rows_setting,TypeInt);
233        ASSERT_UITYPE(elements,TypeArray);
234
235        int cols = cols_setting;
236        int rows = rows_setting;
237
238        if ( cols < 0 | rows < 0 )
239            ui_error( "the dimensions of a matrix has to be non-negative", link.result );
240
241        if ( elements.getLength() != cols * rows )
242            ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements );
243
244        matrix.set_size( rows, cols );
245
246        if ( cols == 0 || rows == 0 )
247            return;
248
249        if ( !elements[0].isNumber() )
250            ui_error( "matrix elements have to be numbers", elements[0] );
251
252        // build matrix row-wise
253        int k = 0;
254        for ( int i=0; i<rows; i++ )
255            for ( int j=0; j<cols; j++)
256                matrix(i,j) = elements[k++];
257        return;
258    }
259
260    ui_error( "only numeric types or TypeList are supported as matrix values", link.result );
261}
262
263//! This methods tries to build a new integer vector
264void UI::from_setting( ivec &vector, const Setting &element )
265{
266    const SettingResolver link( element );
267
268    if ( link.result.isNumber() )
269    {
270        ASSERT_UITYPE(link.result,TypeInt);
271        vector.set_length( 1 );
272        vector(0) = link.result;
273        return;
274    }
275
276    if ( link.result.isList() )
277    {
278                mat matrix;
279                from_setting( matrix, link.result );
280
281        if ( matrix.cols() != 1 & matrix.rows() !=1)
282                        ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result );
283
284        int len = matrix.rows() * matrix.cols();
285        vector.set_length ( len );
286        if ( len == 0 ) return;
287
288                Setting &elements = link.result[2];
289        ASSERT_UITYPE(elements[0],TypeInt);
290
291
292        if ( matrix.cols() == 1 )
293                        for ( int i=0; i<len; i++ )
294                                vector(i) = matrix(i,1);
295                else
296                        for ( int i=0; i<len; i++ )
297                                vector(i) = matrix(1,i);
298        return;
299
300    }
301
302    if ( link.result.isArray() )
303    {
304        int len = link.result.getLength();
305        vector.set_length( len );
306        if ( len == 0 ) return;
307
308        ASSERT_UITYPE(link.result[0],TypeInt);
309        for ( int i=0; i < len; i++ )
310            vector(i) = link.result[i];
311        return;
312    }
313
314    ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result );
315}
316
317//! This methods tries to build a new double vector
318void UI::from_setting( vec &vector, const Setting &element )
319{
320    const SettingResolver link( element );
321
322    if ( link.result.isNumber() )
323    {
324        vector.set_length( 1 );
325        vector(0) = link.result;
326        return;
327    }
328
329    if ( link.result.isList() )
330    {
331                mat matrix;
332                from_setting( matrix, link.result );
333
334        if ( matrix.cols() != 1 & matrix.rows() !=1)
335                        ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result );
336
337        int len = matrix.rows() * matrix.cols();
338        vector.set_length ( len );
339        if ( len == 0 ) return;
340
341        if ( matrix.cols() == 1 )
342                        for ( int i=0; i<len; i++ )
343                                vector(i) = matrix(i,1);
344                else
345                        for ( int i=0; i<len; i++ )
346                                vector(i) = matrix(1,i);
347        return;
348    }
349
350    if ( link.result.isArray() )
351    {
352        int len = link.result.getLength();
353        vector.set_length( len );
354        if ( len == 0 ) return;
355
356        if ( !link.result[0].isNumber())
357            ui_error("a vector element has to be a number", link.result[0]);
358
359        for ( int i=0; i < len; i++ ) 
360            vector(i) = link.result[i];
361
362                return;
363    }
364
365    ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result );
366}
367
368
369void UI::from_setting( string &str, const Setting &element )
370{
371    ASSERT_UITYPE(element,TypeString);
372    str = (const char*) element;
373}
374
375
376///////////////////////// UI FILE /////////////////////////////////////////////
377//! This methods tries to save an instance of type T (or some of its descendant types)
378//! and build DOM tree accordingly. Then, it creates a new DOMNode named according class_name
379//! and connecti it to the passed Setting as a new child node.
380const Setting& UI::to_child_setting( const Setting &element, const int index )
381{
382    if ( !element.isList())
383        ui_error( "only TypeList elements could be indexed by integers", element );
384
385    if ( element.getLength() <= index )
386        ui_error( "there is not any child with index " + index, element );
387
388    return element[index];
389}
390
391const Setting& UI::to_child_setting( const Setting &element, const string &name )
392{
393    ASSERT_UITYPE(element,TypeGroup);
394    if ( !element.exists( name ) )
395        ui_error( "there is not any child named """ + name, element );
396    return element[name];
397}
398
399
400}
Note: See TracBrowser for help on using the browser.