Diffstat (limited to 'libopie2/opiecore/osmartpointer.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiecore/osmartpointer.h | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/libopie2/opiecore/osmartpointer.h b/libopie2/opiecore/osmartpointer.h index e9cee0c..f2f6464 100644 --- a/libopie2/opiecore/osmartpointer.h +++ b/libopie2/opiecore/osmartpointer.h @@ -1,12 +1,12 @@ // -*- Mode: C++; -*- /* This file is part of the Opie Project Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> Copyright (C) The Opie Team <opie-devel@handhelds.org> - =. + =. .=l. .>+-= _;:, .> :=|. This program is free software; you can .> <`_, > . <= redistribute it and/or modify it under :`=1 )Y*s>-.-- : the terms of the GNU Library General Public .="- .-=="i, .._ License as published by the Free Software @@ -45,35 +45,30 @@ namespace Opie { namespace Core { //! simple reference counter class class ORefCount { protected: //! reference count member - long m_RefCount; + unsigned long m_RefCount; public: //! first reference must be added after "new" via Pointer() - ORefCount() : m_RefCount(0) - {} - virtual ~ORefCount() {} + ORefCount(); + virtual ~ORefCount(); //! add a reference - void Incr() { - ++m_RefCount; - } + void Incr(); //! delete a reference - void Decr() { - --m_RefCount; - } + void Decr(); //! is it referenced - bool Shared() { return (m_RefCount > 0); } + bool Shared(); }; //! reference counting wrapper class template<class T> class OSmartPointer { //! pointer to object /*! - * this object must contain Incr(), Decr() and Shared() + * this object must contain Incr(), Decr() and Shared() * methode as public members. The best way is, that it will be a child * class of RefCount */ T *ptr; public: //! standart constructor @@ -91,21 +86,21 @@ public: delete ptr; } } //! construction OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } //! Pointer copy - OSmartPointer(const OSmartPointer<T>& p) + OSmartPointer(const OSmartPointer<T>& p) { if (ptr = p.ptr) ptr->Incr(); } //! pointer copy by assignment - OSmartPointer<T>& operator= (const OSmartPointer<T>& p) + OSmartPointer<T>& operator= (const OSmartPointer<T>& p) { // already same: nothing to do if (ptr == p.ptr) return *this; // decouple reference - if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } + if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } // establish new reference if (ptr = p.ptr) ptr->Incr(); return *this; } OSmartPointer<T>& operator= (T*p) { @@ -117,28 +112,28 @@ public: if (ptr=p) ptr->Incr(); return *this; } //! cast to conventional pointer operator T* () const { return ptr; } - + //! deref: fails for NULL pointer T& operator* () {return *ptr; } //! deref: fails for NULL pointer const T& operator* ()const {return *ptr; } //! deref with method call T* operator-> () {return ptr; } //! deref with const method call const T* operator-> ()const {return ptr; } - + //! supports "if (pointer)" operator bool () const { return (ptr != NULL); } //! "if (pointer)" as non const operator bool () { return ptr != NULL;} - + //! support if (!pointer)" bool operator! () const { return (ptr == NULL); } //! support if (!pointer)" as non const bool operator! () { return (ptr == NULL); } }; |