-rw-r--r-- | libopie2/opiecore/opiecore.pro | 1 | ||||
-rw-r--r-- | libopie2/opiecore/osmart_pointer.h | 144 |
2 files changed, 145 insertions, 0 deletions
diff --git a/libopie2/opiecore/opiecore.pro b/libopie2/opiecore/opiecore.pro index b4186b1..a4080a8 100644 --- a/libopie2/opiecore/opiecore.pro +++ b/libopie2/opiecore/opiecore.pro | |||
@@ -7,8 +7,9 @@ HEADERS = oapplication.h \ | |||
7 | oglobal.h \ | 7 | oglobal.h \ |
8 | oglobalsettings.h \ | 8 | oglobalsettings.h \ |
9 | oprocess.h \ | 9 | oprocess.h \ |
10 | oprocctrl.h \ | 10 | oprocctrl.h \ |
11 | osmart_pointer.h \ | ||
11 | ostorageinfo.h \ | 12 | ostorageinfo.h \ |
12 | xmltree.h | 13 | xmltree.h |
13 | 14 | ||
14 | SOURCES = oapplication.cpp \ | 15 | SOURCES = oapplication.cpp \ |
diff --git a/libopie2/opiecore/osmart_pointer.h b/libopie2/opiecore/osmart_pointer.h new file mode 100644 index 0000000..a362a60 --- a/dev/null +++ b/libopie2/opiecore/osmart_pointer.h | |||
@@ -0,0 +1,144 @@ | |||
1 | // -*- Mode: C++; -*- | ||
2 | /* | ||
3 | This file is part of the Opie Project | ||
4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> | ||
5 | Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
6 | =. | ||
7 | .=l. | ||
8 | .>+-= | ||
9 | _;:, .> :=|. This program is free software; you can | ||
10 | .> <`_, > . <= redistribute it and/or modify it under | ||
11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
12 | .="- .-=="i, .._ License as published by the Free Software | ||
13 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
14 | ._= =} : or (at your option) any later version. | ||
15 | .%`+i> _;_. | ||
16 | .i_,=:_. -<s. This program is distributed in the hope that | ||
17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
18 | : .. .:, . . . without even the implied warranty of | ||
19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
20 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
21 | ..}^=.= = ; Library General Public License for more | ||
22 | ++= -. .` .: details. | ||
23 | : = ...= . :.=- | ||
24 | -. .:....=;==+<; You should have received a copy of the GNU | ||
25 | -_. . . )=. = Library General Public License along with | ||
26 | -- :-=` this library; see the file COPYING.LIB. | ||
27 | If not, write to the Free Software Foundation, | ||
28 | Inc., 59 Temple Place - Suite 330, | ||
29 | Boston, MA 02111-1307, USA. | ||
30 | */ | ||
31 | |||
32 | #ifndef _osmart_pointer_h | ||
33 | #define _osmart_pointer_h | ||
34 | |||
35 | /*! | ||
36 | * \file osmart_pointer.h | ||
37 | * \brief smart pointer and reference counter | ||
38 | * \author Rajko Albrecht | ||
39 | * | ||
40 | */ | ||
41 | |||
42 | namespace Opie { | ||
43 | |||
44 | //! simple reference counter class | ||
45 | class oref_count { | ||
46 | protected: | ||
47 | //! reference count member | ||
48 | long m_RefCount; | ||
49 | public: | ||
50 | //! first reference must be added after "new" via Pointer() | ||
51 | oref_count() : m_RefCount(0) | ||
52 | {} | ||
53 | virtual ~oref_count() {} | ||
54 | //! add a reference | ||
55 | void Incr() { | ||
56 | ++m_RefCount; | ||
57 | } | ||
58 | //! delete a reference | ||
59 | void Decr() { | ||
60 | --m_RefCount; | ||
61 | } | ||
62 | //! is it referenced | ||
63 | bool Shared() { return (m_RefCount > 0); } | ||
64 | }; | ||
65 | |||
66 | //! reference counting wrapper class | ||
67 | template<class T> class osmart_pointer { | ||
68 | //! pointer to object | ||
69 | /*! | ||
70 | * this object must contain Incr(), Decr() and Shared() | ||
71 | * methode as public members. The best way is, that it will be a child | ||
72 | * class of RefCount | ||
73 | */ | ||
74 | T *ptr; | ||
75 | public: | ||
76 | //! standart constructor | ||
77 | osmart_pointer() { ptr = NULL; } | ||
78 | //! standart destructor | ||
79 | /*! | ||
80 | * release the reference, if it were the last reference, destroys | ||
81 | * ptr | ||
82 | */ | ||
83 | ~osmart_pointer() | ||
84 | { | ||
85 | if (ptr){ | ||
86 | ptr->Decr(); | ||
87 | if (!ptr->Shared()) | ||
88 | delete ptr; | ||
89 | } | ||
90 | } | ||
91 | //! construction | ||
92 | osmart_pointer(T* t) { if (ptr = t) ptr->Incr(); } | ||
93 | //! Pointer copy | ||
94 | osmart_pointer(const smart_pointer<T>& p) | ||
95 | { if (ptr = p.ptr) ptr->Incr(); } | ||
96 | //! pointer copy by assignment | ||
97 | osmart_pointer<T>& operator= (const smart_pointer<T>& p) | ||
98 | { | ||
99 | // already same: nothing to do | ||
100 | if (ptr == p.ptr) return *this; | ||
101 | // decouple reference | ||
102 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } | ||
103 | // establish new reference | ||
104 | if (ptr = p.ptr) ptr->Incr(); | ||
105 | return *this; | ||
106 | } | ||
107 | osmart_pointer<T>& operator= (T*p) | ||
108 | { | ||
109 | if (ptr==p)return *this; | ||
110 | if (ptr) { | ||
111 | ptr->Decr(); | ||
112 | if (!ptr->Shared()) delete ptr; | ||
113 | } | ||
114 | if (ptr=p) ptr->Incr(); | ||
115 | return *this; | ||
116 | } | ||
117 | |||
118 | //! cast to conventional pointer | ||
119 | operator T* () const { return ptr; } | ||
120 | |||
121 | //! deref: fails for NULL pointer | ||
122 | T& operator* () {return *ptr; } | ||
123 | //! deref: fails for NULL pointer | ||
124 | const T& operator* ()const {return *ptr; } | ||
125 | |||
126 | //! deref with method call | ||
127 | T* operator-> () {return ptr; } | ||
128 | //! deref with const method call | ||
129 | const T* operator-> ()const {return ptr; } | ||
130 | |||
131 | //! supports "if (pointer)" | ||
132 | operator bool () const { return (ptr != NULL); } | ||
133 | //! "if (pointer)" as non const | ||
134 | operator bool () { return ptr != NULL;} | ||
135 | |||
136 | //! support if (!pointer)" | ||
137 | bool operator! () const { return (ptr == NULL); } | ||
138 | //! support if (!pointer)" as non const | ||
139 | bool operator! () { return (ptr == NULL); } | ||
140 | }; | ||
141 | |||
142 | } | ||
143 | |||
144 | #endif | ||