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