objectproxy.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  * *
6  * This file is part of the dbus-cxx library. *
7  ***************************************************************************/
8 #include <dbus-cxx/signalproxy.h>
11 #include <dbus-cxx/propertyproxy.h>
12 #include <map>
13 #include <memory>
14 #include <mutex>
15 #include <shared_mutex>
16 #include <string>
17 #include "path.h"
18 #include <sigc++/sigc++.h>
19 
20 #ifndef DBUSCXX_OBJECTPROXY_H
21 #define DBUSCXX_OBJECTPROXY_H
22 
23 namespace DBus {
24 
25 class Connection;
26 class CallMessage;
27 class InterfaceProxy;
28 class MethodProxyBase;
29 class PendingCall;
30 class ReturnMessage;
31 template <typename signature> class MethodProxy;
32 class PeerInterfaceProxy;
33 class IntrospectableInterfaceProxy;
34 class PropertiesInterfaceProxy;
35 
44 class ObjectProxy {
45 protected:
46 
51  ObjectProxy( std::shared_ptr<Connection> conn, const std::string& destination, const std::string& path );
52 
53 public:
58  static std::shared_ptr<ObjectProxy> create( const std::string& path );
59 
65  static std::shared_ptr<ObjectProxy> create( const std::string& destination, const std::string& path );
66 
67  static std::shared_ptr<ObjectProxy> create( std::shared_ptr<Connection> conn, const std::string& path );
68 
69  static std::shared_ptr<ObjectProxy> create( std::shared_ptr<Connection> conn, const std::string& destination, const std::string& path );
70 
71  virtual ~ObjectProxy();
72 
73  std::weak_ptr<Connection> connection() const;
74 
75  void set_connection( std::shared_ptr<Connection> conn );
76 
77  const std::string& destination() const;
78 
79  void set_destination( const std::string& destination );
80 
81  const Path& path() const;
82 
83  void set_path( const std::string& path );
84 
85  typedef std::map<std::string, std::shared_ptr<InterfaceProxy>> Interfaces;
86 
87  const Interfaces& interfaces() const;
88 
90  std::shared_ptr<InterfaceProxy> interface_by_name( const std::string& name ) const;
91 
93  bool add_interface( std::shared_ptr<InterfaceProxy> interface_ptr );
94 
100  std::shared_ptr<InterfaceProxy> create_interface( const std::string& name );
101 
103  void remove_interface( const std::string& name );
104 
106  void remove_interface( std::shared_ptr<InterfaceProxy> interface_ptr );
107 
108  bool has_interface( const std::string& name ) const;
109 
110  bool has_interface( std::shared_ptr<InterfaceProxy> interface_ptr ) const;
111 
113  bool add_method( const std::string& interface_name, std::shared_ptr<MethodProxyBase> method );
114 
115  std::shared_ptr<CallMessage> create_call_message( const std::string& interface_name, const std::string& method_name ) const;
116 
117  std::shared_ptr<CallMessage> create_call_message( const std::string& method_name ) const;
118 
126  std::shared_ptr<const ReturnMessage> call( std::shared_ptr<const CallMessage>, int timeout_milliseconds = -1 ) const;
127 
134  template <class T_type>
135  std::shared_ptr<MethodProxy<T_type>>
136  create_method( const std::string& interface_name, const std::string& method_name ) {
137  std::shared_ptr<InterfaceProxy> interface_ptr = this->interface_by_name( interface_name );
138 
139  if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
140 
141  return interface_ptr->create_method<T_type>( method_name );
142  }
143 
150  template <class T_type>
151  std::shared_ptr<SignalProxy<T_type> >
152  create_signal( const std::string& interface_name, const std::string& sig_name ) {
153  std::shared_ptr<InterfaceProxy> interface_ptr = this->interface_by_name( interface_name );
154 
155  if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
156 
157  return interface_ptr->create_signal<T_type>( sig_name );
158  }
159 
160  template <class T_type>
161  std::shared_ptr<PropertyProxy<T_type>>
162  create_property( const std::string& interface_name,
163  const std::string& property_name,
166  std::shared_ptr<InterfaceProxy> interface_ptr = this->interface_by_name( interface_name );
167 
168  if( !interface_ptr ) { interface_ptr = this->create_interface( interface_name ); }
169 
170  return interface_ptr->create_property<T_type>( property_name, update );
171  }
172 
179  sigc::signal<void( std::shared_ptr<InterfaceProxy> )> signal_interface_added();
180 
187  sigc::signal<void( std::shared_ptr<InterfaceProxy> )> signal_interface_removed();
188 
189  std::shared_ptr<PeerInterfaceProxy> getPeerInterface();
190 
191  std::shared_ptr<IntrospectableInterfaceProxy> getIntrospectableInterface();
192 
193  std::shared_ptr<PropertiesInterfaceProxy> getPropertiesInterface();
194 
195 private:
196  class priv_data;
197 
198  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
199 };
200 
201 }
202 
203 #endif
Object proxies are local proxies that provide local methods and signals for remote objects with dbus ...
Definition: objectproxy.h:44
void remove_interface(const std::string &name)
Removes the first interface with the given name.
Definition: objectproxy.cpp:149
std::shared_ptr< SignalProxy< T_type > > create_signal(const std::string &interface_name, const std::string &sig_name)
Creates a signal proxy with a signature based on the template parameters and adds it to the named int...
Definition: objectproxy.h:152
std::shared_ptr< CallMessage > create_call_message(const std::string &interface_name, const std::string &method_name) const
Definition: objectproxy.cpp:234
std::shared_ptr< PropertiesInterfaceProxy > getPropertiesInterface()
Definition: objectproxy.cpp:282
std::weak_ptr< Connection > connection() const
Definition: objectproxy.cpp:73
std::shared_ptr< const ReturnMessage > call(std::shared_ptr< const CallMessage >, int timeout_milliseconds=-1) const
Forwards this CallMessage to the Connection that this ObjectProxy is on, and returns a message with t...
Definition: objectproxy.cpp:258
std::shared_ptr< InterfaceProxy > interface_by_name(const std::string &name) const
Returns the first interface with the given name.
Definition: objectproxy.cpp:105
sigc::signal< void(std::shared_ptr< InterfaceProxy >)> signal_interface_removed()
Return a signal that you may connect to when an interface is removed.
Definition: objectproxy.cpp:270
std::shared_ptr< InterfaceProxy > create_interface(const std::string &name)
Creates and adds the named interface to this object.
Definition: objectproxy.cpp:139
std::shared_ptr< MethodProxy< T_type > > create_method(const std::string &interface_name, const std::string &method_name)
Creates a proxy method with a signature based on the template parameters and adds it to the named int...
Definition: objectproxy.h:136
void set_connection(std::shared_ptr< Connection > conn)
Definition: objectproxy.cpp:77
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
sigc::signal< void(std::shared_ptr< InterfaceProxy >)> signal_interface_added()
Return a signal that you may connect to when an interface is added.
Definition: objectproxy.cpp:266
std::shared_ptr< PropertyProxy< T_type > > create_property(const std::string &interface_name, const std::string &property_name, PropertyAccess access_type=PropertyAccess::ReadWrite, PropertyUpdateType update=PropertyUpdateType::Updates)
Definition: objectproxy.h:162
std::shared_ptr< IntrospectableInterfaceProxy > getIntrospectableInterface()
Definition: objectproxy.cpp:278
std::map< std::string, std::shared_ptr< InterfaceProxy > > Interfaces
Definition: objectproxy.h:85
ObjectProxy(std::shared_ptr< Connection > conn, const std::string &destination, const std::string &path)
This class has a protected constructor.
Definition: objectproxy.cpp:47
void set_destination(const std::string &destination)
Definition: objectproxy.cpp:85
virtual ~ObjectProxy()
Definition: objectproxy.cpp:70
std::shared_ptr< PeerInterfaceProxy > getPeerInterface()
Definition: objectproxy.cpp:274
bool add_method(const std::string &interface_name, std::shared_ptr< MethodProxyBase > method)
Adds the method to the named interface.
Definition: objectproxy.cpp:224
static std::shared_ptr< ObjectProxy > create(const std::string &path)
Creates an ObjectProxy with a specific path.
Definition: objectproxy.cpp:54
bool has_interface(const std::string &name) const
Definition: objectproxy.cpp:192
void set_path(const std::string &path)
Definition: objectproxy.cpp:93
const std::string & destination() const
Definition: objectproxy.cpp:81
const Interfaces & interfaces() const
Definition: objectproxy.cpp:101
const Path & path() const
Definition: objectproxy.cpp:89
bool add_interface(std::shared_ptr< InterfaceProxy > interface_ptr)
Adds the interface to this object.
Definition: objectproxy.cpp:119
Represents a DBus Path.
Definition: path.h:21
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
PropertyAccess
Definition: enums.h:46
PropertyUpdateType
Definition: enums.h:23
@ Updates
When this property changes, the PropertyChanged signal will be emitted with the new value.