1 #pragma once
2 
3 #include <systemd/sd-bus.h>
4 
5 #include <sdbusplus/sdbus.hpp>
6 
7 #include <memory>
8 
9 namespace sdbusplus
10 {
11 
12 namespace slot
13 {
14 
15 using slotp_t = sd_bus_slot*;
16 struct slot;
17 
18 namespace details
19 {
20 
21 /** @brief unique_ptr functor to release a slot reference. */
22 struct SlotDeleter
23 {
SlotDeletersdbusplus::slot::details::SlotDeleter24     explicit SlotDeleter(SdBusInterface* intf) : intf(intf) {}
25 
operator ()sdbusplus::slot::details::SlotDeleter26     void operator()(slotp_t ptr) const
27     {
28         intf->sd_bus_slot_unref(ptr);
29     }
30 
31     SdBusInterface* intf;
32 };
33 
34 using slot = std::unique_ptr<sd_bus_slot, SlotDeleter>;
35 
36 struct slot_friend;
37 
38 } // namespace details
39 
40 /** @class slot
41  *  @brief Provides C++ holder for sd_bus_slot instances.
42  */
43 struct slot
44 {
45     /** @brief Empty (unused) slot */
slotsdbusplus::slot::slot46     slot() : _slot(nullptr, details::SlotDeleter(&sdbus_impl)) {}
slotsdbusplus::slot::slot47     explicit slot(std::nullptr_t) : slot() {}
48 
49     /** @brief Conversion constructor for 'slotp_t'.
50      *
51      *  Takes ownership of the slot-pointer and releases it when done.
52      */
slotsdbusplus::slot::slot53     slot(slotp_t s, sdbusplus::SdBusInterface* intf) :
54         _slot(s, details::SlotDeleter(intf))
55     {}
56 
57     /** @brief Release ownership of the stored slot-pointer. */
releasesdbusplus::slot::slot58     slotp_t release()
59     {
60         return _slot.release();
61     }
62 
63     /** @brief Check if slot contains a real pointer. (non-nullptr). */
operator boolsdbusplus::slot::slot64     explicit operator bool() const
65     {
66         return bool(_slot);
67     }
68 
69     friend struct details::slot_friend;
70 
71   private:
getsdbusplus::slot::slot72     slotp_t get() noexcept
73     {
74         return _slot.get();
75     }
76     details::slot _slot;
77 };
78 
79 namespace details
80 {
81 
82 // Some sdbusplus classes need to be able to pass the underlying slot pointer
83 // along to sd_bus calls, but we don't want to make it available for everyone.
84 // Define a class which can be inherited explicitly (intended for internal users
85 // only) to get the underlying bus pointer.
86 struct slot_friend
87 {
get_slotpsdbusplus::slot::details::slot_friend88     static slotp_t get_slotp(sdbusplus::slot::slot& s) noexcept
89     {
90         return s.get();
91     }
92 };
93 
94 } // namespace details
95 
96 } // namespace slot
97 
98 using slot_t = slot::slot;
99 
100 } // namespace sdbusplus
101