Changeset 527 for library/bdm/base/user_info.h
- Timestamp:
- 08/13/09 15:58:32 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/user_info.h
r494 r527 24 24 #include "libconfig/libconfig.h++" 25 25 #include "../bdmroot.h" 26 #include "../shared_ptr.h" 26 27 #include "itpp/itbase.h" 27 28 … … 253 254 static void assert_type ( const Setting &element, Setting::Type type ); 254 255 255 //! Method assembling a typeless instance, it is implemented in descendant class ParticularUI<T> 256 /*! 257 \brief Method constructing a configured instance 258 259 The returned pointer must be allocated using operator new 260 (it's deleted at the end of its life cycle). The method is 261 implemented in descendant class ParticularUI<T>, which knows 262 the correct type T. 263 */ 256 264 virtual root* new_instance() const = 0; 257 265 … … 274 282 //! This method converts a Setting into a real scalar 275 283 static void from_setting ( double &real, const Setting &element ); 284 276 285 //! This method converts a Setting into a class T descendant 277 286 template<class T> static void from_setting ( T* &instance, const Setting &element ) { … … 287 296 const UI& related_UI = MappedUI::retrieve_ui ( class_name ); 288 297 289 root* typeless_instance = related_UI.new_instance(); 298 root *typeless_instance = related_UI.new_instance(); 299 it_assert_debug ( typeless_instance, "UI::new_instance failed" ); 290 300 291 301 instance = dynamic_cast<T*> ( typeless_instance ); 292 if ( !instance ) 302 if ( !instance ) { 303 delete typeless_instance; 293 304 throw UIException ( "class " + class_name + " is not a descendant of the desired output class. Try to call the UI::build<T> function with a different type parameter.", link.result ); 305 } 294 306 295 307 try { … … 301 313 } 302 314 315 //! This method converts a Setting into a descendant of class 316 //! T, wrapped in an instance of shared_ptr<T> . 317 template<class T> 318 static void from_setting ( shared_ptr<T> &instance, const Setting &element ) { 319 T *tmp_inst = 0; 320 from_setting ( tmp_inst, element ); 321 it_assert_debug ( tmp_inst, "UI::from_setting failed" ); 322 instance = tmp_inst; 323 } 324 303 325 //! This methods converts a Setting into a new templated array of type Array<T> 304 326 template<class T> static void from_setting ( Array<T> &array_to_load, const Setting &element ) { … … 320 342 //! The exception can help to find the place where the template is misused and also to correct it. 321 343 template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) { 322 throw UIException ( "from_setting is not implemented for this type", element ); 344 std::string msg = "from_setting is not implemented for type "; 345 msg += typeid(T).name(); 346 throw UIException ( msg, element ); 323 347 } 324 348 … … 341 365 //! The new instance of type T* is constructed and initialized with values stored in the Setting element[name] 342 366 //! 343 //! If there is not any sub-element named #name, the null pointer is returned. 344 template<class T> static T* build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) { 367 //! If there is not any sub-element named #name and settingPresence is #optional, an empty shared_ptr<T> is returned. When settingPresence is #compulsory, the returned shared_ptr<T> is never empty (an exception is thrown when the object isn't found). 368 template<class T> 369 static shared_ptr<T> build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) { 345 370 if ( !element.exists ( name ) ) { 346 371 if ( settingPresence == optional ) 347 return NULL;372 return shared_ptr<T>(); 348 373 else 349 374 throw UIException ( "the compulsory Setting named \"" + name + "\" is missing", element ); 350 375 } 351 376 352 T*instance;377 shared_ptr<T> instance; 353 378 from_setting<T> ( instance, to_child_setting ( element, name ) ); 354 379 return instance; … … 483 508 } 484 509 510 template< class T> static void save ( const shared_ptr<T> &instance, Setting &element, const string &name = "" ) { 511 save<T> ( instance.get(), element, name ); 512 } 513 485 514 //! An Array<T> instance is stored in the new child Setting appended to the passed element 486 515 template<class T> static void save ( const Array<T> &array_to_save, Setting &element, const string &name = "" ) {