object.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) 2009,2010 by Rick L. Vinyard, Jr. *
4  * rvinyard@cs.nmsu.edu *
5  * Copyright (C) 2019 by Robert Middleton *
6  * robert.middleton@rm5248.com *
7  * *
8  * This file is part of the dbus-cxx library. *
9  ***************************************************************************/
10 #include <dbus-cxx/signal.h>
11 #include <dbus-cxx/interface.h>
13 #include <dbus-cxx/property.h>
14 #include <map>
15 #include <memory>
16 #include <mutex>
17 #include <shared_mutex>
18 #include <string>
19 #include "enums.h"
20 #include <sigc++/sigc++.h>
21 
22 #ifndef DBUSCXXOBJECT_H
23 #define DBUSCXXOBJECT_H
24 
25 namespace DBus {
26 class Connection;
27 class Interface;
28 class Message;
29 template <typename T_type> class Method;
30 
50 class Object {
51 protected:
52 
57  Object( const std::string& path );
58 
59 public:
69  typedef std::map<std::string, std::shared_ptr<Interface> > Interfaces;
70 
81  typedef std::map<std::string, std::shared_ptr<Object>> Children;
82 
87  static std::shared_ptr<Object> create( const std::string& path = std::string() );
88 
89  virtual ~Object();
90 
92  const Path& path() const;
93 
95  std::weak_ptr<Connection> connection() const;
96 
98  bool unregister();
99 
101  sigc::signal<void( std::shared_ptr<Connection> ) >& signal_registered();
102 
106  sigc::signal<void( std::shared_ptr<Connection> ) >& signal_unregistered();
107 
115  void set_connection( std::shared_ptr<Connection> conn );
116 
118  const Interfaces& interfaces() const;
119 
121  std::shared_ptr<Interface> interface_by_name( const std::string& name ) const;
122 
124  bool add_interface( std::shared_ptr<Interface> interface_by_name );
125 
133  std::shared_ptr<Interface> create_interface( const std::string& name );
134 
143  template <typename T_type>
144  std::shared_ptr<Method<T_type> >
145  create_method( const std::string& method_name, sigc::slot<T_type> slot ) {
146  if( !default_interface() ) {
147  this->create_interface( "" );
148  this->set_default_interface( "" );
149  }
150 
151  // TODO throw an error if the default interface still doesn't exist
152 
153  std::shared_ptr< Method<T_type> > method;
154  method = default_interface()->create_method<T_type>( method_name );
155  method->set_method( slot );
156  return method;
157  }
158 
169  template <typename T_type>
170  std::shared_ptr<Method<T_type> >
171  create_method( const std::string& interface_name, const std::string& method_name, sigc::slot<T_type> slot ) {
172  std::shared_ptr<Interface> interface_ptr;
173  interface_ptr = this->interface_by_name( interface_name );
174 
175  if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
176 
177  // TODO throw an error if the interface still doesn't exist
178 
179  std::shared_ptr< Method<T_type> > method;
180  method = interface_ptr->create_method<T_type>( method_name );
181  method->set_method( slot );
182  return method;
183  }
184 
185  template <typename T_type>
186  std::shared_ptr<Property<T_type>>
187  create_property( const std::string& interface_name,
188  const std::string& property_name,
191  std::shared_ptr<Interface> interface_ptr;
192  interface_ptr = this->interface_by_name( interface_name );
193 
194  if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
195 
196  return interface_ptr->create_property<T_type>( property_name, access_type, update_type );
197  }
198 
204  void remove_interface( const std::string& name );
205 
211  bool has_interface( const std::string& name );
212 
217  std::shared_ptr<Interface> default_interface() const;
218 
229  bool set_default_interface( const std::string& new_default_name );
230 
237  bool set_default_interface( std::shared_ptr<Interface> interface_by_name );
238 
244 
256  template <class... T_type>
257  std::shared_ptr<Signal<T_type...> >
258  create_signal( const std::string& name ) {
259  std::shared_ptr<DBus::Signal<T_type...> > sig;
260  std::shared_ptr<Interface> iface = this->default_interface();
261 
262  if( !iface ) { iface = this->create_interface( "" ); }
263 
264  sig = iface->create_signal<T_type...>( name );
265  return sig;
266  }
267 
277  template <class... T_type>
278  std::shared_ptr<Signal<T_type...> >
279  create_signal( const std::string& iface, const std::string& name ) {
280  std::shared_ptr<DBus::Signal<T_type...> > sig;
281 
282  if( !has_interface( iface ) ) { this->create_interface( iface ); }
283 
284  sig = this->interface_by_name( iface )->create_signal<T_type...>( name );
285  return sig;
286  }
287 
289  const Children& children() const;
290 
296  std::shared_ptr<Object> child( const std::string& name ) const;
297 
307  bool add_child( const std::string& name, std::shared_ptr<Object> child, bool force = false );
308 
314  bool remove_child( const std::string& name );
315 
321  bool has_child( const std::string& name ) const;
322 
324  std::string introspect( int space_depth = 0 ) const;
325 
331  sigc::signal<void( std::shared_ptr<Interface> ) > signal_interface_added();
332 
338  sigc::signal<void( std::shared_ptr<Interface> ) > signal_interface_removed();
339 
347  sigc::signal<void( std::shared_ptr<Interface> /*old default*/, std::shared_ptr<Interface> /*new default*/ )> signal_default_interface_changed();
348 
370  HandlerResult handle_message( std::shared_ptr<const Message> msg );
371 
372 private:
373  class priv_data;
374 
375  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
376 };
377 
378 }
379 #endif
An Object represents a local object that is able to be called over the DBus.
Definition: object.h:50
static std::shared_ptr< Object > create(const std::string &path=std::string())
Creates a named Object that will register as a primary or fallback handler.
Definition: object.cpp:54
HandlerResult handle_message(std::shared_ptr< const Message > msg)
Handles the specified message on the specified connection.
Definition: object.cpp:346
std::shared_ptr< Interface > create_interface(const std::string &name)
Creates and adds the named interface to this object.
Definition: object.cpp:149
const Path & path() const
Returns the path this handler is associated with.
Definition: object.cpp:61
virtual ~Object()
Definition: object.cpp:58
std::shared_ptr< Interface > default_interface() const
Get the default interface associated with this object.
Definition: object.cpp:198
bool remove_child(const std::string &name)
Remove the named child from this object.
Definition: object.cpp:267
std::shared_ptr< Signal< T_type... > > create_signal(const std::string &name)
Creates a signal with a return value (possibly void ) and a variable number of parameters and adds it...
Definition: object.h:258
std::string introspect(int space_depth=0) const
Returns a DBus XML description of this interface.
Definition: object.cpp:280
const Children & children() const
Get the children associated with this object instance.
Definition: object.cpp:239
Object(const std::string &path)
This class has a protected constructor.
Definition: object.cpp:49
const Interfaces & interfaces() const
Get all the interfaces associated with this Object instance.
Definition: object.cpp:100
std::shared_ptr< Object > child(const std::string &name) const
Get a named child of this object.
Definition: object.cpp:243
std::shared_ptr< Method< T_type > > create_method(const std::string &interface_name, const std::string &method_name, sigc::slot< T_type > slot)
Creates a method with a signature based on the.
Definition: object.h:171
bool has_child(const std::string &name) const
Test whether an object has a child with a specified name.
Definition: object.cpp:276
std::map< std::string, std::shared_ptr< Object > > Children
Typedef to storage structure for an Object instance's children.
Definition: object.h:81
std::shared_ptr< Signal< T_type... > > create_signal(const std::string &iface, const std::string &name)
Creates a signal with a return value (possibly void ) and a variable number of parameters and adds it...
Definition: object.h:279
bool set_default_interface(const std::string &new_default_name)
Set the default interface to a specific name.
Definition: object.cpp:202
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
bool add_interface(std::shared_ptr< Interface > interface_by_name)
Adds the interface to this object.
Definition: object.cpp:118
sigc::signal< void(std::shared_ptr< Connection >) > & signal_registered()
Emitted when this object is registered with a connection.
Definition: object.cpp:77
sigc::signal< void(std::shared_ptr< Interface >) > signal_interface_removed()
Signal emitted when an interface is removed from this object.
Definition: object.cpp:338
bool unregister()
Unregisters the handler.
Definition: object.cpp:69
std::map< std::string, std::shared_ptr< Interface > > Interfaces
Typedef to the storage structure for an Object instance's interfaces.
Definition: object.h:69
bool has_interface(const std::string &name)
Test whether an Object has a named interface.
Definition: object.cpp:189
std::shared_ptr< Interface > interface_by_name(const std::string &name) const
Returns the interface with the given name.
Definition: object.cpp:104
void set_connection(std::shared_ptr< Connection > conn)
Sets the connection that this object is on.
Definition: object.cpp:85
void remove_interface(const std::string &name)
Removes the interface found with the given name.
Definition: object.cpp:161
sigc::signal< void(std::shared_ptr< Interface >) > signal_interface_added()
Signal emitted when an interface is added to this object.
Definition: object.cpp:334
sigc::signal< void(std::shared_ptr< Connection >) > & signal_unregistered()
Emitted when this object is unregistered from a connection.
Definition: object.cpp:81
std::shared_ptr< Property< T_type > > create_property(const std::string &interface_name, const std::string &property_name, PropertyAccess access_type=PropertyAccess::ReadWrite, PropertyUpdateType update_type=PropertyUpdateType::Updates)
Definition: object.h:187
std::shared_ptr< Method< T_type > > create_method(const std::string &method_name, sigc::slot< T_type > slot)
Creates a method with a signature based on the.
Definition: object.h:145
std::weak_ptr< Connection > connection() const
Returns the connection this handler is registered with.
Definition: object.cpp:65
bool add_child(const std::string &name, std::shared_ptr< Object > child, bool force=false)
Add an object as a child with a specified name This method will fail if the object already has a chil...
Definition: object.cpp:251
sigc::signal< void(std::shared_ptr< Interface >, std::shared_ptr< Interface >)> signal_default_interface_changed()
Signal emitted when the default interface of this object is changed.
Definition: object.cpp:342
void remove_default_interface()
Removes the currently set (if any) default interface.
Definition: object.cpp:231
Represents a DBus Path.
Definition: path.h:21
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.