Changeset 114 for trunk/include/core


Ignore:
Timestamp:
Oct 20, 2005, 8:47:25 PM (19 years ago)
Author:
dkearney
Message:

updates to RpUnits to reduce memory leaks
created copy constructors, destructors, and copy assignment operators
for Units classes. removed conv as a data member and removed several
constructors. removed prev & next pointers from conversion class.
this removes linked list feature from conversion class which was not
being used. this update does not fix python's problem with performing
conversion between RpUnits objects or the allow for the recognition of
/cm3 as an object related to cm3.

also removed some outdated files rappture_interface.h, rappture_interface.c.
RpDict?.cc has been moved into RpDict?.h in previous checkins to reduce
dependencies on include files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/core/RpUnits.h

    r104 r114  
    5353                double&            exponent,
    5454                const RpUnits*     basis,
     55                unit*              prev,
    5556                unit*              next
    5657             )
     
    5859                exponent (exponent),
    5960                basis    (basis),
    60                 prev     (NULL),
     61                prev     (prev),
    6162                next     (next)
    6263            {};
     
    6465        /*
    6566        // private copy constructor
    66         unit ( unit& other )
    67             :   units       (other.units),
    68                 exponent    (other.exponent),
    69                 basis       (other.basis),
    70                 prev        (other.prev),
    71                 next        (other.next)
     67        unit ( const unit& other )
     68            :   units        other.units,
     69                exponent     other.exponent,
     70                basis        other.basis,
     71                prev         unit(other.prev),
     72                next         unit(other.next)
    7273            {};
    7374
    7475        // copy assignment operator
    75         unit& operator= (unit& other) {
    76             units       = other.units;
    77             exponent    = other.exponent;
    78             basis       = other.basis;
    79             prev        = other.prev;
    80             next        = other.next;
    81         }
    82 
     76        unit& operator= (unit& other)
     77            :   units        other.units,
     78                exponent     other.exponent,
     79                basis        RpUnits(other.basis),
     80                prev         unit(other.prev),
     81                next         unit(other.next)
     82        {}
     83        */
     84
     85        /*
    8386        // destructor (its not virtual yet, still testing)
    8487        ~unit () {
     
    106109
    107110        friend class RpUnits;
     111
     112        // copy constructor
     113        conversion ( conversion& other)
     114            :   fromPtr             (other.fromPtr),
     115                toPtr               (other.toPtr),
     116                convForwFxnPtr      (other.convForwFxnPtr),
     117                convBackFxnPtr      (other.convBackFxnPtr),
     118                convForwFxnPtrDD    (other.convForwFxnPtrDD),
     119                convBackFxnPtrDD    (other.convBackFxnPtrDD),
     120                convForwFxnPtrVoid  (other.convForwFxnPtrVoid),
     121                convForwData        (other.convForwData),
     122                convBackFxnPtrVoid  (other.convBackFxnPtrVoid),
     123                convBackData        (other.convBackData)
     124            {};
     125
     126        // copy assignment operator
     127        conversion& operator= ( conversion& other )
     128            {
     129                fromPtr             = other.fromPtr;
     130                toPtr               = other.toPtr;
     131                convForwFxnPtr      = other.convForwFxnPtr;
     132                convBackFxnPtr      = other.convBackFxnPtr;
     133                convForwFxnPtrDD    = other.convForwFxnPtrDD;
     134                convBackFxnPtrDD    = other.convBackFxnPtrDD;
     135                convForwFxnPtrVoid  = other.convForwFxnPtrVoid;
     136                convForwData        = other.convForwData;
     137                convBackFxnPtrVoid  = other.convBackFxnPtrVoid;
     138                convBackData        = other.convBackData;
     139                return *this;
     140            }
     141
     142        // default destructor
     143        virtual ~conversion ()
     144        {}
    108145
    109146    private:
     
    120157        void* convBackData;
    121158
    122         conversion* prev;
    123         conversion* next;
    124 
    125159        // constructor
    126160        // private because i only want RpUnits to be able to
     
    130164                const RpUnits* toPtr,
    131165                double (*convForwFxnPtr)(double),
    132                 double (*convBackFxnPtr)(double),
    133                 conversion* prev,
    134                 conversion* next
     166                double (*convBackFxnPtr)(double)
    135167             )
    136168            :   fromPtr             (fromPtr),
     
    143175                convForwData        (NULL),
    144176                convBackFxnPtrVoid  (NULL),
    145                 convBackData        (NULL),
    146                 prev                (prev),
    147                 next                (next)
    148             {};
    149 
     177                convBackData        (NULL)
     178            {};
     179
     180        // constructor for 2 argument conversion functions
    150181        conversion (
    151182                const RpUnits* fromPtr,
    152183                const RpUnits* toPtr,
    153184                double (*convForwFxnPtr)(double,double),
    154                 double (*convBackFxnPtr)(double,double),
    155                 conversion* prev,
    156                 conversion* next
     185                double (*convBackFxnPtr)(double,double)
    157186             )
    158187            :   fromPtr             (fromPtr),
     
    165194                convForwData        (NULL),
    166195                convBackFxnPtrVoid  (NULL),
    167                 convBackData        (NULL),
    168                 prev                (prev),
    169                 next                (next)
    170             {};
    171 
     196                convBackData        (NULL)
     197            {};
     198
     199        // constructor for user defined void* returning 1 arg conversion fxns.
     200        // the 1 arg is a user defined void* object
    172201        conversion (
    173202                const RpUnits* fromPtr,
     
    176205                void* convForwData,
    177206                void* (*convBackFxnPtrVoid)(void*, void*),
    178                 void* convBackData,
    179                 conversion* prev,
    180                 conversion* next
     207                void* convBackData
    181208             )
    182209            :   fromPtr             (fromPtr),
     
    189216                convForwData        (convForwData),
    190217                convBackFxnPtrVoid  (convBackFxnPtrVoid),
    191                 convBackData        (convBackData),
    192                 prev                (prev),
    193                 next                (next)
    194             {};
    195 
    196         // copy constructor
    197 
    198         // destructor
     218                convBackData        (convBackData)
     219            {};
     220
    199221};
    200222
     
    212234        friend class RpUnits;
    213235
     236        virtual ~convEntry()
     237        {}
     238
    214239    private:
    215240
     
    228253            {};
    229254
    230         /*
    231         ~convEntry()
    232         {
    233 
    234         }
    235         */
    236255};
    237256
     
    293312        static RpUnits * define(const std::string units,
    294313                                const RpUnits* basis);
     314                                // unsigned int create=0);
    295315        static RpUnits * defineCmplx(   const std::string units,
    296316                                        const RpUnits* basis);
     
    318338        // if group equals......................then load................
    319339        //      "all"                       load all available units
    320         //      "energy"                    load units related to length
     340        //      "energy"                    load units related to energy
    321341        //      "length"                    load units related to length
    322342        //      "temp"                      load units related to temperature
     
    325345        //  (no other groups have been created)
    326346
    327         static int addPresets (std::string group);
     347        static int addPresets (const std::string group);
    328348
    329349        // undefining a relation rule is probably not needed
    330350        // int undefine(); // delete a relation
    331 
    332         // convert fxn will be taken care of in the RpUnitsConversion class
    333         // i think
    334         // int convert(const char* from, const char* to);
    335351
    336352        // why are these functions friends...
     
    341357
    342358        // copy constructor
    343         RpUnits ( const RpUnits& myRpUnit )
     359        RpUnits (RpUnits &other)
     360            : head (NULL),
     361              convList (NULL)
    344362        {
    345 
    346             /*
    347             from = myRpUnit.from;
    348             to = myRpUnit.to;
    349             convertForw = myRpUnit.convertForw;
    350             convertBack = myRpUnit.convertBack;
    351             */
    352 
    353             // copy constructor for unit
    354             unit* tmp = NULL;
    355             unit* newUnit = NULL;
    356             unit* p = myRpUnit.head;
    357 
    358             while (p != NULL) {
    359                 newUnit = new unit(p->units, p->exponent,p->basis,NULL);
    360 
    361                 newUnit->prev = tmp;
    362                 if (tmp) {
    363                     tmp->next = newUnit;
     363            unit* p = NULL;
     364            unit* current = NULL;
     365            convEntry* q = NULL;
     366            convEntry* curr2 = NULL;
     367
     368            dict = other.dict;
     369
     370            if (other.head) {
     371                p = other.head;
     372                head = new unit(p->units, p->exponent, p->basis, NULL, NULL);
     373                current = head;
     374
     375                while (p->next) {
     376                    p = p->next;
     377                    current->next = new unit( p->units,
     378                                              p->exponent,
     379                                              p->basis,
     380                                              current,
     381                                              NULL        );
     382                    current = current->next;
    364383                }
    365 
    366                 tmp = newUnit;
    367                 p = p->next;
    368             }
    369 
    370             if (tmp) {
    371                 while (tmp->prev != NULL) {
    372                    tmp = tmp->prev;
     384            }
     385            if (other.convList) {
     386                q = other.convList;
     387                convList = new convEntry (q->conv,NULL,NULL);
     388                curr2 = convList;
     389                while (q->next) {
     390                    q = q->next;
     391                    curr2->next = new convEntry (q->conv,curr2,NULL);
     392                    curr2 = curr2->next;
    373393                }
    374394            }
    375 
    376             head = tmp;
    377 
    378         };
    379 
    380         /*
     395        }
     396
    381397        // copy assignment operator
    382         RpUnits& operator= (const RpUnits& myRpUnit) {
    383 
    384             if ( this != &myRpUnit ) {
     398        RpUnits& operator= (const RpUnits& other) {
     399
     400            unit* p = NULL;
     401            unit* current = NULL;
     402            convEntry* q = NULL;
     403            convEntry* curr2 = NULL;
     404
     405            if ( this != &other ) {
    385406                delete head;
    386407                delete convList;
    387                 delete conv;
    388             }
     408            }
     409
     410            dict = other.dict;
     411
     412            if (other.head) {
     413                p = other.head;
     414                head = new unit(p->units, p->exponent, p->basis, NULL, NULL);
     415                current = head;
     416
     417                while (p->next) {
     418                    p = p->next;
     419                    current->next = new unit( p->units,
     420                                              p->exponent,
     421                                              p->basis,
     422                                              current,
     423                                              NULL        );
     424                    current = current->next;
     425                }
     426            }
     427            if (other.convList) {
     428                q = other.convList;
     429                convList = new convEntry (q->conv,NULL,NULL);
     430                curr2 = convList;
     431                while (q->next) {
     432                    q = q->next;
     433                    curr2->next = new convEntry (q->conv,curr2,NULL);
     434                    curr2 = curr2->next;
     435                }
     436            }
     437
     438            return *this;
    389439        }
    390         */
     440
     441        // default destructor
     442        //
     443        ~RpUnits ()
     444        {
     445            // clean up dynamic memory
     446
     447            unit* p = head;
     448            unit* tmp = p;
     449            convEntry* p2 = convList;
     450            convEntry* tmp2 = p2;
     451
     452            while (p != NULL) {
     453                tmp = p;
     454                p = p->next;
     455                delete tmp;
     456            }
     457
     458            while (p2 != NULL) {
     459                tmp2 = p2;
     460                p2 = p2->next;
     461                delete tmp2;
     462            }
     463        }
    391464
    392465    private:
     
    415488        mutable convEntry* convList;
    416489
    417         // used by the RpUnits when defining conversion elements
    418         conversion* conv;
    419490
    420491        // dictionary to store the units.
     
    436507                    const RpUnits* basis
    437508                )
    438             :   head        ( new unit( units, exponent, basis, NULL) ),
    439                 convList    (NULL),
    440                 conv        (NULL)
     509            :   head        ( new unit( units, exponent, basis, NULL, NULL) ),
     510                convList    (NULL)
    441511        {};
    442512
    443         // create a conversion element
    444 
    445 
    446 
    447 
    448         RpUnits (
    449                     const RpUnits* from,
    450                     const RpUnits* to,
    451                     double (*convForwFxnPtr)(double),
    452                     double (*convBackFxnPtr)(double),
    453                     conversion* prev,
    454                     conversion* next
    455                 )
    456             :   head (NULL),
    457                 convList (NULL),
    458                 conv (new conversion
    459                         (from,to,convForwFxnPtr,convBackFxnPtr,prev,next))
    460         {
    461             connectConversion(from);
    462             connectConversion(to);
    463         };
    464 
    465 
    466         RpUnits (
    467                     const RpUnits* from,
    468                     const RpUnits* to,
    469                     double (*convForwFxnPtr)(double,double),
    470                     double (*convBackFxnPtr)(double,double),
    471                     conversion* prev,
    472                     conversion* next
    473                 )
    474             :   head (NULL),
    475                 convList (NULL),
    476                 conv (new conversion
    477                         (from,to,convForwFxnPtr,convBackFxnPtr,prev,next))
    478         {
    479             connectConversion(from);
    480             connectConversion(to);
    481         };
    482 
    483 
    484 
    485         RpUnits (
    486                     const RpUnits* from,
    487                     const RpUnits* to,
    488                     void* (*convForwFxnPtr)(void*, void*),
    489                     void* convForwData,
    490                     void* (*convBackFxnPtr)(void*, void*),
    491                     void* convBackData,
    492                     conversion* prev,
    493                     conversion* next
    494                 )
    495             :   head (NULL),
    496                 convList (NULL),
    497                 conv (new conversion (  from, to,
    498                                         convForwFxnPtr, convForwData,
    499                                         convBackFxnPtr, convBackData,
    500                                         prev, next
    501                                      )
    502                      )
    503         {
    504             connectConversion(from);
    505             connectConversion(to);
    506         };
    507 
    508         // default destructor
    509         //
    510         ~RpUnits ()
    511         {
    512             // clean up dynamic memory
    513 
    514             unit* p = head;
    515             while (p != NULL) {
    516                 unit* tmp = p;
    517                 p = p->next;
    518                 delete tmp;
    519             }
    520         }
    521 
    522         void RpUnits::connectConversion(const RpUnits* myRpUnit);
    523 
    524         void RpUnits::fillMetricMap();
    525 
    526         // create the container keeping track of defined units
    527         // returns 0 on success (table created or already exists)
    528         // returns !0 on failure (table not created or cannot exist)
    529         int RpUnits::createUnitsTable();
     513        void RpUnits::connectConversion(conversion* conv) const;
    530514
    531515        // insert new RpUnits object into RpUnitsTable
     
    537521        // returns 0 on success (linking within table was successful)
    538522        // returns !0 on failure (linking was not successful)
    539         int RpUnits::link(RpUnits * unitA);
     523        // int RpUnits::link(RpUnits * unitA);
    540524
    541525
     
    561545/*--------------------------------------------------------------------------*/
    562546
    563 // #include "../src/RpUnits.cc"
    564 
    565547#endif
Note: See TracChangeset for help on using the changeset viewer.