# DBus-C++

## Desktop Communication Bus System

libdbus-c-- Documentation 0.9.0

- Files
- Data Structures
- Globals
- Main Page

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
36 namespace DBus
37{
38
39/*
40 * Very simple reference counting
41 */
42
43 class DXXAPI RefCnt
44{
45 public:
46
47 RefCnt()
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
82 private:
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
103 private:
104
105 int *__ref;
106};
107
108/*
109 * Reference counting pointers (emulate boost::shared_ptr)
110 */
111
112 template <class T>
113 class RefPtrI // RefPtr to incomplete type
114{
115 public:
116
117 RefPtrI(T *ptr = 0);
118
119 ~RefPtrI();
120
121 RefPtrI &operator = (const RefPtrI &ref)
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
152 private:
153
154 T *__ptr;
155 RefCnt __cnt;
156};
157
158 template <class T>
159 class RefPtr
160{
161 public:
162
163 RefPtr(T *ptr = 0)
164 : __ptr(ptr)
165 {}
166
167 ~RefPtr()
168 {
169 if (__cnt.one()) delete __ptr;
170 }
171
172 RefPtr &operator = (const RefPtr &ref)
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
203 private:
204
205 T *__ptr;
206 RefCnt __cnt;
207};
208
209/*
210 * Typed callback template
211 */
212
213 template <class R, class P>
214 class Callback_Base
215{
216 public:
217
218 virtual R call(P param) const = 0;
219
220 virtual ~Callback_Base()
221 {}
222};
223
224 template <class R, class P>
225 class Slot
226{
227 public:
228
229 Slot &operator = (Callback_Base<R, P>* s)
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
265 private:
266
267 RefPtr< Callback_Base<R, P> > _cb;
268};
269
270 template <class C, class R, class P>
271 class Callback : public Callback_Base<R, P>
272{
273 public:
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
286 private:
287
288 C *_c;
289 M _m;
290};
291
293 template <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
api.h
DXXAPILOCAL
#define DXXAPILOCAL
Definition api.h:32
DXXAPI
#define DXXAPI
Definition api.h:36
DBus::Callback_Base
Definition util.h:215
DBus::Callback_Base::call
virtual R call(P param) const =0
DBus::Callback_Base::~Callback_Base
virtual ~Callback_Base()
Definition util.h:220
DBus::Callback::_m
M _m
Definition util.h:289
DBus::Callback::Callback
Callback(C *c, M m)
Definition util.h:277
DBus::Callback::_c
C * _c
Definition util.h:288
DBus::Callback::call
R call(P param) const
Definition util.h:281
DBus::Callback::M
R(C::* M)(P)
Definition util.h:275
DBus::RefCnt
Definition util.h:44
DBus::RefCnt::ref
DXXAPILOCAL void ref() const
Definition util.h:84
DBus::RefCnt::one
bool one() const
Definition util.h:77
DBus::RefCnt::RefCnt
RefCnt()
Definition util.h:47
DBus::RefCnt::__ref
int * __ref
Definition util.h:105
DBus::RefCnt::noref
bool noref() const
Definition util.h:72
DBus::RefCnt::~RefCnt
virtual ~RefCnt()
Definition util.h:59
DBus::RefCnt::unref
DXXAPILOCAL void unref() const
Definition util.h:88
DBus::RefCnt::RefCnt
RefCnt(const RefCnt &rc)
Definition util.h:53
DBus::RefPtrI::operator=
RefPtrI & operator=(const RefPtrI &ref)
Definition util.h:121
DBus::RefPtrI::operator*
T & operator*() const
Definition util.h:133
DBus::RefPtrI::RefPtrI
RefPtrI(T *ptr=0)
Definition refptr_impl.h:35
DBus::RefPtrI::~RefPtrI
~RefPtrI()
Definition refptr_impl.h:40
DBus::RefPtrI::__cnt
RefCnt __cnt
Definition util.h:155
DBus::RefPtrI::operator->
T * operator->() const
Definition util.h:138
DBus::RefPtrI::get
T * get() const
Definition util.h:145
DBus::RefPtrI::__ptr
T * __ptr
Definition util.h:154
DBus::RefPtr
Definition util.h:160
DBus::RefPtr::operator->
T * operator->() const
Definition util.h:189
DBus::RefPtr::__cnt
RefCnt __cnt
Definition util.h:206
DBus::RefPtr::operator=
RefPtr & operator=(const RefPtr &ref)
Definition util.h:172
DBus::RefPtr::RefPtr
RefPtr(T *ptr=0)
Definition util.h:163
DBus::RefPtr::__ptr
T * __ptr
Definition util.h:205
DBus::RefPtr::get
T * get() const
Definition util.h:196
DBus::RefPtr::operator*
T & operator*() const
Definition util.h:184
DBus::RefPtr::~RefPtr
~RefPtr()
Definition util.h:167
DBus::Slot
Definition util.h:226
DBus::Slot::operator=
Slot & operator=(Callback_Base< R, P > *s)
Definition util.h:229
DBus::Slot::operator()
R operator()(P param) const
Definition util.h:236
DBus::Slot::call
R call(P param) const
Definition util.h:248
DBus::Slot< bool, const Message & >::_cb
RefPtr< Callback_Base< bool, const Message & > > _cb
Definition util.h:267
DBus::Slot::empty
bool empty() const
Definition util.h:260
debug.h
DBus
Definition connection_p.h:42
DBus::toString
std::string toString(const T &thing, int w=0, int p=0)
create std::string from any number
Definition util.h:294
DBus::debug_log
DXXAPI LogFunction debug_log
Definition debug.cpp:55