Logo Search packages:      
Sourcecode: codeblocks version File versions  Download package

fnb_smart_ptr.h

#ifndef FNB_SMART_PTR_H
#define FNB_SMART_PTR_H


/**
 * A smart pointer class that provides a reference counting and auto delete memory.
 *
 * This class is similar to std::auto_ptr, with 2 exceptions:
 * - This class uses reference counting
 * - We dont provide a release() function (because of the reference counting)
 * It is recommended to use this class instead of using raw pointer wherever possible.
 *
 * \note smart pointer to NULL is valid.
 *
 * \ingroup CodeLite
 * \version 1.0
 * first version
 * \date 09-17-2006
 * \author Eran
 */
template <typename T>
00022 class wxFNBSmartPtr
{
      typedef T* type_ptr;

      /**
     * The reference counting class
     *
       * \ingroup CodeLite
       * \version 1.0
       * first version
       *
       * \date 09-17-2006
       * \author Eran
       */
00036       class SmartPtrRef
      {
            type_ptr m_data;
            int m_refCount;

      public:
            /**
             * Construct a reference counting class for row pointer data
             * \param data pointer
             */
00046             SmartPtrRef(type_ptr data)
                  : m_data( data )
                  , m_refCount( 1 )
            {
            }

            /**
             * Destructor
             */
00055             virtual ~SmartPtrRef()
            {
                  delete m_data;
            }

            /**
             * \return Pointer to the row data 
             */
00063             type_ptr GetData() { return m_data; }

            /**
             * Increase reference counting by 1
             */
00068             void IncRef() { m_refCount ++ ; }


            /**
             * Decrease reference counting by 1
             */
00074             void DecRef() { m_refCount -- ; }
            /**
             * Return the current reference counting
             * \return current reference counting
             */
00079             int  GetRefCount() { return m_refCount; }
      };

      SmartPtrRef *m_ref;

public:
      /**
       * Construct smart pointer from ptr
       * \param ptr pointer
       */
00089       wxFNBSmartPtr(type_ptr ptr)
      {
            // create a fresh copy
            CreateFresh( ptr );
      }
      
      /**
       * Default constructor
       */
00098       wxFNBSmartPtr()
            : m_ref(NULL)
      {
      }

      /**
       * Copy constructor
       * \param rhs right hand side 
       */
00107       wxFNBSmartPtr(const wxFNBSmartPtr& rhs)
            : m_ref(NULL)
      {
            *this = rhs;
      }

      /**
       * Assignment operator
       * \param rhs right hand side 
       * \return reference to this
       */
00118       wxFNBSmartPtr& operator=(const wxFNBSmartPtr& rhs)
      {
            // increase the reference count
            if( m_ref == rhs.m_ref )
                  return *this;

            // Delete previous reference 
            DeleteRefCount();

            if( !rhs.m_ref )
                  return *this;

            m_ref = rhs.m_ref;
            m_ref->IncRef();
            return *this;
      }

      /**
       * Destructor
       */
00138       virtual ~wxFNBSmartPtr()
      {
            DeleteRefCount();
      }

      /**
       * Replace the current pointer with ptr
       * if the current ptr is not NULL, it will be freed (reference counting free) before assingning the new ptr
       * \param ptr new pointer
       */
00148       void Reset(type_ptr ptr)
      {     
            DeleteRefCount();
            CreateFresh( ptr );
      }

      /**
       * Return pointer the row data pointer
       * \return pointer to the row data pointer
       */
00158       type_ptr Get()
      {
            return m_ref->GetData();
      }

      /**
       * Overload the '->' operator 
       * \return pointer to the row data pointer
       */
00167       type_ptr operator->() const 
      {
            return m_ref->GetData();
      }

      /**
       * Dereference operator
       * \return dereference the row data
       */
00176       T& operator*() const
      {
            return *(m_ref->GetData());
      }

      /**
       * Test for NULL operator
       * \return true if the internal row data or the reference counting class are NULL false otherwise
       */
00185       bool operator!() const
      {
            if( !m_ref )
                  return true;

            return m_ref->GetData() == NULL;
      }

private:
      void DeleteRefCount()
      {
            // decrease the ref count (or delete pointer if it is 1)
            if( m_ref )
            {
                  if( m_ref->GetRefCount() == 1 )
                  {
                        delete m_ref;
                        m_ref = NULL;
                  }
                  else
                        m_ref->DecRef();
            }
      };

      void CreateFresh(type_ptr ptr)
      {
            m_ref = new SmartPtrRef( ptr );
      }
};

#endif // FNB_SMART_PTR_H

Generated by  Doxygen 1.6.0   Back to index