methodproxybase.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/callmessage.h>
11 #include <dbus-cxx/headerlog.h>
12 #include <dbus-cxx/utility.h>
13 #include <memory>
14 #include <mutex>
15 #include <string>
16 #include <sstream>
17 #include "messageappenditerator.h"
18 #include <sigc++/sigc++.h>
19 #include <future>
20 
21 #ifndef DBUSCXX_METHODPROXYBASE_H
22 #define DBUSCXX_METHODPROXYBASE_H
23 
24 namespace DBus {
25 
26 class InterfaceProxy;
27 class PendingCall;
28 class ReturnMessage;
29 template <typename signature> class MethodProxy;
30 
42 protected:
43 
44  MethodProxyBase( const std::string& name );
45 
46  MethodProxyBase( const MethodProxyBase& other );
47 
48 public:
49  static std::shared_ptr<MethodProxyBase> create( const std::string& name );
50 
52 
54 
55  const std::string& name() const;
56 
57  std::shared_ptr<CallMessage> create_call_message( ) const;
58 
59  std::shared_ptr<const ReturnMessage> call( std::shared_ptr<const CallMessage>, int timeout_milliseconds = -1 ) const;
60 
61  // std::shared_ptr<PendingCall> call_async( std::shared_ptr<const CallMessage>, int timeout_milliseconds=-1 ) const;
62 
63 private:
64  void set_interface( InterfaceProxy* proxy );
65 
66 private:
67  class priv_data;
68 
69  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
70 
71  // Declare InterfaceProxy as a friend so that it can set the interface
72  friend class InterfaceProxy;
73 };
74 
75 
81 template<typename... T_arg>
82 class MethodProxy<void( T_arg... )> : public MethodProxyBase {
83 protected:
84  MethodProxy( const std::string& name ) :
85  MethodProxyBase( name ) {}
86 
87 public:
88  void operator()( T_arg... args ) {
89  std::ostringstream debug_str;
90  DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
91 
92  debug_str << "DBus::MethodProxy<";
93  debug_str << method_sig_gen.debug_string();
94  debug_str << "> calling method=";
95  debug_str << name();
96  DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
97 
98  std::shared_ptr<CallMessage> _callmsg = this->create_call_message();
99  ( *_callmsg << ... << args );
100  std::shared_ptr<const ReturnMessage> retmsg = this->call( _callmsg, -1 );
101  }
102 
103  std::future<void> call_async( T_arg... args ) {
104  std::ostringstream debug_str;
105  DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
106 
107  debug_str << "DBus::MethodProxy<";
108  debug_str << method_sig_gen.debug_string();
109  debug_str << "> calling async method=";
110  debug_str << name();
111  DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
112 
113  return std::async( std::launch::async, *this, args... );
114  }
115 
116  static std::shared_ptr<MethodProxy> create( const std::string& name ) {
117  return std::shared_ptr<MethodProxy>( new MethodProxy( name ) );
118  }
119 };
120 
127 template<typename T_return, typename... T_arg>
128 class MethodProxy<T_return( T_arg... )> : public MethodProxyBase {
129 protected:
130  MethodProxy( const std::string& name ) :
131  MethodProxyBase( name ) {}
132 
133 public:
134  T_return operator()( T_arg... args ) {
135  std::ostringstream debug_str;
136  DBus::priv::dbus_function_traits<std::function<T_return( T_arg... )>> method_sig_gen;
137 
138  debug_str << "DBus::MethodProxy<";
139  debug_str << method_sig_gen.debug_string();
140  debug_str << "> calling method=";
141  debug_str << name();
142  DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
143 
144  std::shared_ptr<CallMessage> _callmsg = this->create_call_message();
145  MessageAppendIterator iter = _callmsg->append();
146  ( void )( iter << ... << args );
147  std::shared_ptr<const ReturnMessage> retmsg = this->call( _callmsg, -1 );
148  T_return _retval;
149  retmsg >> _retval;
150  return _retval;
151  }
152 
153  std::future<T_return> call_async( T_arg... args ) {
154  std::ostringstream debug_str;
155  DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
156 
157  debug_str << "DBus::MethodProxy<";
158  debug_str << method_sig_gen.debug_string();
159  debug_str << "> calling async method=";
160  debug_str << name();
161  DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
162 
163  return std::async( std::launch::async, *this, args... );
164  }
165 
166  static std::shared_ptr<MethodProxy> create( const std::string& name ) {
167  return std::shared_ptr<MethodProxy>( new MethodProxy( name ) );
168  }
169 };
170 
171 }
172 
173 #endif
An InterfaceProxy represents a remote Interface in another application on the DBus.
Definition: interfaceproxy.h:43
Insertion iterator allow values to be appended to a message.
Definition: messageappenditerator.h:38
Base class for all proxy(remote) methods.
Definition: methodproxybase.h:41
static std::shared_ptr< MethodProxyBase > create(const std::string &name)
Definition: methodproxybase.cpp:37
const std::string & name() const
Definition: methodproxybase.cpp:48
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
void set_interface(InterfaceProxy *proxy)
Definition: methodproxybase.cpp:72
InterfaceProxy * interface_name() const
Definition: methodproxybase.cpp:44
MethodProxyBase(const std::string &name)
Definition: methodproxybase.cpp:28
std::shared_ptr< const ReturnMessage > call(std::shared_ptr< const CallMessage >, int timeout_milliseconds=-1) const
Definition: methodproxybase.cpp:60
std::shared_ptr< CallMessage > create_call_message() const
Definition: methodproxybase.cpp:52
~MethodProxyBase()
Definition: methodproxybase.cpp:41
MethodProxy(const std::string &name)
Definition: methodproxybase.h:130
T_return operator()(T_arg... args)
Definition: methodproxybase.h:134
static std::shared_ptr< MethodProxy > create(const std::string &name)
Definition: methodproxybase.h:166
std::future< T_return > call_async(T_arg... args)
Definition: methodproxybase.h:153
MethodProxy(const std::string &name)
Definition: methodproxybase.h:84
void operator()(T_arg... args)
Definition: methodproxybase.h:88
std::future< void > call_async(T_arg... args)
Definition: methodproxybase.h:103
static std::shared_ptr< MethodProxy > create(const std::string &name)
Definition: methodproxybase.h:116
Definition: interfaceproxy.h:30
#define DBUSCXX_DEBUG_STDSTR(logger, message)
Definition: headerlog.h:27
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
Definition: utility.h:234