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,18 +1,18 @@ | |||
1 | // -*- Mode: C++; -*- | 1 | // -*- Mode: C++; -*- |
2 | /* | 2 | /* |
3 | This file is part of the Opie Project | 3 | This file is part of the Opie Project |
4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> | 4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> |
5 | Copyright (C) The Opie Team <opie-devel@handhelds.org> | 5 | Copyright (C) The Opie Team <opie-devel@handhelds.org> |
6 | =. | 6 | =. |
7 | .=l. | 7 | .=l. |
8 | .>+-= | 8 | .>+-= |
9 | _;:, .> :=|. This program is free software; you can | 9 | _;:, .> :=|. This program is free software; you can |
10 | .> <`_, > . <= redistribute it and/or modify it under | 10 | .> <`_, > . <= redistribute it and/or modify it under |
11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
12 | .="- .-=="i, .._ License as published by the Free Software | 12 | .="- .-=="i, .._ License as published by the Free Software |
13 | - . .-<_> .<> Foundation; either version 2 of the License, | 13 | - . .-<_> .<> Foundation; either version 2 of the License, |
14 | ._= =} : or (at your option) any later version. | 14 | ._= =} : or (at your option) any later version. |
15 | .%`+i> _;_. | 15 | .%`+i> _;_. |
16 | .i_,=:_. -<s. This program is distributed in the hope that | 16 | .i_,=:_. -<s. This program is distributed in the hope that |
17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
18 | : .. .:, . . . without even the implied warranty of | 18 | : .. .:, . . . without even the implied warranty of |
@@ -39,111 +39,106 @@ _;:, .> :=|. This program is free software; you can | |||
39 | * \brief smart pointer and reference counter | 39 | * \brief smart pointer and reference counter |
40 | * \author Rajko Albrecht | 40 | * \author Rajko Albrecht |
41 | * | 41 | * |
42 | */ | 42 | */ |
43 | 43 | ||
44 | namespace Opie { | 44 | namespace Opie { |
45 | namespace Core { | 45 | namespace Core { |
46 | 46 | ||
47 | //! simple reference counter class | 47 | //! simple reference counter class |
48 | class ORefCount { | 48 | class ORefCount { |
49 | protected: | 49 | protected: |
50 | //! reference count member | 50 | //! reference count member |
51 | long m_RefCount; | 51 | unsigned long m_RefCount; |
52 | public: | 52 | public: |
53 | //! first reference must be added after "new" via Pointer() | 53 | //! first reference must be added after "new" via Pointer() |
54 | ORefCount() : m_RefCount(0) | 54 | ORefCount(); |
55 | {} | 55 | virtual ~ORefCount(); |
56 | virtual ~ORefCount() {} | ||
57 | //! add a reference | 56 | //! add a reference |
58 | void Incr() { | 57 | void Incr(); |
59 | ++m_RefCount; | ||
60 | } | ||
61 | //! delete a reference | 58 | //! delete a reference |
62 | void Decr() { | 59 | void Decr(); |
63 | --m_RefCount; | ||
64 | } | ||
65 | //! is it referenced | 60 | //! is it referenced |
66 | bool Shared() { return (m_RefCount > 0); } | 61 | bool Shared(); |
67 | }; | 62 | }; |
68 | 63 | ||
69 | //! reference counting wrapper class | 64 | //! reference counting wrapper class |
70 | template<class T> class OSmartPointer { | 65 | template<class T> class OSmartPointer { |
71 | //! pointer to object | 66 | //! pointer to object |
72 | /*! | 67 | /*! |
73 | * this object must contain Incr(), Decr() and Shared() | 68 | * this object must contain Incr(), Decr() and Shared() |
74 | * methode as public members. The best way is, that it will be a child | 69 | * methode as public members. The best way is, that it will be a child |
75 | * class of RefCount | 70 | * class of RefCount |
76 | */ | 71 | */ |
77 | T *ptr; | 72 | T *ptr; |
78 | public: | 73 | public: |
79 | //! standart constructor | 74 | //! standart constructor |
80 | OSmartPointer() { ptr = NULL; } | 75 | OSmartPointer() { ptr = NULL; } |
81 | //! standart destructor | 76 | //! standart destructor |
82 | /*! | 77 | /*! |
83 | * release the reference, if it were the last reference, destroys | 78 | * release the reference, if it were the last reference, destroys |
84 | * ptr | 79 | * ptr |
85 | */ | 80 | */ |
86 | ~OSmartPointer() | 81 | ~OSmartPointer() |
87 | { | 82 | { |
88 | if (ptr){ | 83 | if (ptr){ |
89 | ptr->Decr(); | 84 | ptr->Decr(); |
90 | if (!ptr->Shared()) | 85 | if (!ptr->Shared()) |
91 | delete ptr; | 86 | delete ptr; |
92 | } | 87 | } |
93 | } | 88 | } |
94 | //! construction | 89 | //! construction |
95 | OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } | 90 | OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } |
96 | //! Pointer copy | 91 | //! Pointer copy |
97 | OSmartPointer(const OSmartPointer<T>& p) | 92 | OSmartPointer(const OSmartPointer<T>& p) |
98 | { if (ptr = p.ptr) ptr->Incr(); } | 93 | { if (ptr = p.ptr) ptr->Incr(); } |
99 | //! pointer copy by assignment | 94 | //! pointer copy by assignment |
100 | OSmartPointer<T>& operator= (const OSmartPointer<T>& p) | 95 | OSmartPointer<T>& operator= (const OSmartPointer<T>& p) |
101 | { | 96 | { |
102 | // already same: nothing to do | 97 | // already same: nothing to do |
103 | if (ptr == p.ptr) return *this; | 98 | if (ptr == p.ptr) return *this; |
104 | // decouple reference | 99 | // decouple reference |
105 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } | 100 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } |
106 | // establish new reference | 101 | // establish new reference |
107 | if (ptr = p.ptr) ptr->Incr(); | 102 | if (ptr = p.ptr) ptr->Incr(); |
108 | return *this; | 103 | return *this; |
109 | } | 104 | } |
110 | OSmartPointer<T>& operator= (T*p) | 105 | OSmartPointer<T>& operator= (T*p) |
111 | { | 106 | { |
112 | if (ptr==p)return *this; | 107 | if (ptr==p)return *this; |
113 | if (ptr) { | 108 | if (ptr) { |
114 | ptr->Decr(); | 109 | ptr->Decr(); |
115 | if (!ptr->Shared()) delete ptr; | 110 | if (!ptr->Shared()) delete ptr; |
116 | } | 111 | } |
117 | if (ptr=p) ptr->Incr(); | 112 | if (ptr=p) ptr->Incr(); |
118 | return *this; | 113 | return *this; |
119 | } | 114 | } |
120 | 115 | ||
121 | //! cast to conventional pointer | 116 | //! cast to conventional pointer |
122 | operator T* () const { return ptr; } | 117 | operator T* () const { return ptr; } |
123 | 118 | ||
124 | //! deref: fails for NULL pointer | 119 | //! deref: fails for NULL pointer |
125 | T& operator* () {return *ptr; } | 120 | T& operator* () {return *ptr; } |
126 | //! deref: fails for NULL pointer | 121 | //! deref: fails for NULL pointer |
127 | const T& operator* ()const {return *ptr; } | 122 | const T& operator* ()const {return *ptr; } |
128 | 123 | ||
129 | //! deref with method call | 124 | //! deref with method call |
130 | T* operator-> () {return ptr; } | 125 | T* operator-> () {return ptr; } |
131 | //! deref with const method call | 126 | //! deref with const method call |
132 | const T* operator-> ()const {return ptr; } | 127 | const T* operator-> ()const {return ptr; } |
133 | 128 | ||
134 | //! supports "if (pointer)" | 129 | //! supports "if (pointer)" |
135 | operator bool () const { return (ptr != NULL); } | 130 | operator bool () const { return (ptr != NULL); } |
136 | //! "if (pointer)" as non const | 131 | //! "if (pointer)" as non const |
137 | operator bool () { return ptr != NULL;} | 132 | operator bool () { return ptr != NULL;} |
138 | 133 | ||
139 | //! support if (!pointer)" | 134 | //! support if (!pointer)" |
140 | bool operator! () const { return (ptr == NULL); } | 135 | bool operator! () const { return (ptr == NULL); } |
141 | //! support if (!pointer)" as non const | 136 | //! support if (!pointer)" as non const |
142 | bool operator! () { return (ptr == NULL); } | 137 | bool operator! () { return (ptr == NULL); } |
143 | }; | 138 | }; |
144 | 139 | ||
145 | } | 140 | } |
146 | } | 141 | } |
147 | 142 | ||
148 | #endif | 143 | #endif |
149 | 144 | ||