10a816c5dSWilliam A. Kennington III #pragma once 20a816c5dSWilliam A. Kennington III 3e0f1d199SWilliam A. Kennington III #include <cerrno> 465db863eSWilliam A. Kennington III #include <cstdint> 5e0f1d199SWilliam A. Kennington III #include <cstdio> 648c42751SWilliam A. Kennington III #include <functional> 7715c72f8SWilliam A. Kennington III #include <sdeventplus/event.hpp> 8e0f1d199SWilliam A. Kennington III #include <sdeventplus/internal/utils.hpp> 9b555c4e6SWilliam A. Kennington III #include <stdplus/handle/managed.hpp> 100a816c5dSWilliam A. Kennington III #include <systemd/sd-bus.h> 110a816c5dSWilliam A. Kennington III #include <type_traits> 12*1e125d97SWilliam A. Kennington III #include <utility> 130a816c5dSWilliam A. Kennington III 140a816c5dSWilliam A. Kennington III namespace sdeventplus 150a816c5dSWilliam A. Kennington III { 160a816c5dSWilliam A. Kennington III namespace source 170a816c5dSWilliam A. Kennington III { 180a816c5dSWilliam A. Kennington III 198fd0cd4cSWilliam A. Kennington III /** @class Enabled 208fd0cd4cSWilliam A. Kennington III * @brief Mapping of sdeventplus source enable values to the sd-event 218fd0cd4cSWilliam A. Kennington III * equivalent 228fd0cd4cSWilliam A. Kennington III */ 238fd0cd4cSWilliam A. Kennington III enum class Enabled 248fd0cd4cSWilliam A. Kennington III { 258fd0cd4cSWilliam A. Kennington III Off = SD_EVENT_OFF, 268fd0cd4cSWilliam A. Kennington III On = SD_EVENT_ON, 278fd0cd4cSWilliam A. Kennington III OneShot = SD_EVENT_ONESHOT, 288fd0cd4cSWilliam A. Kennington III }; 298fd0cd4cSWilliam A. Kennington III 303c845caaSWilliam A. Kennington III /** @class Base 313c845caaSWilliam A. Kennington III * @brief The base class for all sources implementing common source methods 323c845caaSWilliam A. Kennington III * Not instantiated directly by end users 333c845caaSWilliam A. Kennington III */ 340a816c5dSWilliam A. Kennington III class Base 350a816c5dSWilliam A. Kennington III { 360a816c5dSWilliam A. Kennington III public: 3748c42751SWilliam A. Kennington III using Callback = std::function<void(Base& source)>; 3848c42751SWilliam A. Kennington III 3965db863eSWilliam A. Kennington III virtual ~Base(); 4065db863eSWilliam A. Kennington III 413c845caaSWilliam A. Kennington III /** @brief Gets the underlying sd_event_source 423c845caaSWilliam A. Kennington III * 433c845caaSWilliam A. Kennington III * @return The sd_event_source 443c845caaSWilliam A. Kennington III */ 454863b96bSWilliam A. Kennington III sd_event_source* get() const; 463c845caaSWilliam A. Kennington III 473c845caaSWilliam A. Kennington III /** @brief Gets the associated Event object 483c845caaSWilliam A. Kennington III * 493c845caaSWilliam A. Kennington III * @return The Event 503c845caaSWilliam A. Kennington III */ 51715c72f8SWilliam A. Kennington III const Event& get_event() const; 52715c72f8SWilliam A. Kennington III 533c845caaSWilliam A. Kennington III /** @brief Gets the description of the source 543c845caaSWilliam A. Kennington III * 553c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 563c845caaSWilliam A. Kennington III * @return The c-string description or a nullptr if none exists 573c845caaSWilliam A. Kennington III */ 58d9fd9815SWilliam A. Kennington III const char* get_description() const; 593c845caaSWilliam A. Kennington III 603c845caaSWilliam A. Kennington III /** @brief Sets the description of the source 613c845caaSWilliam A. Kennington III * 623c845caaSWilliam A. Kennington III * @param[in] description - The c-string description 633c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 643c845caaSWilliam A. Kennington III */ 65d9fd9815SWilliam A. Kennington III void set_description(const char* description) const; 663c845caaSWilliam A. Kennington III 673c845caaSWilliam A. Kennington III /** @brief Sets the callback associated with the source to be performed 683c845caaSWilliam A. Kennington III * before the event loop goes to sleep, waiting for new events 693c845caaSWilliam A. Kennington III * 703c845caaSWilliam A. Kennington III * @param[in] callback - Function run for preparation of the source 713c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 723c845caaSWilliam A. Kennington III */ 7348c42751SWilliam A. Kennington III void set_prepare(Callback&& callback); 743c845caaSWilliam A. Kennington III 753c845caaSWilliam A. Kennington III /** @brief Whether or not the source has any pending events that have 763c845caaSWilliam A. Kennington III * not been dispatched yet. 773c845caaSWilliam A. Kennington III * 783c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 793c845caaSWilliam A. Kennington III * @return 'true' if the source has pending events 803c845caaSWilliam A. Kennington III * 'false' otherwise 813c845caaSWilliam A. Kennington III */ 828fd0cd4cSWilliam A. Kennington III bool get_pending() const; 833c845caaSWilliam A. Kennington III 843c845caaSWilliam A. Kennington III /** @brief Gets the priority of the source relative to other sources 853c845caaSWilliam A. Kennington III * The lower the priority the more important the source 863c845caaSWilliam A. Kennington III * 873c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 883c845caaSWilliam A. Kennington III * @return A 64 bit integer representing the dispatch priority 893c845caaSWilliam A. Kennington III */ 90d9fd9815SWilliam A. Kennington III int64_t get_priority() const; 913c845caaSWilliam A. Kennington III 923c845caaSWilliam A. Kennington III /** @brief Sets the priority of the source relative to other sources 933c845caaSWilliam A. Kennington III * The lower the priority the more important the source 943c845caaSWilliam A. Kennington III * 953c845caaSWilliam A. Kennington III * @param[in] priority - A 64 bit integer representing the priority 963c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 973c845caaSWilliam A. Kennington III */ 98d9fd9815SWilliam A. Kennington III void set_priority(int64_t priority) const; 993c845caaSWilliam A. Kennington III 1003c845caaSWilliam A. Kennington III /** @brief Determines the enablement value of the source 1013c845caaSWilliam A. Kennington III * 1023c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 1033c845caaSWilliam A. Kennington III * @return The enabled status of the source 1043c845caaSWilliam A. Kennington III */ 1058fd0cd4cSWilliam A. Kennington III Enabled get_enabled() const; 1063c845caaSWilliam A. Kennington III 1073c845caaSWilliam A. Kennington III /** @brief Sets the enablement value of the source 1083c845caaSWilliam A. Kennington III * 1093c845caaSWilliam A. Kennington III * @param[in] enabled - The new state of the source 1103c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 1113c845caaSWilliam A. Kennington III */ 1128fd0cd4cSWilliam A. Kennington III void set_enabled(Enabled enabled) const; 11365db863eSWilliam A. Kennington III 1140a816c5dSWilliam A. Kennington III protected: 115cc6b12beSWilliam A. Kennington III Event event; 1160a816c5dSWilliam A. Kennington III 117bc4e4569SWilliam A. Kennington III /** @brief Constructs a basic event source wrapper 118bc4e4569SWilliam A. Kennington III * Owns the passed reference to the source 119bc4e4569SWilliam A. Kennington III * This ownership is exception safe and will properly free the 120bc4e4569SWilliam A. Kennington III * source in the case of an exception during construction 121bc4e4569SWilliam A. Kennington III * 122bc4e4569SWilliam A. Kennington III * @param[in] event - The event associated with the source 123bc4e4569SWilliam A. Kennington III * @param[in] source - The underlying sd_event_source wrapped 124bc4e4569SWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 125bc4e4569SWilliam A. Kennington III */ 126715c72f8SWilliam A. Kennington III Base(const Event& event, sd_event_source* source, std::false_type); 12748c42751SWilliam A. Kennington III 128cc6b12beSWilliam A. Kennington III // We can't ever copy an event_source because the callback 129cc6b12beSWilliam A. Kennington III // data has to be unique. 130cc6b12beSWilliam A. Kennington III Base(const Base& other) = delete; 131cc6b12beSWilliam A. Kennington III Base& operator=(const Base& other) = delete; 132cc6b12beSWilliam A. Kennington III // We don't want to allow any kind of slicing. 1332d943eadSWilliam A. Kennington III Base(Base&& other); 134cc6b12beSWilliam A. Kennington III Base& operator=(Base&& other); 135cc6b12beSWilliam A. Kennington III 136bc4e4569SWilliam A. Kennington III /** @brief Returns a reference to the prepare callback executed for this 137bc4e4569SWilliam A. Kennington III * source 138bc4e4569SWilliam A. Kennington III * 139bc4e4569SWilliam A. Kennington III * @return A reference to the callback, this should be checked to make sure 140bc4e4569SWilliam A. Kennington III * the callback is valid as there is no guarantee 141bc4e4569SWilliam A. Kennington III */ 142e0f1d199SWilliam A. Kennington III const Callback& get_prepare() const; 143e0f1d199SWilliam A. Kennington III 144bc4e4569SWilliam A. Kennington III /** @brief A helper for subclasses to trivially wrap a c++ style callback 145bc4e4569SWilliam A. Kennington III * to be called from the sd-event c library 146bc4e4569SWilliam A. Kennington III * 147bc4e4569SWilliam A. Kennington III * @param[in] name - The name of the callback for use in error messages 148bc4e4569SWilliam A. Kennington III * @param[in] source - The sd_event_source provided by sd-event 149bc4e4569SWilliam A. Kennington III * @param[in] userdata - The userdata provided by sd-event 150bc4e4569SWilliam A. Kennington III * @param[in] args... - Extra arguments to pass to the callaback 151bc4e4569SWilliam A. Kennington III * @return An negative errno on error, or 0 on success 152bc4e4569SWilliam A. Kennington III */ 153e0f1d199SWilliam A. Kennington III template <typename Callback, class Source, 154e0f1d199SWilliam A. Kennington III const Callback& (Source::*getter)() const, typename... Args> 155e0f1d199SWilliam A. Kennington III static int sourceCallback(const char* name, sd_event_source*, 156*1e125d97SWilliam A. Kennington III void* userdata, Args&&... args) 157e0f1d199SWilliam A. Kennington III { 158e0f1d199SWilliam A. Kennington III if (userdata == nullptr) 159e0f1d199SWilliam A. Kennington III { 160e0f1d199SWilliam A. Kennington III fprintf(stderr, "sdeventplus: %s: Missing userdata\n", name); 161e0f1d199SWilliam A. Kennington III return -EINVAL; 162e0f1d199SWilliam A. Kennington III } 163e0f1d199SWilliam A. Kennington III Source* source = reinterpret_cast<Source*>(userdata); 164e0f1d199SWilliam A. Kennington III return internal::performCallback(name, (source->*getter)(), 165*1e125d97SWilliam A. Kennington III std::ref(*source), 166*1e125d97SWilliam A. Kennington III std::forward<Args>(args)...); 167e0f1d199SWilliam A. Kennington III } 168e0f1d199SWilliam A. Kennington III 16948c42751SWilliam A. Kennington III private: 170e04cb03bSWilliam A. Kennington III static void drop(sd_event_source*&& source, 171e04cb03bSWilliam A. Kennington III const internal::SdEvent*& sdevent); 172e04cb03bSWilliam A. Kennington III 173b555c4e6SWilliam A. Kennington III stdplus::Managed<sd_event_source*, const internal::SdEvent*>::Handle<drop> 174e04cb03bSWilliam A. Kennington III source; 17548c42751SWilliam A. Kennington III Callback prepare; 1762d943eadSWilliam A. Kennington III 177bc4e4569SWilliam A. Kennington III /** @brief A helper used to make sure the userdata for the sd-event 178bc4e4569SWilliam A. Kennington III * callback is set to the current source c++ object 179bc4e4569SWilliam A. Kennington III * 180bc4e4569SWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 181bc4e4569SWilliam A. Kennington III */ 1822d943eadSWilliam A. Kennington III void set_userdata(); 183bc4e4569SWilliam A. Kennington III 184bc4e4569SWilliam A. Kennington III /** @brief A wrapper around the callback that can be called from sd-event 185bc4e4569SWilliam A. Kennington III * 186bc4e4569SWilliam A. Kennington III * @param[in] source - The sd_event_source associated with the call 187bc4e4569SWilliam A. Kennington III * @param[in] userdata - The provided userdata for the source 188bc4e4569SWilliam A. Kennington III * @return 0 on success or a negative errno otherwise 189bc4e4569SWilliam A. Kennington III */ 190e0f1d199SWilliam A. Kennington III static int prepareCallback(sd_event_source* source, void* userdata); 1910a816c5dSWilliam A. Kennington III }; 1920a816c5dSWilliam A. Kennington III 1930a816c5dSWilliam A. Kennington III } // namespace source 1940a816c5dSWilliam A. Kennington III } // namespace sdeventplus 195