-rw-r--r-- | libopie2/opiecore/osmartpointer.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/libopie2/opiecore/osmartpointer.h b/libopie2/opiecore/osmartpointer.h index 8f9da7f..c7dc9d9 100644 --- a/libopie2/opiecore/osmartpointer.h +++ b/libopie2/opiecore/osmartpointer.h | |||
@@ -1,130 +1,132 @@ | |||
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 |
19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
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 _OSmartPointer_h | 32 | #ifndef _OSmartPointer_h |
33 | #define _OSmartPointer_h | 33 | #define _OSmartPointer_h |
34 | 34 | ||
35 | #include <stddef.h> | ||
36 | |||
35 | /*! | 37 | /*! |
36 | * \file OSmartPointer.h | 38 | * \file OSmartPointer.h |
37 | * \brief smart pointer and reference counter | 39 | * \brief smart pointer and reference counter |
38 | * \author Rajko Albrecht | 40 | * \author Rajko Albrecht |
39 | * | 41 | * |
40 | */ | 42 | */ |
41 | 43 | ||
42 | namespace Opie { | 44 | namespace Opie { |
43 | namespace Core { | 45 | namespace Core { |
44 | 46 | ||
45 | //! simple reference counter class | 47 | //! simple reference counter class |
46 | class ORefCount { | 48 | class ORefCount { |
47 | protected: | 49 | protected: |
48 | //! reference count member | 50 | //! reference count member |
49 | long m_RefCount; | 51 | long m_RefCount; |
50 | public: | 52 | public: |
51 | //! first reference must be added after "new" via Pointer() | 53 | //! first reference must be added after "new" via Pointer() |
52 | ORefCount() : m_RefCount(0) | 54 | ORefCount() : m_RefCount(0) |
53 | {} | 55 | {} |
54 | virtual ~ORefCount() {} | 56 | virtual ~ORefCount() {} |
55 | //! add a reference | 57 | //! add a reference |
56 | void Incr() { | 58 | void Incr() { |
57 | ++m_RefCount; | 59 | ++m_RefCount; |
58 | } | 60 | } |
59 | //! delete a reference | 61 | //! delete a reference |
60 | void Decr() { | 62 | void Decr() { |
61 | --m_RefCount; | 63 | --m_RefCount; |
62 | } | 64 | } |
63 | //! is it referenced | 65 | //! is it referenced |
64 | bool Shared() { return (m_RefCount > 0); } | 66 | bool Shared() { return (m_RefCount > 0); } |
65 | }; | 67 | }; |
66 | 68 | ||
67 | //! reference counting wrapper class | 69 | //! reference counting wrapper class |
68 | template<class T> class OSmartPointer { | 70 | template<class T> class OSmartPointer { |
69 | //! pointer to object | 71 | //! pointer to object |
70 | /*! | 72 | /*! |
71 | * this object must contain Incr(), Decr() and Shared() | 73 | * this object must contain Incr(), Decr() and Shared() |
72 | * methode as public members. The best way is, that it will be a child | 74 | * methode as public members. The best way is, that it will be a child |
73 | * class of RefCount | 75 | * class of RefCount |
74 | */ | 76 | */ |
75 | T *ptr; | 77 | T *ptr; |
76 | public: | 78 | public: |
77 | //! standart constructor | 79 | //! standart constructor |
78 | OSmartPointer() { ptr = NULL; } | 80 | OSmartPointer() { ptr = NULL; } |
79 | //! standart destructor | 81 | //! standart destructor |
80 | /*! | 82 | /*! |
81 | * release the reference, if it were the last reference, destroys | 83 | * release the reference, if it were the last reference, destroys |
82 | * ptr | 84 | * ptr |
83 | */ | 85 | */ |
84 | ~OSmartPointer() | 86 | ~OSmartPointer() |
85 | { | 87 | { |
86 | if (ptr){ | 88 | if (ptr){ |
87 | ptr->Decr(); | 89 | ptr->Decr(); |
88 | if (!ptr->Shared()) | 90 | if (!ptr->Shared()) |
89 | delete ptr; | 91 | delete ptr; |
90 | } | 92 | } |
91 | } | 93 | } |
92 | //! construction | 94 | //! construction |
93 | OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } | 95 | OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } |
94 | //! Pointer copy | 96 | //! Pointer copy |
95 | OSmartPointer(const OSmartPointer<T>& p) | 97 | OSmartPointer(const OSmartPointer<T>& p) |
96 | { if (ptr = p.ptr) ptr->Incr(); } | 98 | { if (ptr = p.ptr) ptr->Incr(); } |
97 | //! pointer copy by assignment | 99 | //! pointer copy by assignment |
98 | OSmartPointer<T>& operator= (const OSmartPointer<T>& p) | 100 | OSmartPointer<T>& operator= (const OSmartPointer<T>& p) |
99 | { | 101 | { |
100 | // already same: nothing to do | 102 | // already same: nothing to do |
101 | if (ptr == p.ptr) return *this; | 103 | if (ptr == p.ptr) return *this; |
102 | // decouple reference | 104 | // decouple reference |
103 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } | 105 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } |
104 | // establish new reference | 106 | // establish new reference |
105 | if (ptr = p.ptr) ptr->Incr(); | 107 | if (ptr = p.ptr) ptr->Incr(); |
106 | return *this; | 108 | return *this; |
107 | } | 109 | } |
108 | OSmartPointer<T>& operator= (T*p) | 110 | OSmartPointer<T>& operator= (T*p) |
109 | { | 111 | { |
110 | if (ptr==p)return *this; | 112 | if (ptr==p)return *this; |
111 | if (ptr) { | 113 | if (ptr) { |
112 | ptr->Decr(); | 114 | ptr->Decr(); |
113 | if (!ptr->Shared()) delete ptr; | 115 | if (!ptr->Shared()) delete ptr; |
114 | } | 116 | } |
115 | if (ptr=p) ptr->Incr(); | 117 | if (ptr=p) ptr->Incr(); |
116 | return *this; | 118 | return *this; |
117 | } | 119 | } |
118 | 120 | ||
119 | //! cast to conventional pointer | 121 | //! cast to conventional pointer |
120 | operator T* () const { return ptr; } | 122 | operator T* () const { return ptr; } |
121 | 123 | ||
122 | //! deref: fails for NULL pointer | 124 | //! deref: fails for NULL pointer |
123 | T& operator* () {return *ptr; } | 125 | T& operator* () {return *ptr; } |
124 | //! deref: fails for NULL pointer | 126 | //! deref: fails for NULL pointer |
125 | const T& operator* ()const {return *ptr; } | 127 | const T& operator* ()const {return *ptr; } |
126 | 128 | ||
127 | //! deref with method call | 129 | //! deref with method call |
128 | T* operator-> () {return ptr; } | 130 | T* operator-> () {return ptr; } |
129 | //! deref with const method call | 131 | //! deref with const method call |
130 | const T* operator-> ()const {return ptr; } | 132 | const T* operator-> ()const {return ptr; } |