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 
44 {
45 public:
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 
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 
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;
156 };
157 
158 template <class T>
159 class RefPtr
160 {
161 public:
162 
163  RefPtr(T *ptr = 0)
164  : __ptr(ptr)
165  {}
166 
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;
207 };
208 
209 /*
210  * Typed callback template
211  */
212 
213 template <class R, class P>
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 
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  return _cb->call(param);
247  }
248 
249  R call(P param) const
250  {
251  if (!empty())
252  {
253  return _cb->call(param);
254  }
255 
256  // TODO: think about return type in this case
257  // this assert should help me to find the use case where it's needed...
258  //assert (false);
259  return _cb->call(param);
260 
261  }
262 
263  bool empty() const
264  {
265  return _cb.get() == 0;
266  }
267 
268 private:
269 
271 };
272 
273 template <class C, class R, class P>
274 class Callback : public Callback_Base<R, P>
275 {
276 public:
277 
278  typedef R(C::*M)(P);
279 
280  Callback(C *c, M m)
281  : _c(c), _m(m)
282  {}
283 
284  R call(P param) const
285  {
286  /*if (_c)*/ return (_c->*_m)(param);
287  }
288 
289 private:
290 
291  C *_c;
292  M _m;
293 };
294 
296 template <typename T>
297 std::string toString(const T &thing, int w = 0, int p = 0)
298 {
299  std::ostringstream os;
300  os << std::setw(w) << std::setprecision(p) << thing;
301  return os.str();
302 }
303 
304 } /* namespace DBus */
305 
306 #endif//__DBUSXX_UTIL_H
RefCnt()
Definition: util.h:47
RefPtr(T *ptr=0)
Definition: util.h:163
bool one() const
Definition: util.h:77
DXXAPILOCAL void ref() const
Definition: util.h:84
T * get() const
Definition: util.h:196
R call(P param) const
Definition: util.h:249
T & operator*() const
Definition: util.h:133
RefPtr & operator=(const RefPtr &ref)
Definition: util.h:172
DXXAPILOCAL void unref() const
Definition: util.h:88
Slot & operator=(Callback_Base< R, P > *s)
Definition: util.h:229
int * __ref
Definition: util.h:105
T * operator->() const
Definition: util.h:189
~RefPtr()
Definition: util.h:167
T * __ptr
Definition: util.h:205
RefPtrI & operator=(const RefPtrI &ref)
Definition: util.h:121
#define DXXAPI
Definition: api.h:36
Callback(C *c, M m)
Definition: util.h:280
#define DXXAPILOCAL
Definition: api.h:32
R call(P param) const
Definition: util.h:284
RefCnt(const RefCnt &rc)
Definition: util.h:53
T & operator*() const
Definition: util.h:184
T * operator->() const
Definition: util.h:138
RefPtrI(T *ptr=0)
Definition: refptr_impl.h:35
virtual ~Callback_Base()
Definition: util.h:220
T * __ptr
Definition: util.h:154
T * get() const
Definition: util.h:145
RefPtr< Callback_Base< R, P > > _cb
Definition: util.h:270
virtual R call(P param) const =0
bool noref() const
Definition: util.h:72
DXXAPI LogFunction debug_log
Definition: debug.cpp:55
virtual ~RefCnt()
Definition: util.h:59
bool empty() const
Definition: util.h:263
R operator()(P param) const
Definition: util.h:236
std::string toString(const T &thing, int w=0, int p=0)
create std::string from any number
Definition: util.h:297
RefCnt __cnt
Definition: util.h:206
RefCnt __cnt
Definition: util.h:155
R(C::* M)(P)
Definition: util.h:278