Changeset 527 for library/bdm/base

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

Location:
library/bdm/base
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.cpp

    r507 r527  
    169169 
    170170void mpdf::from_setting ( const Setting &set ) { 
    171         RV *r = UI::build<RV> ( set, "rv" ); 
     171        shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
    172172        if ( r ) { 
    173173                set_rv ( *r ); 
    174                 delete r; 
    175         } 
    176  
    177         r = UI::build<RV> ( set, "rvc" ); 
     174        } 
     175 
     176        r = UI::build<RV> ( set, "rvc", UI::optional ); 
    178177        if ( r ) { 
    179178                set_rvc ( *r ); 
    180                 delete r; 
    181179        } 
    182180} 
  • library/bdm/base/bdmbase.h

    r513 r527  
    379379                //!@} 
    380380                void from_setting (const Setting &set) { 
    381                         RV* r = UI::build<RV> (set, "rv"); 
     381                        shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
    382382                        if (r) { 
    383383                                set_rv (*r); 
    384                                 delete r; 
    385384                        } 
    386385                } 
     
    455454                        return rvc; 
    456455                } 
    457                 int dimension() { 
     456 
     457                int dimension() const { 
    458458                        return ep->dimension(); 
    459459                } 
  • library/bdm/base/datasources.cpp

    r477 r527  
    5757 
    5858void ArxDS::from_setting ( const Setting &set ) { 
    59         RV *yrv = UI::build<RV> ( set, "y" , UI::compulsory ); 
    60         RV *urv = UI::build<RV> ( set, "u" , UI::compulsory ); 
    61         RV *rrv = UI::build<RV> ( set, "rgr" , UI::compulsory ); 
     59        shared_ptr<RV> yrv = UI::build<RV> ( set, "y" , UI::compulsory ); 
     60        shared_ptr<RV> urv = UI::build<RV> ( set, "u" , UI::compulsory ); 
     61        shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr" , UI::compulsory ); 
    6262 
    6363        mat Th; 
     
    112112 
    113113void ITppFileDS::from_setting ( const Setting &set ) { 
    114         RV* rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory ); 
     114        shared_ptr<RV> rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory ); 
    115115 
    116116        it_file it ( set["filename"] ); 
     
    131131        ut.set_length ( 0 ); 
    132132 
     133#if 0 
    133134        RV* rvtmp = UI::build<RV> ( set["IM"], "rvu", UI::compulsory ); 
    134135        //set_drv(rvtmp); 
     136#endif 
    135137} 
  • library/bdm/base/datasources.h

    r477 r527  
    173173        }; 
    174174        //! Set 
    175         void set_drv ( RV &yrv, RV &urv, RV &rrv ) { 
     175        void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) { 
    176176                Rrv = rrv; 
    177177                Urv = urv; 
     
    265265 
    266266class stateDS : public DS { 
     267private: 
     268        //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 
     269        shared_ptr<mpdf> IM; 
     270 
     271        //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 
     272        shared_ptr<mpdf> OM; 
     273 
    267274protected: 
    268         //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$ 
    269         mpdf* IM; 
    270         //!conditional pdf of the observations \f$ f(d_t|x_t) \f$ 
    271         mpdf* OM; 
    272275        //! result storage 
    273276        vec dt; 
     
    278281        //! Logger 
    279282        int L_xt; 
     283 
    280284public: 
    281285        void getdata ( vec &dt0 ) { 
     
    286290        } 
    287291 
    288         stateDS ( mpdf* IM0, mpdf* OM0, int usize ) : DS ( ), IM ( IM0 ), OM ( OM0 ), 
    289                         dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {} 
    290  
    291         stateDS() {} 
    292  
    293         ~stateDS() { 
    294                 delete IM; 
    295                 delete OM; 
    296         } 
     292        stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ), 
     293                dt ( OM0->dimension() ), xt ( IM0->dimension() ), 
     294                ut ( usize ), L_xt(0) { } 
     295 
     296        stateDS() : L_xt(0) { } 
     297 
    297298        virtual void step() { 
    298299                xt = IM->samplecond ( concat ( xt, ut ) ); 
    299300                dt = OM->samplecond ( concat ( xt, ut ) ); 
    300         }; 
     301        } 
    301302 
    302303        virtual void log_add ( logger &L ) { 
  • 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 = "" ) {