blob: 10a1d0dd8d5554d3086c7cbb7e5c8dd59f5ff15b [file] [log] [blame]
Patrick Williams0f9ed692016-10-17 14:07:51 -05001#pragma once
2
Patrick Williams0f9ed692016-10-17 14:07:51 -05003#include <systemd/sd-bus.h>
4
Patrick Venture95269db2018-08-31 09:19:17 -07005#include <memory>
6
Patrick Williams0f9ed692016-10-17 14:07:51 -05007namespace sdbusplus
8{
9
10namespace slot
11{
12
13using slotp_t = sd_bus_slot*;
Ed Tanous8cd7a4a2019-03-13 16:41:49 -070014struct slot;
Patrick Williams0f9ed692016-10-17 14:07:51 -050015
16namespace details
17{
18
19/** @brief unique_ptr functor to release a slot reference. */
20struct SlotDeleter
21{
22 void operator()(slotp_t ptr) const
23 {
24 sd_bus_slot_unref(ptr);
25 }
26};
27
28using slot = std::unique_ptr<sd_bus_slot, SlotDeleter>;
29
Patrick Williamsf0eb6502021-11-22 07:27:19 -060030struct slot_friend;
31
Patrick Williams0f9ed692016-10-17 14:07:51 -050032} // namespace details
33
34/** @class slot
35 * @brief Provides C++ holder for sd_bus_slot instances.
36 */
37struct slot
38{
Andrew Geissler072da3e2018-01-18 07:21:42 -080039 /* Define all of the basic class operations:
40 * Not allowed:
Andrew Geissler072da3e2018-01-18 07:21:42 -080041 * - Copy operations due to internal unique_ptr.
42 * Allowed:
Patrick Williamsa7ac3132021-11-19 14:48:22 -060043 * - Default constructor, assigned as a nullptr.
Andrew Geissler072da3e2018-01-18 07:21:42 -080044 * - Move operations.
45 * - Destructor.
46 */
Patrick Williams0f9ed692016-10-17 14:07:51 -050047 slot(const slot&) = delete;
48 slot& operator=(const slot&) = delete;
49 slot(slot&&) = default;
50 slot& operator=(slot&&) = default;
51 ~slot() = default;
52
53 /** @brief Conversion constructor for 'slotp_t'.
54 *
55 * Takes ownership of the slot-pointer and releases it when done.
56 */
Patrick Williamsa7ac3132021-11-19 14:48:22 -060057 explicit slot(slotp_t&& s = nullptr) : _slot(s)
58 {
59 s = nullptr;
60 }
61
62 /** @brief Conversion from 'slotp_t'.
63 *
64 * Takes ownership of the slot-pointer and releases it when done.
65 */
66 slot& operator=(slotp_t&& s)
67 {
68 _slot.reset(s);
69 s = nullptr;
70
71 return *this;
72 }
Patrick Williams0f9ed692016-10-17 14:07:51 -050073
74 /** @brief Release ownership of the stored slot-pointer. */
Andrew Geissler072da3e2018-01-18 07:21:42 -080075 slotp_t release()
76 {
77 return _slot.release();
78 }
Patrick Williams0f9ed692016-10-17 14:07:51 -050079
80 /** @brief Check if slot contains a real pointer. (non-nullptr). */
Andrew Geissler072da3e2018-01-18 07:21:42 -080081 explicit operator bool() const
82 {
83 return bool(_slot);
84 }
Patrick Williams0f9ed692016-10-17 14:07:51 -050085
Patrick Williamsf0eb6502021-11-22 07:27:19 -060086 friend struct details::slot_friend;
87
Andrew Geissler072da3e2018-01-18 07:21:42 -080088 private:
Patrick Williamsf0eb6502021-11-22 07:27:19 -060089 slotp_t get() noexcept
90 {
91 return _slot.get();
92 }
Andrew Geissler072da3e2018-01-18 07:21:42 -080093 details::slot _slot;
Patrick Williams0f9ed692016-10-17 14:07:51 -050094};
95
Patrick Williamsf0eb6502021-11-22 07:27:19 -060096namespace details
97{
98
99// Some sdbusplus classes need to be able to pass the underlying slot pointer
100// along to sd_bus calls, but we don't want to make it available for everyone.
101// Define a class which can be inherited explicitly (intended for internal users
102// only) to get the underlying bus pointer.
103struct slot_friend
104{
105 static slotp_t get_slotp(sdbusplus::slot::slot& s) noexcept
106 {
107 return s.get();
108 }
109};
110
111} // namespace details
112
Patrick Williams0f9ed692016-10-17 14:07:51 -0500113} // namespace slot
Patrick Williamsce90da12021-11-19 11:36:18 -0600114
115using slot_t = slot::slot;
116
Patrick Williams0f9ed692016-10-17 14:07:51 -0500117} // namespace sdbusplus