summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiecore/osmartpointer.h (renamed from libopie2/opiecore/osmart_pointer.h)27
1 files changed, 14 insertions, 13 deletions
diff --git a/libopie2/opiecore/osmart_pointer.h b/libopie2/opiecore/osmartpointer.h
index 2a2518f..9000e71 100644
--- a/libopie2/opiecore/osmart_pointer.h
+++ b/libopie2/opiecore/osmartpointer.h
@@ -20,100 +20,100 @@ _;:, .> :=|. This program is free software; you can
20 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU 20 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
21..}^=.= = ; Library General Public License for more 21..}^=.= = ; Library General Public License for more
22++= -. .` .: details. 22++= -. .` .: details.
23: = ...= . :.=- 23: = ...= . :.=-
24-. .:....=;==+<; You should have received a copy of the GNU 24-. .:....=;==+<; You should have received a copy of the GNU
25 -_. . . )=. = Library General Public License along with 25 -_. . . )=. = Library General Public License along with
26 -- :-=` this library; see the file COPYING.LIB. 26 -- :-=` this library; see the file COPYING.LIB.
27 If not, write to the Free Software Foundation, 27 If not, write to the Free Software Foundation,
28 Inc., 59 Temple Place - Suite 330, 28 Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. 29 Boston, MA 02111-1307, USA.
30*/ 30*/
31 31
32#ifndef _osmart_pointer_h 32#ifndef _OSmartPointer_h
33#define _osmart_pointer_h 33#define _OSmartPointer_h
34 34
35/*! 35/*!
36 * \file osmart_pointer.h 36 * \file OSmartPointer.h
37 * \brief smart pointer and reference counter 37 * \brief smart pointer and reference counter
38 * \author Rajko Albrecht 38 * \author Rajko Albrecht
39 * 39 *
40 */ 40 */
41 41
42namespace Opie { 42namespace Opie {
43 43
44//! simple reference counter class 44//! simple reference counter class
45class oref_count { 45class ORefCount {
46protected: 46protected:
47 //! reference count member 47 //! reference count member
48 long m_RefCount; 48 long m_RefCount;
49public: 49public:
50 //! first reference must be added after "new" via Pointer() 50 //! first reference must be added after "new" via Pointer()
51 oref_count() : m_RefCount(0) 51 ORefCount() : m_RefCount(0)
52 {} 52 {}
53 virtual ~oref_count() {} 53 virtual ~ORefCount() {}
54 //! add a reference 54 //! add a reference
55 void Incr() { 55 void Incr() {
56 ++m_RefCount; 56 ++m_RefCount;
57 } 57 }
58 //! delete a reference 58 //! delete a reference
59 void Decr() { 59 void Decr() {
60 --m_RefCount; 60 --m_RefCount;
61 } 61 }
62 //! is it referenced 62 //! is it referenced
63 bool Shared() { return (m_RefCount > 0); } 63 bool Shared() { return (m_RefCount > 0); }
64}; 64};
65 65
66//! reference counting wrapper class 66//! reference counting wrapper class
67template<class T> class osmart_pointer { 67template<class T> class OSmartPointer {
68 //! pointer to object 68 //! pointer to object
69 /*! 69 /*!
70 * this object must contain Incr(), Decr() and Shared() 70 * this object must contain Incr(), Decr() and Shared()
71 * methode as public members. The best way is, that it will be a child 71 * methode as public members. The best way is, that it will be a child
72 * class of RefCount 72 * class of RefCount
73 */ 73 */
74 T *ptr; 74 T *ptr;
75public: 75public:
76 //! standart constructor 76 //! standart constructor
77 osmart_pointer() { ptr = NULL; } 77 OSmartPointer() { ptr = NULL; }
78 //! standart destructor 78 //! standart destructor
79 /*! 79 /*!
80 * release the reference, if it were the last reference, destroys 80 * release the reference, if it were the last reference, destroys
81 * ptr 81 * ptr
82 */ 82 */
83 ~osmart_pointer() 83 ~OSmartPointer()
84 { 84 {
85 if (ptr){ 85 if (ptr){
86 ptr->Decr(); 86 ptr->Decr();
87 if (!ptr->Shared()) 87 if (!ptr->Shared())
88 delete ptr; 88 delete ptr;
89 } 89 }
90 } 90 }
91 //! construction 91 //! construction
92 osmart_pointer(T* t) { if (ptr = t) ptr->Incr(); } 92 OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); }
93 //! Pointer copy 93 //! Pointer copy
94 osmart_pointer(const osmart_pointer<T>& p) 94 OSmartPointer(const OSmartPointer<T>& p)
95 { if (ptr = p.ptr) ptr->Incr(); } 95 { if (ptr = p.ptr) ptr->Incr(); }
96 //! pointer copy by assignment 96 //! pointer copy by assignment
97 osmart_pointer<T>& operator= (const osmart_pointer<T>& p) 97 OSmartPointer<T>& operator= (const OSmartPointer<T>& p)
98 { 98 {
99 // already same: nothing to do 99 // already same: nothing to do
100 if (ptr == p.ptr) return *this; 100 if (ptr == p.ptr) return *this;
101 // decouple reference 101 // decouple reference
102 if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } 102 if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; }
103 // establish new reference 103 // establish new reference
104 if (ptr = p.ptr) ptr->Incr(); 104 if (ptr = p.ptr) ptr->Incr();
105 return *this; 105 return *this;
106 } 106 }
107 osmart_pointer<T>& operator= (T*p) 107 OSmartPointer<T>& operator= (T*p)
108 { 108 {
109 if (ptr==p)return *this; 109 if (ptr==p)return *this;
110 if (ptr) { 110 if (ptr) {
111 ptr->Decr(); 111 ptr->Decr();
112 if (!ptr->Shared()) delete ptr; 112 if (!ptr->Shared()) delete ptr;
113 } 113 }
114 if (ptr=p) ptr->Incr(); 114 if (ptr=p) ptr->Incr();
115 return *this; 115 return *this;
116 } 116 }
117 117
118 //! cast to conventional pointer 118 //! cast to conventional pointer
119 operator T* () const { return ptr; } 119 operator T* () const { return ptr; }
@@ -133,12 +133,13 @@ public:
133 //! "if (pointer)" as non const 133 //! "if (pointer)" as non const
134 operator bool () { return ptr != NULL;} 134 operator bool () { return ptr != NULL;}
135 135
136 //! support if (!pointer)" 136 //! support if (!pointer)"
137 bool operator! () const { return (ptr == NULL); } 137 bool operator! () const { return (ptr == NULL); }
138 //! support if (!pointer)" as non const 138 //! support if (!pointer)" as non const
139 bool operator! () { return (ptr == NULL); } 139 bool operator! () { return (ptr == NULL); }
140}; 140};
141 141
142} 142}
143 143
144#endif 144#endif
145