KUDr@3900: /* $Id$ */ KUDr@3900: belugas@6977: /** @file countedptr.hpp */ belugas@6977: KUDr@3900: #ifndef COUNTEDPTR_HPP KUDr@3900: #define COUNTEDPTR_HPP KUDr@3900: KUDr@3900: /** @file CCountedPtr - smart pointer implementation */ KUDr@3900: KUDr@3900: /** CCountedPtr - simple reference counting smart pointer. rubidium@4549: * rubidium@4549: * One of the standard ways how to maintain object's lifetime. rubidium@4549: * rubidium@4549: * See http://ootips.org/yonat/4dev/smart-pointers.html for more rubidium@4549: * general info about smart pointers. rubidium@4549: * rubidium@4549: * This class implements ref-counted pointer for objects/interfaces that rubidium@4549: * support AddRef() and Release() methods. rubidium@4549: */ KUDr@3900: template KUDr@3900: class CCountedPtr { KUDr@3900: /** redefine the template argument to make it visible for derived classes */ KUDr@3900: public: KUDr@3900: typedef Tcls_ Tcls; KUDr@3900: KUDr@3900: protected: KUDr@3900: /** here we hold our pointer to the target */ KUDr@3900: Tcls* m_pT; KUDr@3900: KUDr@3900: public: KUDr@3900: /** default (NULL) construct or construct from a raw pointer */ KUDr@3900: FORCEINLINE CCountedPtr(Tcls* pObj = NULL) : m_pT(pObj) {AddRef();}; KUDr@3900: KUDr@3900: /** copy constructor (invoked also when initializing from another smart ptr) */ KUDr@3900: FORCEINLINE CCountedPtr(const CCountedPtr& src) : m_pT(src.m_pT) {AddRef();}; KUDr@3900: KUDr@3900: /** destructor releasing the reference */ KUDr@3900: FORCEINLINE ~CCountedPtr() {Release();}; KUDr@3900: KUDr@3900: protected: KUDr@3900: /** add one ref to the underlaying object */ KUDr@3900: FORCEINLINE void AddRef() {if (m_pT != NULL) m_pT->AddRef();} KUDr@3900: KUDr@3900: public: KUDr@3900: /** release smart pointer (and decrement ref count) if not null */ rubidium@7792: FORCEINLINE void Release() {if (m_pT != NULL) {Tcls* pT = m_pT; m_pT = NULL; pT->Release();}} KUDr@3900: KUDr@3900: /** dereference of smart pointer - const way */ KUDr@3900: FORCEINLINE const Tcls* operator -> () const {assert(m_pT != NULL); return m_pT;}; KUDr@3900: KUDr@3900: /** dereference of smart pointer - non const way */ KUDr@3900: FORCEINLINE Tcls* operator -> () {assert(m_pT != NULL); return m_pT;}; KUDr@3900: KUDr@3900: /** raw pointer casting operator - const way */ KUDr@3900: FORCEINLINE operator const Tcls*() const {assert(m_pT == NULL); return m_pT;} KUDr@3900: KUDr@3900: /** raw pointer casting operator - non-const way */ rubidium@7792: FORCEINLINE operator Tcls*() {return m_pT;} KUDr@3900: KUDr@3900: /** operator & to support output arguments */ KUDr@3900: FORCEINLINE Tcls** operator &() {assert(m_pT == NULL); return &m_pT;} KUDr@3900: KUDr@3900: /** assignment operator from raw ptr */ KUDr@3900: FORCEINLINE CCountedPtr& operator = (Tcls* pT) {Assign(pT); return *this;} KUDr@3900: KUDr@3900: /** assignment operator from another smart ptr */ rubidium@7792: FORCEINLINE CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;} KUDr@3900: KUDr@3900: /** assignment operator helper */ KUDr@3900: FORCEINLINE void Assign(Tcls* pT); KUDr@3900: KUDr@3900: /** one way how to test for NULL value */ KUDr@3900: FORCEINLINE bool IsNull() const {return m_pT == NULL;} KUDr@3900: KUDr@3900: /** another way how to test for NULL value */ rubidium@7792: //FORCEINLINE bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;} KUDr@3900: KUDr@3900: /** yet another way how to test for NULL value */ rubidium@7792: //FORCEINLINE bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;} KUDr@3900: KUDr@3900: /** assign pointer w/o incrementing ref count */ KUDr@3900: FORCEINLINE void Attach(Tcls* pT) {Release(); m_pT = pT;} KUDr@3900: KUDr@3900: /** detach pointer w/o decrementing ref count */ KUDr@3900: FORCEINLINE Tcls* Detach() {Tcls* pT = m_pT; m_pT = NULL; return pT;} KUDr@3900: }; KUDr@3900: KUDr@3900: template KUDr@3900: FORCEINLINE void CCountedPtr::Assign(Tcls* pT) KUDr@3900: { KUDr@3900: // if they are the same, we do nothing KUDr@3900: if (pT != m_pT) { KUDr@3900: if (pT) pT->AddRef(); // AddRef new pointer if any KUDr@3900: Tcls* pTold = m_pT; // save original ptr KUDr@3900: m_pT = pT; // update m_pT to new value KUDr@3900: if (pTold) pTold->Release(); // release old ptr if any KUDr@3900: } KUDr@3900: } KUDr@3900: rubidium@7792: /** rubidium@7792: * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl rubidium@7792: * collections as item type. For example CCountedPtr has overloaded operator & which rubidium@7792: * prevents using CCountedPtr in stl collections (i.e. std::list >) rubidium@7792: */ rubidium@7792: template struct AdaptT { rubidium@7792: T m_t; rubidium@7792: rubidium@7792: /** construct by wrapping the given object */ rubidium@7792: AdaptT(const T &t) rubidium@7792: : m_t(t) rubidium@7792: {} rubidium@7792: rubidium@7792: /** assignment operator */ rubidium@7792: T& operator = (const T &t) rubidium@7792: { rubidium@7792: m_t = t; rubidium@7792: return t; rubidium@7792: } rubidium@7792: rubidium@7792: /** type-cast operator (used when AdaptT is used instead of T) */ rubidium@7792: operator T& () rubidium@7792: { rubidium@7792: return m_t; rubidium@7792: } rubidium@7792: rubidium@7792: /** const type-cast operator (used when AdaptT is used instead of const T) */ rubidium@7792: operator const T& () const rubidium@7792: { rubidium@7792: return m_t; rubidium@7792: } rubidium@7792: }; rubidium@7792: rubidium@7792: rubidium@7792: /** Simple counted object. Use it as base of your struct/class if you want to use rubidium@7792: * basic reference counting. Your struct/class will destroy and free itself when rubidium@7792: * last reference to it is released (using Relese() method). The initial reference rubidium@7792: * count (when it is created) is zero (don't forget AddRef() at least one time if rubidium@7792: * not using CCountedPtr. rubidium@7792: * rubidium@7792: * @see misc/countedobj.cpp for implementation. rubidium@7792: */ rubidium@7792: struct SimpleCountedObject { rubidium@7792: int32 m_ref_cnt; rubidium@7792: rubidium@7792: SimpleCountedObject() rubidium@7792: : m_ref_cnt(0) rubidium@7792: {} rubidium@7792: rubidium@7792: virtual ~SimpleCountedObject() rubidium@7792: {}; rubidium@7792: rubidium@7792: virtual int32 AddRef(); rubidium@7792: virtual int32 Release(); rubidium@7792: virtual void FinalRelease() {}; rubidium@7792: }; rubidium@7792: rubidium@7792: rubidium@7792: rubidium@7792: KUDr@3900: #endif /* COUNTEDPTR_HPP */