interface.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *  D-Bus++ - C++ bindings for D-Bus
00004  *
00005  *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
00006  *
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Lesser General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2.1 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 
00025 #ifndef __DBUSXX_INTERFACE_H
00026 #define __DBUSXX_INTERFACE_H
00027 
00028 #include <string>
00029 #include <map>
00030 #include "api.h"
00031 #include "util.h"
00032 #include "types.h"
00033 
00034 #include "message.h"
00035 
00036 namespace DBus
00037 {
00038 
00039 //todo: this should belong to to properties.h
00040 struct DXXAPI PropertyData
00041 {
00042   bool    read;
00043   bool    write;
00044   std::string sig;
00045   Variant   value;
00046 };
00047 
00048 typedef std::map<std::string, PropertyData> PropertyTable;
00049 
00050 class IntrospectedInterface;
00051 
00052 class ObjectAdaptor;
00053 class InterfaceAdaptor;
00054 class SignalMessage;
00055 
00056 typedef std::map<std::string, InterfaceAdaptor *> InterfaceAdaptorTable;
00057 
00058 class DXXAPI AdaptorBase
00059 {
00060 public:
00061 
00062   virtual const ObjectAdaptor *object() const = 0 ;
00063 
00064 protected:
00065 
00066   InterfaceAdaptor *find_interface(const std::string &name);
00067 
00068   virtual ~AdaptorBase()
00069   {}
00070 
00071   virtual void _emit_signal(SignalMessage &) = 0;
00072 
00073   InterfaceAdaptorTable _interfaces;
00074 };
00075 
00076 /*
00077 */
00078 
00079 class ObjectProxy;
00080 class InterfaceProxy;
00081 class CallMessage;
00082 
00083 typedef std::map<std::string, InterfaceProxy *> InterfaceProxyTable;
00084 
00085 class DXXAPI ProxyBase
00086 {
00087 public:
00088 
00089   virtual const ObjectProxy *object() const = 0 ;
00090 
00091 protected:
00092 
00093   InterfaceProxy *find_interface(const std::string &name);
00094 
00095   virtual ~ProxyBase()
00096   {}
00097 
00098   virtual Message _invoke_method(CallMessage &) = 0;
00099 
00100   virtual bool _invoke_method_noreply(CallMessage &call) = 0;
00101 
00102   InterfaceProxyTable _interfaces;
00103 };
00104 
00105 class DXXAPI Interface
00106 {
00107 public:
00108 
00109   Interface(const std::string &name);
00110 
00111   virtual ~Interface();
00112 
00113   inline const std::string &name() const;
00114 
00115 private:
00116 
00117   std::string   _name;
00118 };
00119 
00120 /*
00121 */
00122 
00123 const std::string &Interface::name() const
00124 {
00125   return _name;
00126 }
00127 
00128 /*
00129 */
00130 
00131 typedef std::map< std::string, Slot<Message, const CallMessage &> > MethodTable;
00132 
00133 class DXXAPI InterfaceAdaptor : public Interface, public virtual AdaptorBase
00134 {
00135 public:
00136 
00137   InterfaceAdaptor(const std::string &name);
00138 
00139   Message dispatch_method(const CallMessage &);
00140 
00141   void emit_signal(const SignalMessage &);
00142 
00143   Variant *get_property(const std::string &name);
00144 
00145   void set_property(const std::string &name, Variant &value);
00146 
00147   virtual IntrospectedInterface *introspect() const
00148   {
00149     return NULL;
00150   }
00151 
00152 protected:
00153 
00154   MethodTable _methods;
00155   PropertyTable _properties;
00156 };
00157 
00158 /*
00159 */
00160 
00161 typedef std::map< std::string, Slot<void, const SignalMessage &> > SignalTable;
00162 
00163 class DXXAPI InterfaceProxy : public Interface, public virtual ProxyBase
00164 {
00165 public:
00166 
00167   InterfaceProxy(const std::string &name);
00168 
00169   Message invoke_method(const CallMessage &);
00170 
00171   bool invoke_method_noreply(const CallMessage &call);
00172 
00173   bool dispatch_signal(const SignalMessage &);
00174 
00175 protected:
00176 
00177   SignalTable _signals;
00178 };
00179 
00180 # define register_method(interface, method, callback) \
00181   InterfaceAdaptor::_methods[ #method ] = \
00182     new ::DBus::Callback< interface, ::DBus::Message, const ::DBus::CallMessage &>(this, & interface :: callback);
00183 
00184 # define bind_property(variable, type, can_read, can_write) \
00185   InterfaceAdaptor::_properties[ #variable ].read = can_read; \
00186   InterfaceAdaptor::_properties[ #variable ].write = can_write; \
00187   InterfaceAdaptor::_properties[ #variable ].sig = type; \
00188   variable.bind(InterfaceAdaptor::_properties[ #variable ]);
00189 
00190 # define connect_signal(interface, signal, callback) \
00191   InterfaceProxy::_signals[ #signal ] = \
00192     new ::DBus::Callback< interface, void, const ::DBus::SignalMessage &>(this, & interface :: callback);
00193 
00194 } /* namespace DBus */
00195 
00196 #endif//__DBUSXX_INTERFACE_H