src/misc/countedptr.hpp
changeset 7792 fa3c0a96e8a4
parent 6977 67b989528f3d
child 6872 1c4a4a609f85
equal deleted inserted replaced
7791:b279d235997a 7792:fa3c0a96e8a4
     3 /** @file countedptr.hpp */
     3 /** @file countedptr.hpp */
     4 
     4 
     5 #ifndef COUNTEDPTR_HPP
     5 #ifndef COUNTEDPTR_HPP
     6 #define COUNTEDPTR_HPP
     6 #define COUNTEDPTR_HPP
     7 
     7 
     8 #if 0 // reenable when needed
       
     9 /** @file CCountedPtr - smart pointer implementation */
     8 /** @file CCountedPtr - smart pointer implementation */
    10 
     9 
    11 /** CCountedPtr - simple reference counting smart pointer.
    10 /** CCountedPtr - simple reference counting smart pointer.
    12  *
    11  *
    13  *     One of the standard ways how to maintain object's lifetime.
    12  *     One of the standard ways how to maintain object's lifetime.
    42 	/** add one ref to the underlaying object */
    41 	/** add one ref to the underlaying object */
    43 	FORCEINLINE void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
    42 	FORCEINLINE void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
    44 
    43 
    45 public:
    44 public:
    46 	/** release smart pointer (and decrement ref count) if not null */
    45 	/** release smart pointer (and decrement ref count) if not null */
    47 	FORCEINLINE void Release() {if (m_pT != NULL) {m_pT->Release(); m_pT = NULL;}}
    46 	FORCEINLINE void Release() {if (m_pT != NULL) {Tcls* pT = m_pT; m_pT = NULL; pT->Release();}}
    48 
    47 
    49 	/** dereference of smart pointer - const way */
    48 	/** dereference of smart pointer - const way */
    50 	FORCEINLINE const Tcls* operator -> () const {assert(m_pT != NULL); return m_pT;};
    49 	FORCEINLINE const Tcls* operator -> () const {assert(m_pT != NULL); return m_pT;};
    51 
    50 
    52 	/** dereference of smart pointer - non const way */
    51 	/** dereference of smart pointer - non const way */
    54 
    53 
    55 	/** raw pointer casting operator - const way */
    54 	/** raw pointer casting operator - const way */
    56 	FORCEINLINE operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
    55 	FORCEINLINE operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
    57 
    56 
    58 	/** raw pointer casting operator - non-const way */
    57 	/** raw pointer casting operator - non-const way */
    59 	FORCEINLINE operator Tcls*() {assert(m_pT == NULL); return m_pT;}
    58 	FORCEINLINE operator Tcls*() {return m_pT;}
    60 
    59 
    61 	/** operator & to support output arguments */
    60 	/** operator & to support output arguments */
    62 	FORCEINLINE Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
    61 	FORCEINLINE Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
    63 
    62 
    64 	/** assignment operator from raw ptr */
    63 	/** assignment operator from raw ptr */
    65 	FORCEINLINE CCountedPtr& operator = (Tcls* pT) {Assign(pT); return *this;}
    64 	FORCEINLINE CCountedPtr& operator = (Tcls* pT) {Assign(pT); return *this;}
    66 
    65 
    67 	/** assignment operator from another smart ptr */
    66 	/** assignment operator from another smart ptr */
    68 	FORCEINLINE CCountedPtr& operator = (CCountedPtr& src) {Assign(src.m_pT); return *this;}
    67 	FORCEINLINE CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;}
    69 
    68 
    70 	/** assignment operator helper */
    69 	/** assignment operator helper */
    71 	FORCEINLINE void Assign(Tcls* pT);
    70 	FORCEINLINE void Assign(Tcls* pT);
    72 
    71 
    73 	/** one way how to test for NULL value */
    72 	/** one way how to test for NULL value */
    74 	FORCEINLINE bool IsNull() const {return m_pT == NULL;}
    73 	FORCEINLINE bool IsNull() const {return m_pT == NULL;}
    75 
    74 
    76 	/** another way how to test for NULL value */
    75 	/** another way how to test for NULL value */
    77 	FORCEINLINE bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
    76 	//FORCEINLINE bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
    78 
    77 
    79 	/** yet another way how to test for NULL value */
    78 	/** yet another way how to test for NULL value */
    80 	FORCEINLINE bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
    79 	//FORCEINLINE bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
    81 
    80 
    82 	/** assign pointer w/o incrementing ref count */
    81 	/** assign pointer w/o incrementing ref count */
    83 	FORCEINLINE void Attach(Tcls* pT) {Release(); m_pT = pT;}
    82 	FORCEINLINE void Attach(Tcls* pT) {Release(); m_pT = pT;}
    84 
    83 
    85 	/** detach pointer w/o decrementing ref count */
    84 	/** detach pointer w/o decrementing ref count */
    96 		m_pT = pT;                   // update m_pT to new value
    95 		m_pT = pT;                   // update m_pT to new value
    97 		if (pTold) pTold->Release(); // release old ptr if any
    96 		if (pTold) pTold->Release(); // release old ptr if any
    98 	}
    97 	}
    99 }
    98 }
   100 
    99 
   101 #endif /* 0 */
   100 /**
       
   101  * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
       
   102  * collections as item type. For example CCountedPtr has overloaded operator & which
       
   103  * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
       
   104  */
       
   105 template <class T> struct AdaptT {
       
   106 	T m_t;
       
   107 
       
   108 	/** construct by wrapping the given object */
       
   109 	AdaptT(const T &t)
       
   110 		: m_t(t)
       
   111 	{}
       
   112 
       
   113 	/** assignment operator */
       
   114 	T& operator = (const T &t)
       
   115 	{
       
   116 		m_t = t;
       
   117 		return t;
       
   118 	}
       
   119 
       
   120 	/** type-cast operator (used when AdaptT is used instead of T) */
       
   121 	operator T& ()
       
   122 	{
       
   123 		return m_t;
       
   124 	}
       
   125 
       
   126 	/** const type-cast operator (used when AdaptT is used instead of const T) */
       
   127 	operator const T& () const
       
   128 	{
       
   129 		return m_t;
       
   130 	}
       
   131 };
       
   132 
       
   133 
       
   134 /** Simple counted object. Use it as base of your struct/class if you want to use
       
   135  *  basic reference counting. Your struct/class will destroy and free itself when
       
   136  *  last reference to it is released (using Relese() method). The initial reference
       
   137  *  count (when it is created) is zero (don't forget AddRef() at least one time if
       
   138  *  not using CCountedPtr<T>.
       
   139  *
       
   140  *  @see misc/countedobj.cpp for implementation.
       
   141  */
       
   142 struct SimpleCountedObject {
       
   143 	int32 m_ref_cnt;
       
   144 
       
   145 	SimpleCountedObject()
       
   146 		: m_ref_cnt(0)
       
   147 	{}
       
   148 
       
   149 	virtual ~SimpleCountedObject()
       
   150 	{};
       
   151 
       
   152 	virtual int32 AddRef();
       
   153 	virtual int32 Release();
       
   154 	virtual void FinalRelease() {};
       
   155 };
       
   156 
       
   157 
       
   158 
       
   159 
   102 #endif /* COUNTEDPTR_HPP */
   160 #endif /* COUNTEDPTR_HPP */