1 // Copyright 2014 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_ 6 #define LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_ 7 8 #include <string> 9 #include <typeinfo> 10 11 #include <base/bind.h> 12 #include <base/macros.h> 13 #include <brillo/brillo_export.h> 14 #include <brillo/dbus/dbus_param_writer.h> 15 #include <dbus/message.h> 16 17 namespace brillo { 18 namespace dbus_utils { 19 20 class DBusObject; 21 22 // Base class for D-Bus signal proxy classes. 23 // Used mostly to store the polymorphic DBusSignal<...> in a single map 24 // container inside DBusInterface object. 25 class BRILLO_EXPORT DBusSignalBase { 26 public: 27 DBusSignalBase(DBusObject* dbus_object, 28 const std::string& interface_name, 29 const std::string& signal_name); 30 virtual ~DBusSignalBase() = default; 31 32 protected: 33 bool SendSignal(dbus::Signal* signal) const; 34 35 std::string interface_name_; 36 std::string signal_name_; 37 38 private: 39 DBusObject* dbus_object_; 40 41 DISALLOW_COPY_AND_ASSIGN(DBusSignalBase); 42 }; 43 44 // DBusSignal<...> is a concrete signal proxy class that knows about the 45 // exact number of signal arguments and their types. 46 template<typename... Args> 47 class DBusSignal : public DBusSignalBase { 48 public: 49 // Expose the custom constructor from DBusSignalBase. 50 using DBusSignalBase::DBusSignalBase; 51 ~DBusSignal() override = default; 52 53 // DBusSignal<...>::Send(...) dispatches the signal with the given arguments. Send(const Args &...args)54 bool Send(const Args&... args) const { 55 dbus::Signal signal(interface_name_, signal_name_); 56 dbus::MessageWriter signal_writer(&signal); 57 DBusParamWriter::Append(&signal_writer, args...); 58 return SendSignal(&signal); 59 } 60 61 private: 62 DISALLOW_COPY_AND_ASSIGN(DBusSignal); 63 }; 64 65 } // namespace dbus_utils 66 } // namespace brillo 67 68 #endif // LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_ 69