CountedObject.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (c) 2003-2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 
00023 #ifndef _DWFCORE_COUNTED_OBJECT_H
00024 #define _DWFCORE_COUNTED_OBJECT_H
00025 
00030 
00031 #include "dwfcore/Core.h"
00032 
00033 
00034 namespace DWFCore
00035 {
00036 
00048     class DWFCountedObject : virtual public DWFCoreMemory
00049 {
00050 
00051 public:
00052 
00059     _DWFCORE_API
00060     DWFCountedObject( int nInitialCount = 0 )
00061         throw()
00062         : _nCount( nInitialCount )
00063     {;}
00064 
00070     _DWFCORE_API
00071     virtual ~DWFCountedObject()
00072         throw()
00073     {;}
00074 
00080     _DWFCORE_API
00081     DWFCountedObject( const DWFCountedObject& rSrc )
00082         throw()
00083         : _nCount( rSrc._nCount )
00084     {;}
00085 
00091     _DWFCORE_API
00092     DWFCountedObject& operator=( const DWFCountedObject& rSrc )
00093         throw()
00094     {
00095         _nCount = rSrc._nCount;
00096         return *this;
00097     }
00098 
00107     _DWFCORE_API
00108     int count() const
00109         throw()
00110     {
00111         return _nCount;
00112     }
00113 
00119     _DWFCORE_API
00120     virtual void increment() const
00121         throw()
00122     {
00123         DWFCore::AtomicIncrement( &_nCount );
00124     }
00125 
00133     _DWFCORE_API
00134     virtual void decrement() const
00135         throw()
00136     {
00137         DWFCore::AtomicDecrement( &_nCount );
00138     }
00139 
00152     _DWFCORE_API
00153     virtual void destroy()
00154     {
00155         // Note : (todo) the DWFCORE_FREE_OBJECT macro expands to "delete pointer; pointer = NULL;
00156         //        However, it is very important to note that "this" cannot be set to a value 
00157         //        i.e. the usage of the statement "this = NULL;" will result in a error.
00158         delete this;
00159         //DWFCORE_FREE_OBJECT( this );
00160     }
00161 
00162 private:
00163 
00164     mutable int _nCount;
00165 };
00166 
00167 
00177 class DWFCountedObjectPointer : virtual public DWFCoreMemory
00178 {
00179 
00180 public:
00181 
00193     _DWFCORE_API
00194     DWFCountedObjectPointer( DWFCountedObject* pObject )
00195         throw()
00196         : _pObject( pObject )
00197     {
00198         if (_pObject)
00199         {
00200             _pObject->increment();
00201         }
00202     }
00203 
00211     _DWFCORE_API
00212     virtual ~DWFCountedObjectPointer()
00213         throw()
00214     {
00215         _unbind();
00216     }
00217 
00227     _DWFCORE_API
00228     DWFCountedObjectPointer( const DWFCountedObjectPointer& rSrc )
00229         throw()
00230     {
00231         _pObject = rSrc._pObject;
00232 
00233         if (_pObject)
00234         {
00235             _pObject->increment();
00236         }
00237     }
00238 
00248     _DWFCORE_API
00249     DWFCountedObjectPointer& operator=( const DWFCountedObjectPointer& rSrc )
00250         throw()
00251     {
00252         _unbind();
00253 
00254         _pObject = rSrc._pObject;
00255 
00256         if (_pObject)
00257         {
00258             _pObject->increment();
00259         }
00260 
00261         return *this;
00262     }
00263 
00271     _DWFCORE_API
00272     operator DWFCountedObject*() const
00273         throw()
00274     {
00275         return _pObject;
00276     }
00277 
00285     _DWFCORE_API
00286     operator const DWFCountedObject*() const
00287         throw()
00288     {
00289         return (const DWFCountedObject*)_pObject;
00290     }
00291 
00300     _DWFCORE_API
00301     operator void*() const
00302         throw()
00303     {
00304         return (void*)_pObject;
00305     }
00306 
00315     _DWFCORE_API
00316     operator const void*() const
00317         throw()
00318     {
00319         return (const void*)_pObject;
00320     }
00321 
00329     _DWFCORE_API
00330     operator DWFCountedObject&() const
00331         throw()
00332     {
00333         return *_pObject;
00334     }
00335 
00343     _DWFCORE_API
00344     operator const DWFCountedObject&() const
00345         throw()
00346     {
00347         return (const DWFCountedObject&)*_pObject;
00348     }
00349 
00358     _DWFCORE_API
00359     bool isNull() const
00360         throw()
00361     {
00362         return (_pObject == NULL);
00363     }
00364 
00376     _DWFCORE_API
00377     bool operator==( const DWFCountedObjectPointer& rRHS ) const
00378         throw()
00379     {
00380         return (_pObject == rRHS._pObject);
00381     }
00382 
00395     _DWFCORE_API
00396     friend bool operator!=( const DWFCountedObjectPointer& rLHS,
00397                             const DWFCountedObjectPointer& rRHS)
00398         throw()
00399     {
00400         return (rLHS._pObject != rRHS._pObject);
00401     }
00402 
00403 private:
00404 
00405     void _unbind()
00406         throw()
00407     {
00408         if (_pObject)
00409         {
00410             _pObject->decrement();
00411             if (_pObject->count() == 0)
00412             {
00413                 _pObject->destroy();
00414                 _pObject = NULL;
00415             }
00416         }
00417     }
00418 
00419 private:
00420 
00421     DWFCountedObject* _pObject;
00422 };
00423 
00434 template<class T>
00435 class DWFParameterizedCountedObjectPointer : public DWFCountedObjectPointer
00436                                            , virtual public DWFCoreMemory
00437 {
00438 
00439 public:
00440 
00452     DWFParameterizedCountedObjectPointer( T* pObject )
00453         throw()
00454         : DWFCountedObjectPointer( pObject )
00455     {;}
00456 
00464     virtual ~DWFParameterizedCountedObjectPointer()
00465         throw()
00466     {;}
00467 
00477     DWFParameterizedCountedObjectPointer( const DWFParameterizedCountedObjectPointer& rSrc )
00478         throw()
00479         : DWFCountedObjectPointer( rSrc )
00480     {
00481         ;
00482     }
00483 
00493     DWFParameterizedCountedObjectPointer& operator=( const DWFParameterizedCountedObjectPointer& rSrc )
00494         throw()
00495     {
00496         (DWFCountedObjectPointer&)*this = (const DWFCountedObjectPointer&)rSrc;
00497         return *this;
00498     }
00499 
00507     operator T*() const
00508         throw()
00509     {
00510         return (T*)(DWFCountedObjectPointer::operator DWFCountedObject*());
00511     }
00512 
00520     operator const T*() const
00521         throw()
00522     {
00523         return (const T*)(DWFCountedObjectPointer::operator const DWFCountedObject*());
00524     }
00525 
00533     operator T&() const
00534         throw()
00535     {
00536         return (T&)*(DWFCountedObjectPointer::operator DWFCountedObject*());
00537     }
00538 
00546     operator const T&() const
00547         throw()
00548     {
00549         return (const T&)*(DWFCountedObjectPointer::operator const DWFCountedObject*());
00550     }
00551 
00559     T* operator->() const
00560         throw()
00561     {
00562         return (T*)(DWFCountedObjectPointer::operator DWFCountedObject*());
00563     }
00564 
00565 
00577     bool operator==( const DWFParameterizedCountedObjectPointer& rRHS ) const
00578         throw()
00579     {
00580         return ((DWFCountedObjectPointer&)*this == (DWFCountedObjectPointer&)rRHS);
00581     }
00582 
00595     friend bool operator!=( const DWFParameterizedCountedObjectPointer& rLHS,
00596                             const DWFParameterizedCountedObjectPointer& rRHS)
00597         throw()
00598     {
00599         return ((DWFCountedObjectPointer&)rLHS != (DWFCountedObjectPointer&)rRHS);
00600     }
00601 };
00602 
00603 }
00604 
00605 #endif

Generated on Tue Jan 6 22:39:28 2009 for Autodesk DWF Core Library by  doxygen 1.4.5