Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

CountedObject.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (c) 2003-2005 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
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 
00090     //\
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()
00109         throw()
00110     {
00111         return _nCount;
00112     }
00113 
00119     _DWFCORE_API
00120     virtual void increment()
00121         throw()
00122     {
00123         DWFCore::AtomicIncrement( &_nCount );
00124     }
00125 
00133     _DWFCORE_API
00134     virtual void decrement()
00135         throw()
00136     {
00137         DWFCore::AtomicDecrement( &_nCount );
00138     }
00139 
00152     _DWFCORE_API
00153     virtual void destroy()
00154     {
00155         DWFCORE_FREE_OBJECT( this );
00156     }
00157 
00158 private:
00159 
00160     int _nCount;
00161 };
00162 
00163 
00173 class DWFCountedObjectPointer
00174 {
00175 
00176 public:
00177 
00189     _DWFCORE_API
00190     DWFCountedObjectPointer( DWFCountedObject* pObject )
00191         throw()
00192         : _pObject( pObject )
00193     {
00194         if (_pObject)
00195         {
00196             _pObject->increment();
00197         }
00198     }
00199 
00207     _DWFCORE_API
00208     virtual ~DWFCountedObjectPointer()
00209         throw()
00210     {
00211         _unbind();
00212     }
00213 
00223     _DWFCORE_API
00224     DWFCountedObjectPointer( const DWFCountedObjectPointer& rSrc )
00225         throw()
00226     {
00227         _pObject = rSrc._pObject;
00228 
00229         if (_pObject)
00230         {
00231             _pObject->increment();
00232         }
00233     }
00234 
00244     _DWFCORE_API
00245     DWFCountedObjectPointer& operator=( const DWFCountedObjectPointer& rSrc )
00246         throw()
00247     {
00248         _unbind();
00249 
00250         _pObject = rSrc._pObject;
00251 
00252         if (_pObject)
00253         {
00254             _pObject->increment();
00255         }
00256 
00257         return *this;
00258     }
00259 
00267     _DWFCORE_API
00268     operator DWFCountedObject*() const
00269         throw()
00270     {
00271         return _pObject;
00272     }
00273 
00281     _DWFCORE_API
00282     operator const DWFCountedObject*() const
00283         throw()
00284     {
00285         return (const DWFCountedObject*)_pObject;
00286     }
00287 
00296     _DWFCORE_API
00297     operator void*() const
00298         throw()
00299     {
00300         return (void*)_pObject;
00301     }
00302 
00311     _DWFCORE_API
00312     operator const void*() const
00313         throw()
00314     {
00315         return (const void*)_pObject;
00316     }
00317 
00325     _DWFCORE_API
00326     operator DWFCountedObject&() const
00327         throw()
00328     {
00329         return *_pObject;
00330     }
00331 
00339     _DWFCORE_API
00340     operator const DWFCountedObject&() const
00341         throw()
00342     {
00343         return (const DWFCountedObject&)*_pObject;
00344     }
00345 
00353     _DWFCORE_API
00354     bool null() const
00355         throw()
00356     {
00357         return (_pObject == NULL);
00358     }
00359 
00371     _DWFCORE_API
00372     bool operator==( const DWFCountedObjectPointer& rRHS ) const
00373         throw()
00374     {
00375         return (_pObject == rRHS._pObject);
00376     }
00377 
00390     _DWFCORE_API
00391     friend bool operator!=( const DWFCountedObjectPointer& rLHS,
00392                             const DWFCountedObjectPointer& rRHS)
00393         throw()
00394     {
00395         return (rLHS._pObject != rRHS._pObject);
00396     }
00397 
00398 private:
00399 
00400     void _unbind()
00401         throw()
00402     {
00403         if (_pObject)
00404         {
00405             _pObject->decrement();
00406             if (_pObject->count() == 0)
00407             {
00408                 _pObject->destroy();
00409                 _pObject = NULL;
00410             }
00411         }
00412     }
00413 
00414 private:
00415 
00416     DWFCountedObject* _pObject;
00417 };
00418 
00429 template<class T>
00430 class DWFParameterizedCountedObjectPointer : public DWFCountedObjectPointer
00431 {
00432 
00433 public:
00434 
00446     DWFParameterizedCountedObjectPointer( T* pObject )
00447         throw()
00448         : DWFCountedObjectPointer( pObject )
00449     {;}
00450 
00458     virtual ~DWFParameterizedCountedObjectPointer()
00459         throw()
00460     {;}
00461 
00471     DWFParameterizedCountedObjectPointer( const DWFParameterizedCountedObjectPointer& rSrc )
00472         throw()
00473         : DWFCountedObjectPointer( rSrc )
00474     {
00475         ;
00476     }
00477 
00487     DWFParameterizedCountedObjectPointer& operator=( const DWFParameterizedCountedObjectPointer& rSrc )
00488         throw()
00489     {
00490         (DWFCountedObjectPointer&)*this = (const DWFCountedObjectPointer&)rSrc;
00491         return *this;
00492     }
00493 
00501     operator T*() const
00502         throw()
00503     {
00504         return (T*)(DWFCountedObjectPointer::operator DWFCountedObject*());
00505     }
00506 
00514     operator const T*() const
00515         throw()
00516     {
00517         return (const T*)(DWFCountedObjectPointer::operator const DWFCountedObject*());
00518     }
00519 
00527     operator T&() const
00528         throw()
00529     {
00530         return (T&)*(DWFCountedObjectPointer::operator DWFCountedObject*());
00531     }
00532 
00540     operator const T&() const
00541         throw()
00542     {
00543         return (const T&)*(DWFCountedObjectPointer::operator const DWFCountedObject*());
00544     }
00545 
00553     T* operator->() const
00554         throw()
00555     {
00556         return (T*)(DWFCountedObjectPointer::operator DWFCountedObject*());
00557     }
00558 
00559 
00571     bool operator==( const DWFParameterizedCountedObjectPointer& rRHS ) const
00572         throw()
00573     {
00574         return ((DWFCountedObjectPointer&)*this == (DWFCountedObjectPointer&)rRHS);
00575     }
00576 
00589     friend bool operator!=( const DWFParameterizedCountedObjectPointer& rLHS,
00590                             const DWFParameterizedCountedObjectPointer& rRHS)
00591         throw()
00592     {
00593         return ((DWFCountedObjectPointer&)rLHS != (DWFCountedObjectPointer&)rRHS);
00594     }
00595 };
00596 
00597 }
00598 
00599 #endif

Generated on Tue May 17 12:05:10 2005 for Autodesk DWF Core Library by  doxygen 1.4.1