KUDr@3900: /* $Id$ */ KUDr@3900: 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. KUDr@3900: * KUDr@3900: * One of the standard ways how to maintain object's lifetime. KUDr@3900: * KUDr@3900: * See http://ootips.org/yonat/4dev/smart-pointers.html for more KUDr@3900: * general info about smart pointers. KUDr@3900: * KUDr@3900: * This class implements ref-counted pointer for objects/interfaces that KUDr@3900: * support AddRef() and Release() methods. KUDr@3900: */ 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 */ KUDr@3900: FORCEINLINE void Release() {if (m_pT != NULL) {m_pT->Release(); m_pT = NULL;}} 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 */ KUDr@3900: FORCEINLINE operator Tcls*() {assert(m_pT == NULL); 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 */ KUDr@3900: FORCEINLINE CCountedPtr& operator = (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 */ KUDr@3900: 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 */ KUDr@3900: 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: KUDr@3900: KUDr@3900: #endif /* COUNTEDPTR_HPP */