Free memory with dbus_free instead of free. Account for NULL signatures.
[dbus-cxx-async.git] / include / dbus-c++ / interface.h
blob1484e2aa790c9a52956049b29431e3886cbc7e2f
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_INTERFACE_H
26 #define __DBUSXX_INTERFACE_H
28 #include <string>
29 #include <map>
30 #include "api.h"
31 #include "util.h"
32 #include "types.h"
34 #include "message.h"
35 #include "pendingcall.h"
37 namespace DBus {
39 //todo: this should belong to to properties.h
40 struct DBUSXXAPI PropertyData
42 bool read;
43 bool write;
44 std::string sig;
45 Variant value;
48 typedef std::map<std::string, PropertyData> PropertyTable;
50 class IntrospectedInterface;
52 class ObjectAdaptor;
53 class InterfaceAdaptor;
54 class SignalMessage;
56 typedef std::map<std::string, InterfaceAdaptor *> InterfaceAdaptorTable;
58 class DBUSXXAPI AdaptorBase
60 public:
62 virtual ObjectAdaptor &object() = 0 ;
64 protected:
66 InterfaceAdaptor *find_interface(const std::string &name);
67 InterfaceAdaptor *find_interface_with_method(const std::string &method);
69 virtual ~AdaptorBase()
72 virtual void _emit_signal(SignalMessage &) = 0;
74 InterfaceAdaptorTable _interfaces;
80 class ObjectProxy;
81 class InterfaceProxy;
82 class CallMessage;
84 typedef std::map<std::string, InterfaceProxy *> InterfaceProxyTable;
86 class DBUSXXAPI ProxyBase
88 public:
90 virtual ObjectProxy &object() = 0 ;
92 protected:
94 InterfaceProxy *find_interface(const std::string &name);
96 virtual ~ProxyBase()
99 virtual Message _invoke_method(CallMessage &, int timeout) = 0;
101 virtual PendingCall _invoke_method_async(CallMessage &, const PendingCallSlot &, void* p, int timeout) = 0;
103 InterfaceProxyTable _interfaces;
106 class DBUSXXAPI Interface
108 public:
110 Interface(const std::string &name);
112 virtual ~Interface();
114 const std::string &name() const { return _name; }
116 private:
118 std::string _name;
124 typedef std::map< std::string, Slot<Message, const CallMessage &> > MethodTable;
125 typedef std::map< std::string, Slot<void, const CallMessage &> > AsyncTable;
127 class DBUSXXAPI InterfaceAdaptor : public Interface, public virtual AdaptorBase
129 public:
131 InterfaceAdaptor(const std::string &name);
133 void dispatch_method(const CallMessage &, Message **);
135 void emit_signal(const SignalMessage &);
137 Variant *get_property(const std::string &name);
139 void set_property(const std::string &name, Variant &value);
141 bool has_method(const std::string &name) const;
143 virtual IntrospectedInterface *const introspect() const { return NULL; }
145 protected:
147 MethodTable _methods;
148 AsyncTable _asyncs;
149 PropertyTable _properties;
155 typedef std::map< std::string, Slot<void, const SignalMessage &> > SignalTable;
157 class DBUSXXAPI InterfaceProxy : public Interface, public virtual ProxyBase
159 public:
161 InterfaceProxy(const std::string &name);
163 Message invoke_method(const CallMessage &, int timeout = -1);
165 PendingCall invoke_method_async(const CallMessage &, const PendingCallSlot &, void*, int timeout = -1);
167 bool dispatch_signal(const SignalMessage &);
169 protected:
171 SignalTable _signals;
174 # define register_method(interface, method, callback) \
175 InterfaceAdaptor::_methods[ #method ] = \
176 new ::DBus::Callback< interface, ::DBus::Message, const ::DBus::CallMessage &>(this, & interface :: callback, ::DBus::CallMessage());
178 # define register_async(interface, method, callback) \
179 InterfaceAdaptor::_asyncs[ #method ] = \
180 new ::DBus::CallbackNoReturn< interface, const ::DBus::CallMessage &>(this, & interface :: callback);
182 # define bind_property(variable, type, can_read, can_write) \
183 InterfaceAdaptor::_properties[ #variable ].read = can_read; \
184 InterfaceAdaptor::_properties[ #variable ].write = can_write; \
185 InterfaceAdaptor::_properties[ #variable ].sig = type; \
186 variable.bind(InterfaceAdaptor::_properties[ #variable ]);
188 # define connect_signal(interface, signal, callback) \
189 InterfaceProxy::_signals[ #signal ] = \
190 new ::DBus::CallbackNoReturn< interface, const ::DBus::SignalMessage &>(this, & interface :: callback);
192 } /* namespace DBus */
194 #endif//__DBUSXX_INTERFACE_H