dbus-cxx
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>
12#include <dbus-cxx/dbus-cxx-config.h>
13#include <dbus-cxx/property.h>
14#include <map>
15#include <memory>
16#include <mutex>
17#include <shared_mutex>
18#include <thread>
19#include <string>
20#include "enums.h"
21#include <sigc++/sigc++.h>
22
23#ifndef DBUSCXXOBJECT_H
24#define DBUSCXXOBJECT_H
25
26namespace DBus {
27class Connection;
28class Interface;
29class Message;
30template <typename T_type> class Method;
31
51class Object {
52protected:
53
58 Object( const std::string& path );
59
60public:
70 typedef std::map<std::string, std::shared_ptr<Interface> > Interfaces;
71
82 typedef std::map<std::string, std::shared_ptr<Object>> Children;
83
92 static std::shared_ptr<Object> create( const std::string& path = std::string() );
93
94 virtual ~Object();
95
99 const Path& path() const;
100
102 std::weak_ptr<Connection> connection() const;
103
105 bool unregister();
106
108 sigc::signal<void( std::shared_ptr<Connection> ) >& signal_registered();
109
113 sigc::signal<void( std::shared_ptr<Connection> ) >& signal_unregistered();
114
122 void set_connection( std::shared_ptr<Connection> conn );
123
125 const Interfaces& interfaces() const;
126
128 std::shared_ptr<Interface> interface_by_name( const std::string& name ) const;
129
131 bool add_interface( std::shared_ptr<Interface> interface_by_name );
132
140 std::shared_ptr<Interface> create_interface( const std::string& name );
141
150 template <typename T_type>
151 std::shared_ptr<Method<T_type> >
152 create_method( const std::string& method_name, sigc::slot<T_type> slot ) {
153 if( !default_interface() ) {
154 this->create_interface( "" );
155 this->set_default_interface( "" );
156 }
157
158 // TODO throw an error if the default interface still doesn't exist
159
160 std::shared_ptr< Method<T_type> > method;
161 method = default_interface()->create_method<T_type>( method_name );
162 method->set_method( slot );
163 return method;
164 }
165
176 template <typename T_type>
177 std::shared_ptr<Method<T_type> >
178 create_method( const std::string& interface_name, const std::string& method_name, sigc::slot<T_type> slot ) {
179 std::shared_ptr<Interface> interface_ptr;
180 interface_ptr = this->interface_by_name( interface_name );
181
182 if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
183
184 // TODO throw an error if the interface still doesn't exist
185
186 std::shared_ptr< Method<T_type> > method;
187 method = interface_ptr->create_method<T_type>( method_name );
188 method->set_method( slot );
189 return method;
190 }
191
192 template <typename T_type>
193 std::shared_ptr<Property<T_type>>
194 create_property( const std::string& interface_name,
195 const std::string& property_name,
198 std::shared_ptr<Interface> interface_ptr;
199 interface_ptr = this->interface_by_name( interface_name );
200
201 if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
202
203 return interface_ptr->create_property<T_type>( property_name, access_type, update_type );
204 }
205
211 void remove_interface( const std::string& name );
212
218 bool has_interface( const std::string& name );
219
224 std::shared_ptr<Interface> default_interface() const;
225
236 bool set_default_interface( const std::string& new_default_name );
237
244 bool set_default_interface( std::shared_ptr<Interface> interface_by_name );
245
251
263 template <class... T_type>
264 std::shared_ptr<Signal<T_type...> >
265 create_signal( const std::string& name ) {
266 std::shared_ptr<DBus::Signal<T_type...> > sig;
267 std::shared_ptr<Interface> iface = this->default_interface();
268
269 if( !iface ) { iface = this->create_interface( "" ); }
270
271 sig = iface->create_signal<T_type...>( name );
272 return sig;
273 }
274
284 template <class... T_type>
285 std::shared_ptr<Signal<T_type...> >
286 create_signal( const std::string& iface, const std::string& name ) {
287 std::shared_ptr<DBus::Signal<T_type...> > sig;
288
289 if( !has_interface( iface ) ) { this->create_interface( iface ); }
290
291 sig = this->interface_by_name( iface )->create_signal<T_type...>( name );
292 return sig;
293 }
294
296 const Children& children() const;
297
303 std::shared_ptr<Object> child( const std::string& name ) const;
304
314 bool add_child( const std::string& name, std::shared_ptr<Object> child, bool force = false );
315
321 bool remove_child( const std::string& name );
322
328 bool has_child( const std::string& name ) const;
329 bool has_child( std::shared_ptr<Object> child ) const;
330
332 std::string introspect( int space_depth = 0 ) const;
333
339 sigc::signal<void( std::shared_ptr<Interface> ) > signal_interface_added();
340
346 sigc::signal<void( std::shared_ptr<Interface> ) > signal_interface_removed();
347
355 sigc::signal<void( std::shared_ptr<Interface> /*old default*/, std::shared_ptr<Interface> /*new default*/ )> signal_default_interface_changed();
356
378 HandlerResult handle_message( std::shared_ptr<const Message> msg );
379
380 void set_handling_thread( std::thread::id thr );
381 std::thread::id handling_thread();
382
383 bool is_lightweight() const;
384
385 static std::shared_ptr<Object> create_lightweight( const std::string& path = std::string() );
386
387 bool has_objectmanager() const;
388
389 void set_has_objectmanager( bool objectmanager );
390
391private:
393 std::map<std::string,std::map<std::string,DBus::Variant>> interfaces_and_properties();
394 void child_interface_removed( std::shared_ptr<DBus::Interface> iface );
395 void child_interface_added( std::shared_ptr<DBus::Interface> iface );
396 void add_self_to_objectmanager( DBus::Object* obj_manager );
397
398private:
399 class priv_data;
400
401 DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
402};
403
404}
405#endif
An Object represents a local object that is able to be called over the DBus.
Definition: object.h:51
static std::shared_ptr< Object > create(const std::string &path=std::string())
Create an Object that will be exported out onto the bus.
Definition: object.cpp:61
HandlerResult handle_message(std::shared_ptr< const Message > msg)
Handles the specified message on the specified connection.
Definition: object.cpp:394
void set_handling_thread(std::thread::id thr)
Definition: object.cpp:509
std::shared_ptr< Interface > create_interface(const std::string &name)
Creates and adds the named interface to this object.
Definition: object.cpp:165
const Path & path() const
Returns the path this handler is associated with.
Definition: object.cpp:74
virtual ~Object()
Definition: object.cpp:71
std::shared_ptr< Interface > default_interface() const
Get the default interface associated with this object.
Definition: object.cpp:214
bool has_objectmanager() const
Definition: object.cpp:547
bool remove_child(const std::string &name)
Remove the named child from this object.
Definition: object.cpp:288
void child_interface_added(std::shared_ptr< DBus::Interface > iface)
Definition: object.cpp:601
std::string introspect(int space_depth=0) const
Returns a DBus XML description of this interface.
Definition: object.cpp:311
const Children & children() const
Get the children associated with this object instance.
Definition: object.cpp:255
Object(const std::string &path)
This class has a protected constructor.
Definition: object.cpp:54
const Interfaces & interfaces() const
Get all the interfaces associated with this Object instance.
Definition: object.cpp:116
std::shared_ptr< Object > child(const std::string &name) const
Get a named child of this object.
Definition: object.cpp:259
static std::shared_ptr< Object > create_lightweight(const std::string &path=std::string())
Definition: object.cpp:65
bool has_child(const std::string &name) const
Test whether an object has a child with a specified name.
Definition: object.cpp:297
std::map< std::string, std::shared_ptr< Object > > Children
Typedef to storage structure for an Object instance's children.
Definition: object.h:82
bool set_default_interface(const std::string &new_default_name)
Set the default interface to a specific name.
Definition: object.cpp:218
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:134
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:194
sigc::signal< void(std::shared_ptr< Connection >) > & signal_registered()
Emitted when this object is registered with a connection.
Definition: object.cpp:90
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:178
std::thread::id handling_thread()
Definition: object.cpp:513
sigc::signal< void(std::shared_ptr< Interface >) > signal_interface_removed()
Signal emitted when an interface is removed from this object.
Definition: object.cpp:386
bool unregister()
Unregisters the handler.
Definition: object.cpp:82
void set_has_objectmanager(bool objectmanager)
Definition: object.cpp:551
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:286
std::map< std::string, std::shared_ptr< Interface > > Interfaces
Typedef to the storage structure for an Object instance's interfaces.
Definition: object.h:70
void handle_objectmanager(ObjectManagerObjects *ret)
Definition: object.cpp:539
bool has_interface(const std::string &name)
Test whether an Object has a named interface.
Definition: object.cpp:205
std::shared_ptr< Interface > interface_by_name(const std::string &name) const
Returns the interface with the given name.
Definition: object.cpp:120
void set_connection(std::shared_ptr< Connection > conn)
Sets the connection that this object is on.
Definition: object.cpp:98
void add_self_to_objectmanager(DBus::Object *obj_manager)
Definition: object.cpp:573
void remove_interface(const std::string &name)
Removes the interface found with the given name.
Definition: object.cpp:177
sigc::signal< void(std::shared_ptr< Interface >) > signal_interface_added()
Signal emitted when an interface is added to this object.
Definition: object.cpp:382
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:152
sigc::signal< void(std::shared_ptr< Connection >) > & signal_unregistered()
Emitted when this object is unregistered from a connection.
Definition: object.cpp:94
void child_interface_removed(std::shared_ptr< DBus::Interface > iface)
Definition: object.cpp:584
std::weak_ptr< Connection > connection() const
Returns the connection this handler is registered with.
Definition: object.cpp:78
std::map< std::string, std::map< std::string, DBus::Variant > > interfaces_and_properties()
Definition: object.cpp:521
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:267
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:390
void remove_default_interface()
Removes the currently set (if any) default interface.
Definition: object.cpp:247
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:265
bool is_lightweight() const
Definition: object.cpp:517
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
std::map< DBus::Path, std::map< std::string, std::map< std::string, DBus::Variant > > > ObjectManagerObjects
Definition: types.h:22
PropertyUpdateType
Definition: enums.h:23
@ Updates
When this property changes, the PropertyChanged signal will be emitted with the new value.
static std::shared_ptr< DBus::Connection > conn
Definition: recursive-signal-test-qt.cpp:30