Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 3 | #include <systemd/sd-bus.h> |
| 4 | |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 7 | namespace sdbusplus |
| 8 | { |
| 9 | |
| 10 | namespace slot |
| 11 | { |
| 12 | |
| 13 | using slotp_t = sd_bus_slot*; |
Ed Tanous | 8cd7a4a | 2019-03-13 16:41:49 -0700 | [diff] [blame] | 14 | struct slot; |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 15 | |
| 16 | namespace details |
| 17 | { |
| 18 | |
| 19 | /** @brief unique_ptr functor to release a slot reference. */ |
| 20 | struct SlotDeleter |
| 21 | { |
| 22 | void operator()(slotp_t ptr) const |
| 23 | { |
| 24 | sd_bus_slot_unref(ptr); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | using slot = std::unique_ptr<sd_bus_slot, SlotDeleter>; |
| 29 | |
Patrick Williams | f0eb650 | 2021-11-22 07:27:19 -0600 | [diff] [blame] | 30 | struct slot_friend; |
| 31 | |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 32 | } // namespace details |
| 33 | |
| 34 | /** @class slot |
| 35 | * @brief Provides C++ holder for sd_bus_slot instances. |
| 36 | */ |
| 37 | struct slot |
| 38 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 39 | /* Define all of the basic class operations: |
| 40 | * Not allowed: |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 41 | * - Copy operations due to internal unique_ptr. |
| 42 | * Allowed: |
Patrick Williams | a7ac313 | 2021-11-19 14:48:22 -0600 | [diff] [blame] | 43 | * - Default constructor, assigned as a nullptr. |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 44 | * - Move operations. |
| 45 | * - Destructor. |
| 46 | */ |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 47 | 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 Williams | a7ac313 | 2021-11-19 14:48:22 -0600 | [diff] [blame] | 57 | 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 Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 73 | |
| 74 | /** @brief Release ownership of the stored slot-pointer. */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 75 | slotp_t release() |
| 76 | { |
| 77 | return _slot.release(); |
| 78 | } |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 79 | |
| 80 | /** @brief Check if slot contains a real pointer. (non-nullptr). */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 81 | explicit operator bool() const |
| 82 | { |
| 83 | return bool(_slot); |
| 84 | } |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 85 | |
Patrick Williams | f0eb650 | 2021-11-22 07:27:19 -0600 | [diff] [blame] | 86 | friend struct details::slot_friend; |
| 87 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 88 | private: |
Patrick Williams | f0eb650 | 2021-11-22 07:27:19 -0600 | [diff] [blame] | 89 | slotp_t get() noexcept |
| 90 | { |
| 91 | return _slot.get(); |
| 92 | } |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 93 | details::slot _slot; |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
Patrick Williams | f0eb650 | 2021-11-22 07:27:19 -0600 | [diff] [blame] | 96 | namespace 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. |
| 103 | struct 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 Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 113 | } // namespace slot |
Patrick Williams | ce90da1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 114 | |
| 115 | using slot_t = slot::slot; |
| 116 | |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 117 | } // namespace sdbusplus |