util.h
Go to the documentation of this file.
1/*
2 *
3 * D-Bus++ - C++ bindings for D-Bus
4 *
5 * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24
25#ifndef __DBUSXX_UTIL_H
26#define __DBUSXX_UTIL_H
27
28#include <sstream>
29#include <iostream>
30#include <iomanip>
31#include <cassert>
32
33#include "api.h"
34#include "debug.h"
35
36namespace DBus
37{
38
39/*
40 * Very simple reference counting
41 */
42
44{
45public:
46
48 {
49 __ref = new int;
50 (*__ref) = 1;
51 }
52
53 RefCnt(const RefCnt &rc)
54 {
55 __ref = rc.__ref;
56 ref();
57 }
58
59 virtual ~RefCnt()
60 {
61 unref();
62 }
63
64 RefCnt &operator = (const RefCnt &ref)
65 {
66 ref.ref();
67 unref();
68 __ref = ref.__ref;
69 return *this;
70 }
71
72 bool noref() const
73 {
74 return (*__ref) == 0;
75 }
76
77 bool one() const
78 {
79 return (*__ref) == 1;
80 }
81
82private:
83
84 DXXAPILOCAL void ref() const
85 {
86 ++ (*__ref);
87 }
88 DXXAPILOCAL void unref() const
89 {
90 -- (*__ref);
91
92 if ((*__ref) < 0)
93 {
94 debug_log("%p: refcount dropped below zero!", __ref);
95 }
96
97 if (noref())
98 {
99 delete __ref;
100 }
101 }
102
103private:
104
105 int *__ref;
106};
107
108/*
109 * Reference counting pointers (emulate boost::shared_ptr)
110 */
111
112template <class T>
113class RefPtrI // RefPtr to incomplete type
114{
115public:
116
117 RefPtrI(T *ptr = 0);
118
119 ~RefPtrI();
120
122 {
123 if (this != &ref)
124 {
125 if (__cnt.one()) delete __ptr;
126
127 __ptr = ref.__ptr;
128 __cnt = ref.__cnt;
129 }
130 return *this;
131 }
132
133 T &operator *() const
134 {
135 return *__ptr;
136 }
137
138 T *operator ->() const
139 {
140 if (__cnt.noref()) return 0;
141
142 return __ptr;
143 }
144
145 T *get() const
146 {
147 if (__cnt.noref()) return 0;
148
149 return __ptr;
150 }
151
152private:
153
156};
157
158template <class T>
160{
161public:
162
163 RefPtr(T *ptr = 0)
164 : __ptr(ptr)
165 {}
166
168 {
169 if (__cnt.one()) delete __ptr;
170 }
171
173 {
174 if (this != &ref)
175 {
176 if (__cnt.one()) delete __ptr;
177
178 __ptr = ref.__ptr;
179 __cnt = ref.__cnt;
180 }
181 return *this;
182 }
183
184 T &operator *() const
185 {
186 return *__ptr;
187 }
188
189 T *operator ->() const
190 {
191 if (__cnt.noref()) return 0;
192
193 return __ptr;
194 }
195
196 T *get() const
197 {
198 if (__cnt.noref()) return 0;
199
200 return __ptr;
201 }
202
203private:
204
207};
208
209/*
210 * Typed callback template
211 */
212
213template <class R, class P>
215{
216public:
217
218 virtual R call(P param) const = 0;
219
221 {}
222};
223
224template <class R, class P>
225class Slot
226{
227public:
228
230 {
231 _cb = s;
232
233 return *this;
234 }
235
236 R operator()(P param) const
237 {
238 if (!empty())
239 {
240 return _cb->call(param);
241 }
242
243 // TODO: think about return type in this case
244 // this assert should help me to find the use case where it's needed...
245 //assert (false);
246 }
247
248 R call(P param) const
249 {
250 if (!empty())
251 {
252 return _cb->call(param);
253 }
254
255 // TODO: think about return type in this case
256 // this assert should help me to find the use case where it's needed...
257 //assert (false);
258 }
259
260 bool empty() const
261 {
262 return _cb.get() == 0;
263 }
264
265private:
266
268};
269
270template <class C, class R, class P>
271class Callback : public Callback_Base<R, P>
272{
273public:
274
275 typedef R(C::*M)(P);
276
277 Callback(C *c, M m)
278 : _c(c), _m(m)
279 {}
280
281 R call(P param) const
282 {
283 /*if (_c)*/ return (_c->*_m)(param);
284 }
285
286private:
287
288 C *_c;
290};
291
293template <typename T>
294std::string toString(const T &thing, int w = 0, int p = 0)
295{
296 std::ostringstream os;
297 os << std::setw(w) << std::setprecision(p) << thing;
298 return os.str();
299}
300
301} /* namespace DBus */
302
303#endif//__DBUSXX_UTIL_H
#define DXXAPILOCAL
Definition: api.h:32
#define DXXAPI
Definition: api.h:36
virtual R call(P param) const =0
virtual ~Callback_Base()
Definition: util.h:220
Callback(C *c, M m)
Definition: util.h:277
R call(P param) const
Definition: util.h:281
R(C::* M)(P)
Definition: util.h:275
DXXAPILOCAL void ref() const
Definition: util.h:84
bool one() const
Definition: util.h:77
RefCnt()
Definition: util.h:47
int * __ref
Definition: util.h:105
bool noref() const
Definition: util.h:72
virtual ~RefCnt()
Definition: util.h:59
DXXAPILOCAL void unref() const
Definition: util.h:88
RefCnt(const RefCnt &rc)
Definition: util.h:53
RefPtrI & operator=(const RefPtrI &ref)
Definition: util.h:121
T & operator*() const
Definition: util.h:133
RefPtrI(T *ptr=0)
Definition: refptr_impl.h:35
RefCnt __cnt
Definition: util.h:155
T * operator->() const
Definition: util.h:138
T * get() const
Definition: util.h:145
T * __ptr
Definition: util.h:154
T * operator->() const
Definition: util.h:189
RefCnt __cnt
Definition: util.h:206
RefPtr & operator=(const RefPtr &ref)
Definition: util.h:172
RefPtr(T *ptr=0)
Definition: util.h:163
T * __ptr
Definition: util.h:205
T * get() const
Definition: util.h:196
T & operator*() const
Definition: util.h:184
~RefPtr()
Definition: util.h:167
Slot & operator=(Callback_Base< R, P > *s)
Definition: util.h:229
R operator()(P param) const
Definition: util.h:236
R call(P param) const
Definition: util.h:248
RefPtr< Callback_Base< R, P > > _cb
Definition: util.h:267
bool empty() const
Definition: util.h:260
std::string toString(const T &thing, int w=0, int p=0)
create std::string from any number
Definition: util.h:294
DXXAPI LogFunction debug_log
Definition: debug.cpp:55