summaryrefslogtreecommitdiff
path: root/libopie2/opiecore/osharedpointer.h
Unidiff
Diffstat (limited to 'libopie2/opiecore/osharedpointer.h') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiecore/osharedpointer.h136
1 files changed, 136 insertions, 0 deletions
diff --git a/libopie2/opiecore/osharedpointer.h b/libopie2/opiecore/osharedpointer.h
new file mode 100644
index 0000000..f19d836
--- a/dev/null
+++ b/libopie2/opiecore/osharedpointer.h
@@ -0,0 +1,136 @@
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) 2004 Holger Hans Peter Freyther <freyther@handhelds.org>
6 Copyright (C) The Opie Team <opie-devel@handhelds.org>
7 =.
8 .=l.
9 .>+-=
10_;:, .> :=|. This program is free software; you can
11.> <`_, > . <= redistribute it and/or modify it under
12:`=1 )Y*s>-.-- : the terms of the GNU Library General Public
13.="- .-=="i, .._ License as published by the Free Software
14- . .-<_> .<> Foundation; either version 2 of the License,
15 ._= =} : or (at your option) any later version.
16 .%`+i> _;_.
17 .i_,=:_. -<s. This program is distributed in the hope that
18 + . -:. = it will be useful, but WITHOUT ANY WARRANTY;
19 : .. .:, . . . without even the implied warranty of
20 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A
21 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
22..}^=.= = ; Library General Public License for more
23++= -. .` .: details.
24: = ...= . :.=-
25-. .:....=;==+<; You should have received a copy of the GNU
26 -_. . . )=. = Library General Public License along with
27 -- :-=` this library; see the file COPYING.LIB.
28 If not, write to the Free Software Foundation,
29 Inc., 59 Temple Place - Suite 330,
30 Boston, MA 02111-1307, USA.
31*/
32
33
34#ifndef O_SHARED_POINTER_H
35#define O_SHARED_POINTER_H
36
37#include <opie2/osmartpointer.h>
38
39/*!
40 * \file osharedpointer.h
41 * \brief Adapter with Refcounting around any data type
42 * \author Rajko Albrecht and Holger Hans Peter Freyther
43 */
44
45namespace Opie {
46namespace Core {
47template<class T>
48class OSharedPointerData : public ORefCount {
49public:
50 OSharedPointerData( T* dt ) {
51 data = dt;
52 }
53
54 ~OSharedPointerData() {
55 delete data;
56 }
57
58 T *data;
59};
60
61
62/**
63 * \brief A small Adapter around any Pointer
64 *
65 *
66 *
67 */
68template<class T>
69class OSharedPointer {
70 typedef OSharedPointerData<T> Data;
71 Data* data;
72
73 void unref() {
74 if (data) { data->Decr(); if (!data->Shared()) delete data; }
75 data = 0;
76 }
77
78public:
79 OSharedPointer () { data = 0;}
80
81 OSharedPointer( T* t ) {
82 data = new Data( t );
83 data->Incr();
84 }
85
86 OSharedPointer( const OSharedPointer<T>& p ) {
87 if ( data = p.data ) data->Incr();
88 }
89
90 ~OSharedPointer() {
91 unref();
92 }
93
94 OSharedPointer<T> &operator=( const OSharedPointer<T>& p ) {
95 // already same: nothing to do
96 if (data == p.data) return *this;
97 // decouple reference
98 unref();
99 // establish new reference
100 if (data = p.data) data->Incr();
101 return *this;
102 }
103
104 OSharedPointer<T> &operator=( T *p ) {
105 unref();
106 data = new Data( p );
107 data->Incr();
108
109 return *this;
110 }
111
112 operator T* () const { return data->data; }
113 T& operator*() { return *data->data; }
114 const T& operator*()const { return data->data; }
115
116 //! deref with method call
117 T* operator-> () {return data->data; }
118 //! deref with const method call
119 const T* operator-> ()const {return data->data; }
120
121
122 //! supports "if (pointer)"
123 operator bool () const { return (data != 0 && data->data != 0); }
124 //! "if (pointer)" as non const
125 operator bool () { return ( data != 0 && data->data != NULL );}
126
127 //! support if (!pointer)"
128 bool operator! () const { return (data == 0 || data->data == 0); }
129 //! support if (!pointer)" as non const
130 bool operator! () { return (data == 0 || data->data == 0); }
131};
132
133}
134}
135
136#endif