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