signal.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  ***************************************************************************/
8 #include <sstream>
9 #include <dbus-cxx/utility.h>
10 #include <dbus-cxx/signalbase.h>
11 #include <dbus-cxx/headerlog.h>
12 #include <dbus-cxx/signalmessage.h>
13 
14 #ifndef DBUSCXX_DBUS_SIGNAL_H_
15 #define DBUSCXX_DBUS_SIGNAL_H_
16 
17 namespace DBus {
18 
19 template <typename... T_sig>
20 class Signal;
21 
22 template <typename T_ret, typename... T_args>
23 class Signal<T_ret( T_args... )> {};
24 
39 template <typename... T_type>
40 class Signal<void(T_type...)>
41  : public sigc::signal<void( T_type... )>, public SignalBase {
42 private:
43  Signal( const std::string& path, const std::string& interface_name, const std::string& member ):
44  SignalBase( path, interface_name, member ) {
45  m_internal_callback_connection =
46  this->connect( sigc::mem_fun( *this, &Signal::internal_callback ) );
47  }
48 
49 public:
50  static std::shared_ptr<Signal> create( const std::string& path, const std::string& interface_name, const std::string& member ) {
51  return std::shared_ptr<Signal>( new Signal( path, interface_name, member ) );
52  }
53 
54  // virtual std::shared_ptr<signal_base> clone()
55  // {
56  // return std::shared_ptr<signal_base>( new signal(*this) );
57  // }
58 
60  virtual std::string introspect( int space_depth = 0 ) const {
61  std::ostringstream sout;
62  std::string spaces;
63  DBus::priv::dbus_function_traits<std::function<void( T_type... )>> method_sig_gen;
64 
65  for( int i = 0; i < space_depth; i++ ) { spaces += " "; }
66 
67  sout << spaces << "<signal name=\"" << name() << "\">\n";
68  sout << spaces << method_sig_gen.introspect( m_arg_names, 0, spaces );
69  sout << spaces << "</signal>\n";
70  return sout.str();
71  }
72 
73  virtual std::string arg_name( size_t i ) {
74  if( i < m_arg_names.size() ) { return m_arg_names[i]; }
75 
76  return std::string();
77  }
78 
79  virtual void set_arg_name( size_t i, const std::string& name ) {
80  if( i < m_arg_names.size() ) {
81  m_arg_names[i] = name;
82  }
83  }
84 
85 protected:
86 
87  friend class Interface;
88 
89  std::vector<std::string> m_arg_names;
90 
92 
93  void internal_callback( T_type... args ) {
94  std::shared_ptr<SignalMessage> __msg = SignalMessage::create( path(), interface_name(), name() );
95  DBUSCXX_DEBUG_STDSTR( "DBus.Signal", "Sending following signal: "
96  << __msg->path()
97  << " "
98  << __msg->interface_name()
99  << " "
100  << __msg->member() );
101 
102  if( !destination().empty() ) { __msg->set_destination( destination() ); }
103 
104  ( *__msg << ... << args );
105  bool result = this->handle_dbus_outgoing( __msg );
106  DBUSCXX_DEBUG_STDSTR( "DBus.Signal", "signal::internal_callback: result=" << result );
107  }
108 };
109 
110 } /* namespace DBus */
111 
112 #endif /* _DBUS_CXX_SIGNAL_H_ */
An Interface represents a local copy of a DBus interface.
Definition: interface.h:41
Base class of all signals(proxy and adapter).
Definition: signalbase.h:35
static std::shared_ptr< SignalMessage > create()
Definition: signalmessage.cpp:31
virtual void set_arg_name(size_t i, const std::string &name)
Definition: signal.h:79
virtual std::string arg_name(size_t i)
Definition: signal.h:73
sigc::connection m_internal_callback_connection
Definition: signal.h:91
Signal(const std::string &path, const std::string &interface_name, const std::string &member)
Definition: signal.h:43
std::vector< std::string > m_arg_names
Definition: signal.h:89
void internal_callback(T_type... args)
Definition: signal.h:93
static std::shared_ptr< Signal > create(const std::string &path, const std::string &interface_name, const std::string &member)
Definition: signal.h:50
virtual std::string introspect(int space_depth=0) const
Returns a DBus XML description of this interface.
Definition: signal.h:60
Definition: signal.h:20
#define DBUSCXX_DEBUG_STDSTR(logger, message)
Definition: headerlog.h:27
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
Definition: utility.h:234