interface.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-or-later OR BSD-3-Clause
2 /***************************************************************************
3  * Copyright (C) 2019 by Robert Middleton *
4  * robert.middleton@rm5248.com *
5  * *
6  * This file is part of the dbus-cxx library. *
7  ***************************************************************************/
8 #include "enums.h"
9 #include "path.h"
10 #include <dbus-cxx/methodbase.h>
11 #include <dbus-cxx/signal.h>
13 #include <dbus-cxx/property.h>
14 #include <sigc++/sigc++.h>
15 #include <set>
16 #include <map>
17 #include <mutex>
18 #include <shared_mutex>
19 
20 #ifndef DBUSCXX_INTERFACE_H
21 #define DBUSCXX_INTERFACE_H
22 
23 namespace DBus {
24 class CallMessage;
25 class Connection;
26 class Object;
27 class SignalBase;
28 
41 class Interface {
42 protected:
47  Interface( const std::string& name );
48 
49 public:
59  typedef std::map<std::string, std::shared_ptr<MethodBase>> Methods;
60 
68  typedef std::set<std::shared_ptr<SignalBase>> Signals;
69 
74  static std::shared_ptr<Interface> create( const std::string& name = std::string() );
75 
76  virtual ~Interface();
77 
90  HandlerResult handle_call_message( std::shared_ptr<Connection> conn, std::shared_ptr<const CallMessage> message );
91 
102  HandlerResult handle_properties_message( std::shared_ptr<Connection> conn, std::shared_ptr<const CallMessage> message );
103 
105  const std::string& name() const;
106 
108  Path path() const;
109 
111  const Methods& methods() const;
112 
114  std::shared_ptr<MethodBase> method( const std::string& name ) const;
115 
121  template <typename T_type>
122  std::shared_ptr<Method<T_type> >
123  create_method( const std::string& name ) {
124  std::shared_ptr< Method<T_type> > method;
126  this->add_method( method );
127  return method;
128  }
129 
138  template <typename T_type>
139  std::shared_ptr<Method<T_type> >
140  create_method( const std::string& name, sigc::slot<T_type> slot ) {
141  std::shared_ptr< Method<T_type> > method;
143  method->set_method( slot );
144  this->add_method( method );
145  return method;
146  }
147 
148  template <typename T_type>
149  std::shared_ptr<Property<T_type>>
150  create_property( const std::string& name,
153  std::shared_ptr<Property<T_type>> prop = Property<T_type>::create( name, access_type, update_type );
154  add_property( prop );
155  return prop;
156  }
157 
158  bool add_property( std::shared_ptr<PropertyBase> prop );
159 
160  bool has_property( const std::string& name ) const;
161 
163  bool add_method( std::shared_ptr<MethodBase> method );
164 
166  bool remove_method( const std::string& name );
167 
172  bool remove_method( std::shared_ptr<MethodBase> method );
173 
175  bool has_method( const std::string& name ) const;
176 
181  bool add_signal( std::shared_ptr<SignalBase> signal );
182 
189  bool remove_signal( std::shared_ptr<SignalBase> signal );
190 
194  bool remove_signal( const std::string& name );
195 
199  bool has_signal( std::shared_ptr<SignalBase> signal ) const;
200 
202  bool has_signal( const std::string& name ) const;
203 
209  template <class... T_type>
210  std::shared_ptr<Signal<T_type...> >
211  create_signal( const std::string& member ) {
212  std::shared_ptr<DBus::Signal<T_type...> > sig;
213  sig = DBus::Signal<T_type...>::create( path(), name(), member );
214 
215  if( this->add_signal( sig ) ) { return sig; }
216 
217  return std::shared_ptr<DBus::Signal<T_type...> >();
218  }
219 
221  const Signals& signals();
222 
223  const std::set<std::shared_ptr<PropertyBase>>& properties();
224 
230  std::shared_ptr<SignalBase> signal( const std::string& signal_name );
231 
233  sigc::signal<void( std::shared_ptr<MethodBase> )> signal_method_added();
234 
236  sigc::signal<void( std::shared_ptr<MethodBase> )> signal_method_removed();
237 
239  std::string introspect( int space_depth = 0 ) const;
240 
241 private:
242  void set_path( const std::string& new_path );
243  void property_updated( DBus::PropertyBase* prop );
244  void set_connection( std::weak_ptr<Connection> conn );
245 
246 private:
247  class priv_data;
248 
249  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
250 
251  friend class Object;
252  friend class PropertyBase;
253 };
254 
255 } /* namespace DBus */
256 
257 #endif /* DBUS_CXX_INTERFACE_H */
An Interface represents a local copy of a DBus interface.
Definition: interface.h:41
sigc::signal< void(std::shared_ptr< MethodBase >)> signal_method_removed()
Signal emitted when a method of the given name is removed.
Definition: interface.cpp:260
bool add_signal(std::shared_ptr< SignalBase > signal)
Adds the given signal.
Definition: interface.cpp:156
std::shared_ptr< Method< T_type > > create_method(const std::string &name, sigc::slot< T_type > slot)
Creates a method with a signature equivalent to the provided.
Definition: interface.h:140
std::map< std::string, std::shared_ptr< MethodBase > > Methods
Typedef to the storage structure for methods.
Definition: interface.h:59
Interface(const std::string &name)
This class has a protected constructor.
Definition: interface.cpp:42
sigc::signal< void(std::shared_ptr< MethodBase >)> signal_method_added()
Signal emitted when a method of the given name is added.
Definition: interface.cpp:256
const Methods & methods() const
Returns the methods associated with this interface.
Definition: interface.cpp:62
const std::string & name() const
Get the name of this interface.
Definition: interface.cpp:58
bool remove_method(const std::string &name)
Removes the first method with the given name.
Definition: interface.cpp:98
Path path() const
Returns the path of the object associated with this interface or a null string if no object is associ...
Definition: interface.cpp:54
const std::set< std::shared_ptr< PropertyBase > > & properties()
Definition: interface.cpp:463
const Signals & signals()
Returns the signals associated with this interface.
Definition: interface.cpp:238
std::shared_ptr< Method< T_type > > create_method(const std::string &name)
Creates a method with a return value (possibly void ) and $1 parameters.
Definition: interface.h:123
std::string introspect(int space_depth=0) const
Returns a DBus XML description of this interface.
Definition: interface.cpp:264
bool has_signal(std::shared_ptr< SignalBase > signal) const
True if the given signal is part of this interface.
Definition: interface.cpp:215
bool remove_signal(std::shared_ptr< SignalBase > signal)
Removes the given signal.
Definition: interface.cpp:183
std::shared_ptr< MethodBase > method(const std::string &name) const
Returns the method with the given name, or an invalid shared_ptr if not found.
Definition: interface.cpp:66
bool add_method(std::shared_ptr< MethodBase > method)
Adds the named method.
Definition: interface.cpp:80
void property_updated(DBus::PropertyBase *prop)
Definition: interface.cpp:434
HandlerResult handle_properties_message(std::shared_ptr< Connection > conn, std::shared_ptr< const CallMessage > message)
Handle the specified properties message.
Definition: interface.cpp:306
HandlerResult handle_call_message(std::shared_ptr< Connection > conn, std::shared_ptr< const CallMessage > message)
Handles the specified call message.
Definition: interface.cpp:291
bool has_method(const std::string &name) const
True if the interface has a method with the given name.
Definition: interface.cpp:147
void set_connection(std::weak_ptr< Connection > conn)
Definition: interface.cpp:454
std::shared_ptr< Property< T_type > > create_property(const std::string &name, PropertyAccess access_type=PropertyAccess::ReadWrite, PropertyUpdateType update_type=PropertyUpdateType::Updates)
Definition: interface.h:150
std::shared_ptr< SignalBase > signal(const std::string &signal_name)
Returns the first signal found with a matching name.
Definition: interface.cpp:242
bool has_property(const std::string &name) const
Definition: interface.cpp:418
std::set< std::shared_ptr< SignalBase > > Signals
Typedef to the storage structure for signals.
Definition: interface.h:68
std::shared_ptr< Signal< T_type... > > create_signal(const std::string &member)
Creates a signal with any number of parameters.
Definition: interface.h:211
void set_path(const std::string &new_path)
Definition: interface.cpp:391
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
virtual ~Interface()
Definition: interface.cpp:50
static std::shared_ptr< Interface > create(const std::string &name=std::string())
Creates a named Interface.
Definition: interface.cpp:46
bool add_property(std::shared_ptr< PropertyBase > prop)
Definition: interface.cpp:399
static std::shared_ptr< Method< T_type > > create(const std::string &name)
Definition: methodbase.h:84
An Object represents a local object that is able to be called over the DBus.
Definition: object.h:50
Represents a DBus Path.
Definition: path.h:21
Base type of Property to allow for storage in e.g.
Definition: property.h:27
static std::shared_ptr< Property< T_type > > create(std::string name, PropertyAccess access, PropertyUpdateType update)
Definition: property.h:94
Definition: signal.h:20
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
PropertyAccess
Definition: enums.h:46
HandlerResult
Definition: enums.h:95
PropertyUpdateType
Definition: enums.h:23
@ Updates
When this property changes, the PropertyChanged signal will be emitted with the new value.