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 | |
| 30 | } // namespace details |
| 31 | |
| 32 | /** @class slot |
| 33 | * @brief Provides C++ holder for sd_bus_slot instances. |
| 34 | */ |
| 35 | struct slot |
| 36 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 37 | /* 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 Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 45 | 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 Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 56 | explicit slot(slotp_t s) : _slot(s) |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 57 | {} |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 58 | |
| 59 | /** @brief Release ownership of the stored slot-pointer. */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 60 | slotp_t release() |
| 61 | { |
| 62 | return _slot.release(); |
| 63 | } |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 64 | |
| 65 | /** @brief Check if slot contains a real pointer. (non-nullptr). */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 66 | explicit operator bool() const |
| 67 | { |
| 68 | return bool(_slot); |
| 69 | } |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 70 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 71 | private: |
| 72 | details::slot _slot; |
Patrick Williams | 0f9ed69 | 2016-10-17 14:07:51 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace slot |
| 76 | } // namespace sdbusplus |