- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- File List
- File Members

# TAutomaticPointer.hpp

Go to the documentation of this file.

    00001 #ifndef __TAutomaticPointer__
    00002 #define __TAutomaticPointer__
    00003
    00004 #include "TPointerDeleter.hpp"
    00005 using Exponent::Collections::TCountedObjectDeleter;
    00006
    00007 //  ===========================================================================
    00008 namespace Exponent
    00009 {
    00010     namespace Collections
    00011     {
    00071         template <class TypeName> class TAutomaticPointer : public CCountedObject
    00072         {
    00074             EXPONENT_CLASS_DECLARATION;
    00077 //  ===========================================================================
    00078
    00079         public:
    00080
    00081 //  ===========================================================================
    00082
    00083             static TCountedObjectDeleter<TypeName> TAUTOMATICPOINTER_DEFAULT_DELETER;
    00085 //  ===========================================================================
    00086
    00087
    00092             TAutomaticPointer(TPointerDeleter<TypeName> *pointerDeleter = &TAUTOMATICPOINTER_DEFAULT_DELETER) : m_pointer(NULL), m_pointerDeletionHandler(NULL)
    00093             {
    00094                 EXPONENT_CLASS_CONSTRUCTION(TAutomaticPointer<TypeName>);
    00095                 NULL_POINTER(m_pointer);
    00096                 m_pointerDeletionHandler = pointerDeleter;
    00097             }
    00098
    00104             TAutomaticPointer(TypeName *pointer, TPointerDeleter<TypeName> *pointerDeleter = &TAUTOMATICPOINTER_DEFAULT_DELETER) : m_pointer(NULL), m_pointerDeletionHandler(NULL)
    00105             {
    00106                 EXPONENT_CLASS_CONSTRUCTION(TAutomaticPointer<TypeName>);
    00107
    00108                 // If valid store the pointer
    00109                 if (pointer)
    00110                 {
    00111                     m_pointer = pointer;
    00112                 }
    00113
    00114                 // STore the deleter
    00115                 m_pointerDeletionHandler = pointerDeleter;
    00116
    00117                 // If we have, then we want to notify the handler
    00118                 if (m_pointerDeletionHandler && m_pointer)
    00119                 {
    00120                     m_pointerDeletionHandler->pointerAdded(m_pointer);
    00121                 }
    00122             }
    00123
    00129             TAutomaticPointer(const TAutomaticPointer<TypeName> &pointer, TPointerDeleter<TypeName> *pointerDeleter = &TAUTOMATICPOINTER_DEFAULT_DELETER) : m_pointer(NULL), m_pointerDeletionHandler(NULL)
    00130             {
    00131                 EXPONENT_CLASS_CONSTRUCTION(TAutomaticPointer<TypeName>);
    00132                 NULL_POINTER(m_pointer);
    00133                 m_pointerDeletionHandler = pointerDeleter;
    00134                 *this = pointer;
    00135             }
    00136
    00140             virtual ~TAutomaticPointer()
    00141             {
    00142                 EXPONENT_CLASS_DESTRUCTION(TAutomaticPointer<TypeName>);
    00143                 this->freePointer();
    00144             }
    00145
    00146 //  ===========================================================================
    00147
    00153             TAutomaticPointer<TypeName> &operator = (const TAutomaticPointer<TypeName> &pointer)
    00154             {
    00155                 // Do we have one currently
    00156                 if (m_pointer)
    00157                 {
    00158                     // First we have to delete the old one
    00159                     if (m_pointerDeletionHandler)
    00160                     {
    00161                         // Delete it
    00162                         m_pointerDeletionHandler->deletePointer(m_pointer);
    00163                         NULL_POINTER(m_pointer);
    00164                     }
    00165                     else
    00166                     {
    00167                         FREE_POINTER(m_pointer);
    00168                     }
    00169                 }
    00170
    00171                 // Now we copy the new one
    00172                 m_pointer = pointer.m_pointer;
    00173
    00174                 // If we have, then we want to notify the handler
    00175                 if (m_pointerDeletionHandler && m_pointer)
    00176                 {
    00177                     m_pointerDeletionHandler->pointerAdded(m_pointer);
    00178                 }
    00179
    00180                 return *this;
    00181             }
    00182
    00183
    00189             TAutomaticPointer<TypeName> &operator = (TypeName *pointer)
    00190             {
    00191                 // Do we have one currently
    00192                 if (m_pointer)
    00193                 {
    00194                     // First we have to delete the old one
    00195                     if (m_pointerDeletionHandler)
    00196                     {
    00197                         // Delete it
    00198                         m_pointerDeletionHandler->deletePointer(m_pointer);
    00199                         NULL_POINTER(m_pointer);
    00200                     }
    00201                     else
    00202                     {
    00203                         FREE_POINTER(m_pointer);
    00204                     }
    00205                 }
    00206
    00207                 // Now we copy the new one
    00208                 if (pointer)
    00209                 {
    00210                     m_pointer = pointer;
    00211                 }
    00212
    00213                 // If we have, then we want to notify the handler
    00214                 if (m_pointerDeletionHandler && m_pointer)
    00215                 {
    00216                     m_pointerDeletionHandler->pointerAdded(m_pointer);
    00217                 }
    00218
    00219                 return *this;
    00220             }
    00221
    00222 //  ===========================================================================
    00223
    00228             TypeName *operator -> () { return m_pointer; }
    00229
    00234             TypeName &operator * () { return *m_pointer; }
    00235
    00240             bool pointerIsValid() const { return m_pointer != NULL; }
    00241
    00245             void freePointer()
    00246             {
    00247                 // Ifwe have a handler
    00248                 if (m_pointerDeletionHandler)
    00249                 {
    00250                     // Delete it
    00251                     m_pointerDeletionHandler->deletePointer(m_pointer);
    00252                 }
    00253                 else
    00254                 {
    00255                     FREE_POINTER(m_pointer);
    00256                 }
    00257                 NULL_POINTER(m_pointer);
    00258             }
    00259
    00264             const TypeName *getPointer() const { return m_pointer; }
    00265
    00270             TypeName *getMutablePointer() { return m_pointer; }
    00271
    00272 //  ===========================================================================
    00273
    00274         protected:
    00275
    00276 //  ===========================================================================
    00277
    00278             TypeName *m_pointer;
    00279             TPointerDeleter<TypeName> *m_pointerDeletionHandler;
    00281         };
    00282
    00284         EXPONENT_TEMPLATE_CLASS_IMPLEMENTATION(TAutomaticPointer<TypeName>, TypeName, CCountedObject);
    00285         template<class TypeName> TCountedObjectDeleter<TypeName> TAutomaticPointer<TypeName>::TAUTOMATICPOINTER_DEFAULT_DELETER;
    00287     }
    00288 }
    00289 #endif