dbus-cxx
signalproxy.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 ***************************************************************************/
10#include <dbus-cxx/utility.h>
11#include <functional>
12#include <memory>
13#include <string>
14#include <tuple>
15#include "enums.h"
16#include "error.h"
17#include "headerlog.h"
18#include "messageiterator.h"
19#include "matchrule.h"
20#include <sigc++/sigc++.h>
21
22#ifndef DBUSCXX_SIGNALPROXYBASE_H
23#define DBUSCXX_SIGNALPROXYBASE_H
24
25namespace DBus {
26template <typename type> class SignalProxy;
27
38public:
39 virtual HandlerResult handle_signal( std::shared_ptr<const SignalMessage> );
40
41 const std::string& match_rule() const;
42
43 void update_match_rule();
44
45protected:
46 SignalProxyBase( const SignalMatchRule& matchRule );
47
48 virtual ~SignalProxyBase();
49
50 bool matches( std::shared_ptr<const SignalMessage> msg );
51
56 // virtual std::shared_ptr<signal_base> clone() = 0;
57
58 virtual HandlerResult on_dbus_incoming( std::shared_ptr<const SignalMessage> msg ) = 0;
59
60private:
61 class priv_data;
62
63 DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
64};
65
76template <typename... T_arg>
77class SignalProxy<void(T_arg...)>
78 : public sigc::signal<void(T_arg...)>, public SignalProxyBase {
79public:
80 SignalProxy( const SignalMatchRule& matchRule ):
81 SignalProxyBase( matchRule )
82 { }
83
84 static std::shared_ptr<SignalProxy> create( const SignalMatchRule& matchRule )
85 { return std::shared_ptr<SignalProxy>( new SignalProxy( matchRule ) ); }
86
87protected:
88 HandlerResult on_dbus_incoming( std::shared_ptr<const SignalMessage> msg ) {
89 std::tuple<T_arg...> tup_args;
90 std::ostringstream debug_str;
91 DBus::priv::dbus_function_traits<std::function<void( T_arg... )>> method_sig_gen;
92
93 debug_str << "DBus::signal_proxy<";
94 debug_str << method_sig_gen.debug_string();
95 debug_str << ">::on_dbus_incoming method=";
96 debug_str << msg->member();
97 DBUSCXX_DEBUG_STDSTR( "DBus.signal_proxy", debug_str.str() );
98
99 try {
100 MessageIterator i = msg->begin();
101 std::apply( [i]( auto&& ...arg ) mutable {
102 ( void )( i >> ... >> arg );
103 },
104 tup_args );
105 std::apply( &SignalProxy::emit, std::tuple_cat( std::make_tuple( this ), tup_args ) );
106 } catch( ErrorInvalidTypecast& e ) {
107 DBUSCXX_DEBUG_STDSTR( "DBus.signal_proxy", "Caught error invalid typecast" );
109 } catch( ... ) {
110 DBUSCXX_DEBUG_STDSTR( "DBus.signal_proxy", "Unknown exception" );
112 }
113
115 }
116
117};
118
119
120}
121
122#endif
Definition: error.h:341
Extraction iterator allowing values to be retrieved from a message.
Definition: messageiterator.h:56
Base class of all signals(proxy and adapter).
Definition: signalbase.h:35
A special MatchRule for signals.
Definition: matchrule.h:45
Base class for a signal proxy that allows you to listen for signals.
Definition: signalproxy.h:37
bool matches(std::shared_ptr< const SignalMessage > msg)
Definition: signalproxy.cpp:41
SignalProxyBase(const SignalMatchRule &matchRule)
Definition: signalproxy.cpp:22
virtual HandlerResult on_dbus_incoming(std::shared_ptr< const SignalMessage > msg)=0
This method is needed to be able to create a duplicate of a child capable of parsing their specific t...
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
void update_match_rule()
Definition: signalproxy.cpp:57
virtual ~SignalProxyBase()
Definition: signalproxy.cpp:28
virtual HandlerResult handle_signal(std::shared_ptr< const SignalMessage >)
Definition: signalproxy.cpp:31
const std::string & match_rule() const
Definition: signalproxy.cpp:37
SignalProxy(const SignalMatchRule &matchRule)
Definition: signalproxy.h:80
static std::shared_ptr< SignalProxy > create(const SignalMatchRule &matchRule)
Definition: signalproxy.h:84
HandlerResult on_dbus_incoming(std::shared_ptr< const SignalMessage > msg)
Definition: signalproxy.h:88
Definition: signalproxy.h:26
#define DBUSCXX_DEBUG_STDSTR(logger, message)
Definition: headerlog.h:27
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
HandlerResult
Definition: enums.h:95
@ Not_Handled
This message was not handled for some reason.
@ Handled
This message was handled appropriately.
Definition: utility.h:235