blob: bda322a637e52c44b3843fe825b4894dfaa20d4c [file] [log] [blame]
Alex Vakulenkofa8f9442014-08-27 14:50:36 -07001// 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
Alex Vakulenkofed60b02015-10-27 09:53:05 -07005#ifndef LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_
6#define LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_
Alex Vakulenkofa8f9442014-08-27 14:50:36 -07007
8#include <string>
9#include <typeinfo>
10
Alex Vakulenkofa8f9442014-08-27 14:50:36 -070011#include <base/bind.h>
Alex Vakulenkof2418e52014-09-04 08:26:42 -070012#include <base/macros.h>
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070013#include <brillo/brillo_export.h>
14#include <brillo/dbus/dbus_param_writer.h>
Alex Vakulenkofa8f9442014-08-27 14:50:36 -070015#include <dbus/message.h>
16
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070017namespace brillo {
Alex Vakulenkofa8f9442014-08-27 14:50:36 -070018namespace dbus_utils {
19
20class 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.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070025class BRILLO_EXPORT DBusSignalBase {
Alex Vakulenkofa8f9442014-08-27 14:50:36 -070026 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.
46template<typename... Args>
47class 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.
54 bool Send(const Args&... args) const {
55 dbus::Signal signal(interface_name_, signal_name_);
56 dbus::MessageWriter signal_writer(&signal);
Alex Vakulenko003e3bb2014-11-09 13:21:59 -080057 DBusParamWriter::Append(&signal_writer, args...);
58 return SendSignal(&signal);
Alex Vakulenkofa8f9442014-08-27 14:50:36 -070059 }
60
61 private:
62 DISALLOW_COPY_AND_ASSIGN(DBusSignal);
63};
64
65} // namespace dbus_utils
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070066} // namespace brillo
Alex Vakulenkofa8f9442014-08-27 14:50:36 -070067
Alex Vakulenkofed60b02015-10-27 09:53:05 -070068#endif // LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_H_