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