xref: /openbmc/phosphor-dbus-monitor/src/pathwatch.hpp (revision e5576bf2a145117c583c3ad05fec3a08aa90993d)
1 /**
2  * @file PathWatch.hpp
3  * @brief Add watch for the object path for interfaces added/removed signal
4  *
5  * In general class users should include pathwatchimpl.hpp instead to avoid
6  * link failures.
7  */
8 #pragma once
9 
10 #include "data_types.hpp"
11 #include "watch.hpp"
12 
13 namespace phosphor
14 {
15 namespace dbus
16 {
17 namespace monitoring
18 {
19 
20 class Callback;
21 
22 /** @class PathWatch
23  *  @brief Watch on object path for interfaceadded/interfaceremoved signals
24  */
25 template <typename DBusInterfaceType> class PathWatch : public Watch
26 {
27   public:
28     PathWatch() = delete;
29     PathWatch(const PathWatch&) = delete;
30     PathWatch(PathWatch&&) = default;
31     PathWatch& operator=(const PathWatch&) = delete;
32     PathWatch& operator=(PathWatch&&) = default;
33     virtual ~PathWatch() = default;
34     PathWatch(const std::string& path, Callback& callback) :
35         Watch(), objectPath(path), cb(callback), alreadyRan(false)
36     {
37     }
38 
39     /** @brief Start the watch.
40      *
41      *  Watch start interface implementation for PathWatch.
42      */
43     void start() override;
44 
45     /** @brief Run the watch callback method.
46      *
47      *  Watch callback interface implementation for PathWatch.
48      */
49     void callback(Context ctx) override;
50 
51     /** @brief Run the watch callback method.
52      *
53      *  Watch callback interface implementation for PathWatch.
54      */
55     void callback(Context ctx, sdbusplus::message::message& msg) override;
56 
57   protected:
58     /** @brief Path of the D-Bus object to watch for. */
59     const std::string& objectPath;
60 
61     /** @brief Optional callback method. */
62     Callback& cb;
63 
64     /** @brief The start method should only be invoked once. */
65     bool alreadyRan;
66 };
67 
68 } // namespace monitoring
69 } // namespace dbus
70 } // namespace phosphor
71