dbus-cxx
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 ***************************************************************************/
11#include <dbus-cxx/headerlog.h>
12#include <dbus-cxx/utility.h>
13#include <memory>
14#include <mutex>
15#include <string>
16#include <sstream>
18#include <sigc++/sigc++.h>
19#include <future>
20
21#ifndef DBUSCXX_METHODPROXYBASE_H
22#define DBUSCXX_METHODPROXYBASE_H
23
24namespace DBus {
25
26class InterfaceProxy;
27class PendingCall;
28class ReturnMessage;
29template <typename signature> class MethodProxy;
30
42protected:
43
44 MethodProxyBase( const std::string& name );
45
46 MethodProxyBase( const MethodProxyBase& other );
47
48public:
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
106 void enable_interactive_authorization( unsigned int timeout_milliseconds = 0 );
107
118
119 // std::shared_ptr<PendingCall> call_async( std::shared_ptr<const CallMessage>, int timeout_milliseconds=-1 ) const;
120
121private:
122 void set_interface( InterfaceProxy* proxy );
123
124private:
125 class priv_data;
126
127 DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
128
129 // Declare InterfaceProxy as a friend so that it can set the interface
130 friend class InterfaceProxy;
131};
132
133
139template<typename... T_arg>
140class MethodProxy<void( T_arg... )> : public MethodProxyBase {
141protected:
142 MethodProxy( const std::string& name ) :
143 MethodProxyBase( name ) {}
144
145public:
146 void operator()( T_arg... args ) {
147 std::ostringstream debug_str;
148 DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
149
150 debug_str << "DBus::MethodProxy<";
151 debug_str << method_sig_gen.debug_string();
152 debug_str << "> calling method=";
153 debug_str << name();
154 DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
155
156 std::shared_ptr<CallMessage> _callmsg = this->create_call_message();
157 ( *_callmsg << ... << args );
158 std::shared_ptr<const ReturnMessage> retmsg = this->call( _callmsg, -1 );
159 }
160
161 std::future<void> call_async( T_arg... args ) {
162 std::ostringstream debug_str;
163 DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
164
165 debug_str << "DBus::MethodProxy<";
166 debug_str << method_sig_gen.debug_string();
167 debug_str << "> calling async method=";
168 debug_str << name();
169 DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
170
171 return std::async( std::launch::async, *this, args... );
172 }
173
174 static std::shared_ptr<MethodProxy> create( const std::string& name ) {
175 return std::shared_ptr<MethodProxy>( new MethodProxy( name ) );
176 }
177};
178
185template<typename T_return, typename... T_arg>
186class MethodProxy<T_return( T_arg... )> : public MethodProxyBase {
187protected:
188 MethodProxy( const std::string& name ) :
189 MethodProxyBase( name ) {}
190
191public:
192 T_return operator()( T_arg... args ) {
193 std::ostringstream debug_str;
194 DBus::priv::dbus_function_traits<std::function<T_return( T_arg... )>> method_sig_gen;
195
196 debug_str << "DBus::MethodProxy<";
197 debug_str << method_sig_gen.debug_string();
198 debug_str << "> calling method=";
199 debug_str << name();
200 DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
201
202 std::shared_ptr<CallMessage> _callmsg = this->create_call_message();
203 MessageAppendIterator iter = _callmsg->append();
204 ( void )( iter << ... << args );
205 std::shared_ptr<const ReturnMessage> retmsg = this->call( _callmsg, -1 );
206 T_return _retval;
207 retmsg >> _retval;
208 return _retval;
209 }
210
211 std::future<T_return> call_async( T_arg... args ) {
212 std::ostringstream debug_str;
213 DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
214
215 debug_str << "DBus::MethodProxy<";
216 debug_str << method_sig_gen.debug_string();
217 debug_str << "> calling async method=";
218 debug_str << name();
219 DBUSCXX_DEBUG_STDSTR( "DBus.MethodProxy", debug_str.str() );
220
221 return std::async( std::launch::async, *this, args... );
222 }
223
224 static std::shared_ptr<MethodProxy> create( const std::string& name ) {
225 return std::shared_ptr<MethodProxy>( new MethodProxy( name ) );
226 }
227};
228
229}
230
231#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
void disable_interactive_authorization()
Disable interactive authorization for method call.
Definition: methodproxybase.cpp:103
void enable_interactive_authorization(unsigned int timeout_milliseconds=0)
Enable interactive authorization for method call.
Definition: methodproxybase.cpp:90
static std::shared_ptr< MethodProxyBase > create(const std::string &name)
Definition: methodproxybase.cpp:52
const std::string & name() const
Definition: methodproxybase.cpp:63
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
void set_interface(InterfaceProxy *proxy)
Definition: methodproxybase.cpp:113
InterfaceProxy * interface_name() const
Definition: methodproxybase.cpp:59
MethodProxyBase(const std::string &name)
Definition: methodproxybase.cpp:43
std::shared_ptr< const ReturnMessage > call(std::shared_ptr< const CallMessage >, int timeout_milliseconds=-1) const
Definition: methodproxybase.cpp:76
std::shared_ptr< CallMessage > create_call_message() const
Definition: methodproxybase.cpp:67
~MethodProxyBase()
Definition: methodproxybase.cpp:56
std::future< T_return > call_async(T_arg... args)
Definition: methodproxybase.h:211
MethodProxy(const std::string &name)
Definition: methodproxybase.h:188
T_return operator()(T_arg... args)
Definition: methodproxybase.h:192
static std::shared_ptr< MethodProxy > create(const std::string &name)
Definition: methodproxybase.h:224
MethodProxy(const std::string &name)
Definition: methodproxybase.h:142
void operator()(T_arg... args)
Definition: methodproxybase.h:146
std::future< void > call_async(T_arg... args)
Definition: methodproxybase.h:161
static std::shared_ptr< MethodProxy > create(const std::string &name)
Definition: methodproxybase.h:174
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:235