blob: 27f4a7048e2c7b9c57dc318a2f6314b8bdb58cf9 [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
30} // namespace details
31
32/** @class slot
33 * @brief Provides C++ holder for sd_bus_slot instances.
34 */
35struct slot
36{
Andrew Geissler072da3e2018-01-18 07:21:42 -080037 /* Define all of the basic class operations:
38 * Not allowed:
39 * - Default constructor to avoid nullptrs.
40 * - Copy operations due to internal unique_ptr.
41 * Allowed:
42 * - Move operations.
43 * - Destructor.
44 */
Patrick Williams0f9ed692016-10-17 14:07:51 -050045 slot() = delete;
46 slot(const slot&) = delete;
47 slot& operator=(const slot&) = delete;
48 slot(slot&&) = default;
49 slot& operator=(slot&&) = default;
50 ~slot() = default;
51
52 /** @brief Conversion constructor for 'slotp_t'.
53 *
54 * Takes ownership of the slot-pointer and releases it when done.
55 */
Andrew Geissler072da3e2018-01-18 07:21:42 -080056 explicit slot(slotp_t s) : _slot(s)
Patrick Williams127b8ab2020-05-21 15:24:19 -050057 {}
Patrick Williams0f9ed692016-10-17 14:07:51 -050058
59 /** @brief Release ownership of the stored slot-pointer. */
Andrew Geissler072da3e2018-01-18 07:21:42 -080060 slotp_t release()
61 {
62 return _slot.release();
63 }
Patrick Williams0f9ed692016-10-17 14:07:51 -050064
65 /** @brief Check if slot contains a real pointer. (non-nullptr). */
Andrew Geissler072da3e2018-01-18 07:21:42 -080066 explicit operator bool() const
67 {
68 return bool(_slot);
69 }
Patrick Williams0f9ed692016-10-17 14:07:51 -050070
Andrew Geissler072da3e2018-01-18 07:21:42 -080071 private:
72 details::slot _slot;
Patrick Williams0f9ed692016-10-17 14:07:51 -050073};
74
75} // namespace slot
76} // namespace sdbusplus