Show
Ignore:
Timestamp:
08/13/09 15:58:32 (15 years ago)
Author:
vbarta
Message:

using shared_ptr in UI (optionally so far; loading & saving Array<T *> still works but should be phased out); testsuite run leaks down from 8822 to 480 bytes

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/user_info.h

    r494 r527  
    2424#include "libconfig/libconfig.h++" 
    2525#include "../bdmroot.h" 
     26#include "../shared_ptr.h" 
    2627#include "itpp/itbase.h" 
    2728 
     
    253254        static void assert_type ( const Setting &element, Setting::Type type ); 
    254255 
    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        */ 
    256264        virtual root* new_instance() const = 0; 
    257265 
     
    274282        //! This method converts a Setting into a real scalar 
    275283        static void from_setting ( double &real, const Setting &element ); 
     284 
    276285        //! This method converts a Setting into a class T descendant 
    277286        template<class T> static void from_setting ( T* &instance, const Setting &element ) { 
     
    287296                const UI& related_UI = MappedUI::retrieve_ui ( class_name ); 
    288297 
    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" ); 
    290300 
    291301                instance = dynamic_cast<T*> ( typeless_instance ); 
    292                 if ( !instance ) 
     302                if ( !instance ) { 
     303                        delete typeless_instance; 
    293304                        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                } 
    294306 
    295307                try { 
     
    301313        } 
    302314 
     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 
    303325        //! This methods converts a Setting into a new templated array of type Array<T> 
    304326        template<class T> static void from_setting ( Array<T> &array_to_load, const Setting &element ) { 
     
    320342        //! The exception can help to find the place where the template is misused and also to correct it. 
    321343        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 ); 
    323347        } 
    324348 
     
    341365        //! The new instance of type T* is constructed and initialized with values stored in the Setting element[name] 
    342366        //! 
    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 ) { 
    345370                if ( !element.exists ( name ) ) { 
    346371                        if ( settingPresence == optional ) 
    347                                 return NULL; 
     372                                return shared_ptr<T>(); 
    348373                        else 
    349374                                throw UIException ( "the compulsory Setting named \"" + name + "\" is missing", element ); 
    350375                } 
    351376 
    352                 T* instance; 
     377                shared_ptr<T> instance; 
    353378                from_setting<T> ( instance, to_child_setting ( element, name ) ); 
    354379                return instance; 
     
    483508        } 
    484509 
     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 
    485514        //! An Array<T> instance is stored in the new child Setting appended to the passed element 
    486515        template<class T> static void save ( const Array<T> &array_to_save, Setting &element, const string &name = "" ) {