interfaceproxy.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  ***************************************************************************/
9 #include <dbus-cxx/signalproxy.h>
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <set>
14 #include <shared_mutex>
15 #include <string>
16 #include "path.h"
17 #include <sigc++/sigc++.h>
18 #include <dbus-cxx/propertyproxy.h>
19 
20 #ifndef DBUSCXX_INTERFACEPROXY_H
21 #define DBUSCXX_INTERFACEPROXY_H
22 
23 namespace DBus {
24 
25 class CallMessage;
26 class Connection;
27 class ObjectProxy;
28 class PendingCall;
29 class ReturnMessage;
30 template <typename signature> class MethodProxy;
31 
44 protected:
45  InterfaceProxy( const std::string& name );
46 
47 public:
48  typedef std::multimap<std::string, std::shared_ptr<MethodProxyBase>> Methods;
49 
50  typedef std::set<std::shared_ptr<SignalProxyBase>> Signals;
51 
52  static std::shared_ptr<InterfaceProxy> create( const std::string& name = std::string() );
53 
54  virtual ~InterfaceProxy();
55 
56  ObjectProxy* object() const;
57 
58  Path path() const;
59 
60  std::weak_ptr<Connection> connection() const;
61 
62  const std::string& name() const;
63 
64  const Methods& methods() const;
65 
67  std::shared_ptr<MethodProxyBase> method( const std::string& name ) const;
68 
69  template <class T_type>
70  std::shared_ptr<MethodProxy<T_type>> create_method( const std::string& name ) {
71  std::shared_ptr< MethodProxy<T_type> > method;
73 
74  if( !this->add_method( method ) ) {
75  return std::shared_ptr< MethodProxy<T_type> >();
76  }
77 
78  return method;
79  }
80 
85  bool add_method( std::shared_ptr<MethodProxyBase> method );
86 
88  void remove_method( const std::string& name );
89 
91  void remove_method( std::shared_ptr<MethodProxyBase> method );
92 
94  bool has_method( const std::string& name ) const;
95 
97  bool has_method( std::shared_ptr<MethodProxyBase> method ) const;
98 
103  const std::map<std::string,std::shared_ptr<PropertyProxyBase>>& properties() const;
104 
105  std::shared_ptr<PropertyProxyBase> property( const std::string& name ) const;
106 
108  bool has_property( const std::string& name ) const;
109 
111  bool has_property( std::shared_ptr<PropertyProxyBase> property ) const;
112 
114  void remove_property( const std::string& name );
115 
117  void remove_property( std::shared_ptr<PropertyProxyBase> method );
118 
122  template <class T_type>
123  std::shared_ptr<PropertyProxy<T_type>> create_property( const std::string& name,
125  std::shared_ptr< PropertyProxy<T_type> > prop;
126  prop = PropertyProxy<T_type>::create( name, update );
127 
128  if( !this->add_property( prop ) ) {
129  return std::shared_ptr< PropertyProxy<T_type> >();
130  }
131 
132  return prop;
133  }
134 
139  bool add_property( std::shared_ptr<PropertyProxyBase> property );
140 
144  void cache_properties();
145 
146  std::shared_ptr<CallMessage> create_call_message( const std::string& method_name ) const;
147 
148  std::shared_ptr<const ReturnMessage> call( std::shared_ptr<const CallMessage>, int timeout_milliseconds = -1 ) const;
149 
150  // std::shared_ptr<PendingCall> call_async( std::shared_ptr<const CallMessage>, int timeout_milliseconds=-1 ) const;
151 
152  template <class T_arg>
153  std::shared_ptr<SignalProxy<T_arg >> create_signal( const std::string& sig_name ) {
154  std::shared_ptr< SignalProxy<T_arg> > sig;
156  .set_path( this->path() )
157  .set_interface( name() )
158  .set_member( sig_name )
159  .as_signal_match();
160  sig = SignalProxy<T_arg>::create( match );
161  this->add_signal( sig );
162  return sig;
163  }
164 
165  const Signals& signals() const;
166 
167  std::shared_ptr<SignalProxyBase> signal( const std::string& signame );
168 
169  bool add_signal( std::shared_ptr<SignalProxyBase> sig );
170 
171  bool remove_signal( const std::string& signame );
172 
173  bool remove_signal( std::shared_ptr<SignalProxyBase> sig );
174 
175  bool has_signal( const std::string& signame ) const;
176 
177  bool has_signal( std::shared_ptr<SignalProxyBase> sig ) const;
178 
179 private:
180  void on_object_set_path( const std::string& path );
181 
182  void set_object( ObjectProxy* obj );
183 
184  void property_updated( std::string,std::map<std::string,DBus::Variant>,std::vector<std::string> );
185 
186 private:
187  class priv_data;
188 
189  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
190 
191  friend class ObjectProxy;
192 };
193 
194 }
195 
196 #endif
An InterfaceProxy represents a remote Interface in another application on the DBus.
Definition: interfaceproxy.h:43
std::shared_ptr< SignalProxy< T_arg > > create_signal(const std::string &sig_name)
Definition: interfaceproxy.h:153
void cache_properties()
Ask the remote object for the status of all of its properties.
Definition: interfaceproxy.cpp:402
static std::shared_ptr< InterfaceProxy > create(const std::string &name=std::string())
Definition: interfaceproxy.cpp:46
std::shared_ptr< SignalProxyBase > signal(const std::string &signame)
Definition: interfaceproxy.cpp:237
std::shared_ptr< CallMessage > create_call_message(const std::string &method_name) const
Definition: interfaceproxy.cpp:215
bool has_method(const std::string &name) const
True if the interface has a method with the given name.
Definition: interfaceproxy.cpp:182
bool remove_signal(const std::string &signame)
Definition: interfaceproxy.cpp:267
bool has_property(const std::string &name) const
True if the interface has a property with the given name.
Definition: interfaceproxy.cpp:332
void remove_property(const std::string &name)
Removes the property with the given name.
Definition: interfaceproxy.cpp:365
bool add_property(std::shared_ptr< PropertyProxyBase > property)
Adds the given property.
Definition: interfaceproxy.cpp:313
std::multimap< std::string, std::shared_ptr< MethodProxyBase > > Methods
Definition: interfaceproxy.h:48
std::shared_ptr< PropertyProxy< T_type > > create_property(const std::string &name, PropertyUpdateType update=PropertyUpdateType::Updates)
Create a property with the given type, and the given name.
Definition: interfaceproxy.h:123
void remove_method(const std::string &name)
Removes the method with the given name.
Definition: interfaceproxy.cpp:145
std::shared_ptr< const ReturnMessage > call(std::shared_ptr< const CallMessage >, int timeout_milliseconds=-1) const
Definition: interfaceproxy.cpp:221
const std::map< std::string, std::shared_ptr< PropertyProxyBase > > & properties() const
Get the (local) cache of all properties.
Definition: interfaceproxy.cpp:298
const Methods & methods() const
Definition: interfaceproxy.cpp:111
const std::string & name() const
Definition: interfaceproxy.cpp:107
InterfaceProxy(const std::string &name)
Definition: interfaceproxy.cpp:42
bool add_method(std::shared_ptr< MethodProxyBase > method)
Adds the named method.
Definition: interfaceproxy.cpp:126
void property_updated(std::string, std::map< std::string, DBus::Variant >, std::vector< std::string >)
Definition: interfaceproxy.cpp:423
std::weak_ptr< Connection > connection() const
Definition: interfaceproxy.cpp:101
std::set< std::shared_ptr< SignalProxyBase > > Signals
Definition: interfaceproxy.h:50
bool has_signal(const std::string &signame) const
Definition: interfaceproxy.cpp:280
ObjectProxy * object() const
Definition: interfaceproxy.cpp:61
virtual ~InterfaceProxy()
Definition: interfaceproxy.cpp:50
void set_object(ObjectProxy *obj)
Definition: interfaceproxy.cpp:65
bool add_signal(std::shared_ptr< SignalProxyBase > sig)
Definition: interfaceproxy.cpp:245
std::shared_ptr< MethodProxyBase > method(const std::string &name) const
Returns the first method with the given name.
Definition: interfaceproxy.cpp:115
std::shared_ptr< MethodProxy< T_type > > create_method(const std::string &name)
Definition: interfaceproxy.h:70
void on_object_set_path(const std::string &path)
Definition: interfaceproxy.cpp:292
const Signals & signals() const
Definition: interfaceproxy.cpp:233
Path path() const
Definition: interfaceproxy.cpp:95
std::shared_ptr< PropertyProxyBase > property(const std::string &name) const
Definition: interfaceproxy.cpp:302
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
MatchRuleBuilder & set_interface(const std::string &interface_name)
Definition: matchrule.cpp:43
MatchRuleBuilder & set_path(const std::string &path)
Definition: matchrule.cpp:38
SignalMatchRule as_signal_match()
Definition: matchrule.cpp:63
static MatchRuleBuilder create()
Definition: matchrule.cpp:87
MatchRuleBuilder & set_member(const std::string &member)
Definition: matchrule.cpp:48
Definition: interfaceproxy.h:30
Object proxies are local proxies that provide local methods and signals for remote objects with dbus ...
Definition: objectproxy.h:44
Represents a DBus Path.
Definition: path.h:21
static std::shared_ptr< PropertyProxy< T_type > > create(std::string name, PropertyUpdateType update)
Definition: propertyproxy.h:101
A special MatchRule for signals.
Definition: matchrule.h:45
Definition: signalproxy.h:26
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
PropertyUpdateType
Definition: enums.h:23
@ Updates
When this property changes, the PropertyChanged signal will be emitted with the new value.