Free memory with dbus_free instead of free. Account for NULL signatures.
[dbus-cxx-async.git] / include / dbus-c++ / dispatcher.h
blobcf0880781aceaf373dc6d2c9694a9ba46f1a80e9
1 /*
3 * D-Bus++ - C++ bindings for D-Bus
5 * Copyright (C) 2005-2009 Paolo Durante <shackan@gmail.com>
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.
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.
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
25 #ifndef __DBUSXX_DISPATCHER_H
26 #define __DBUSXX_DISPATCHER_H
28 #include "api.h"
29 #include "connection.h"
30 #include "eventloop.h"
32 namespace DBus {
34 class DBUSXXAPI Timeout
36 public:
38 class Internal;
40 Timeout(Internal *i);
42 virtual ~Timeout(){}
44 int interval() const;
46 bool enabled() const;
48 bool handle();
50 virtual void toggle() = 0;
52 private:
54 DBUSXXAPILOCAL Timeout(const Timeout &);
56 private:
58 Internal *_int;
61 class DBUSXXAPI Watch
63 public:
65 class Internal;
67 Watch(Internal *i);
69 virtual ~Watch(){}
71 int descriptor() const;
73 int flags() const;
75 bool enabled() const;
77 bool handle(int flags);
79 virtual void toggle() = 0;
81 private:
83 DBUSXXAPILOCAL Watch(const Watch &);
85 private:
87 Internal *_int;
90 class DBUSXXAPI Dispatcher : public Synchronized
92 public:
93 Dispatcher();
94 virtual ~Dispatcher();
96 void queue_connection(Connection::Private *);
98 void dispatch_pending();
99 bool has_something_to_dispatch();
101 virtual void enter() = 0;
103 virtual void leave() = 0;
105 virtual Timeout *add_timeout(Timeout::Internal *) = 0;
107 virtual void rem_timeout(Timeout *) = 0;
109 virtual Watch *add_watch(Watch::Internal *) = 0;
111 virtual void rem_watch(Watch *) = 0;
113 struct Private;
115 private:
117 Connection::PrivatePList _pending_queue;
120 /* classes for multithreading support
123 class DBUSXXAPI Mutex
125 public:
127 virtual ~Mutex() {}
129 virtual void lock() = 0;
131 virtual void unlock() = 0;
133 struct Internal;
135 protected:
137 Internal *_int;
140 class DBUSXXAPI CondVar
142 public:
144 virtual ~CondVar() {}
146 virtual void wait(Mutex *) = 0;
148 virtual bool wait_timeout(Mutex *, int timeout) = 0;
150 virtual void wake_one() = 0;
152 virtual void wake_all() = 0;
154 struct Internal;
156 protected:
158 Internal *_int;
161 typedef Mutex *(*MutexNewFn)();
162 typedef void (*MutexUnlockFn)(Mutex *mx);
164 #ifndef DBUS_HAS_RECURSIVE_MUTEX
165 typedef bool (*MutexFreeFn)(Mutex *mx);
166 typedef bool (*MutexLockFn)(Mutex *mx);
167 #else
168 typedef void (*MutexFreeFn)(Mutex *mx);
169 typedef void (*MutexLockFn)(Mutex *mx);
170 #endif//DBUS_HAS_RECURSIVE_MUTEX
172 typedef CondVar *(*CondVarNewFn)();
173 typedef void (*CondVarFreeFn)(CondVar *cv);
174 typedef void (*CondVarWaitFn)(CondVar *cv, Mutex *mx);
175 typedef bool (*CondVarWaitTimeoutFn)(CondVar *cv, Mutex *mx, int timeout);
176 typedef void (*CondVarWakeOneFn)(CondVar *cv);
177 typedef void (*CondVarWakeAllFn)(CondVar *cv);
179 void DBUSXXAPI _init_threading();
181 void DBUSXXAPI _init_threading(
182 MutexNewFn, MutexFreeFn, MutexLockFn, MutexUnlockFn,
183 CondVarNewFn, CondVarFreeFn, CondVarWaitFn, CondVarWaitTimeoutFn, CondVarWakeOneFn, CondVarWakeAllFn
186 template<class Mx, class Cv>
187 struct Threading
189 static void init()
191 _init_threading(
192 mutex_new, mutex_free, mutex_lock, mutex_unlock,
193 condvar_new, condvar_free, condvar_wait, condvar_wait_timeout, condvar_wake_one, condvar_wake_all
197 static Mutex *mutex_new()
199 return new Mx;
202 static void mutex_free(Mutex *mx)
204 delete mx;
207 static void mutex_lock(Mutex *mx)
209 mx->lock();
212 static void mutex_unlock(Mutex *mx)
214 mx->unlock();
217 static CondVar *condvar_new()
219 return new Cv;
222 static void condvar_free(CondVar *cv)
224 delete cv;
227 static void condvar_wait(CondVar *cv, Mutex *mx)
229 cv->wait(mx);
232 static bool condvar_wait_timeout(CondVar *cv, Mutex *mx, int timeout)
234 return cv->wait_timeout(mx, timeout);
237 static void condvar_wake_one(CondVar *cv)
239 cv->wake_one();
242 static void condvar_wake_all(CondVar *cv)
244 cv->wake_all();
248 } /* namespace DBus */
250 #endif//__DBUSXX_DISPATCHER_H