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> 9*b555c4e6SWilliam A. Kennington III #include <stdplus/handle/managed.hpp> 100a816c5dSWilliam A. Kennington III #include <systemd/sd-bus.h> 110a816c5dSWilliam A. Kennington III #include <type_traits> 120a816c5dSWilliam A. Kennington III 130a816c5dSWilliam A. Kennington III namespace sdeventplus 140a816c5dSWilliam A. Kennington III { 150a816c5dSWilliam A. Kennington III namespace source 160a816c5dSWilliam A. Kennington III { 170a816c5dSWilliam A. Kennington III 188fd0cd4cSWilliam A. Kennington III /** @class Enabled 198fd0cd4cSWilliam A. Kennington III * @brief Mapping of sdeventplus source enable values to the sd-event 208fd0cd4cSWilliam A. Kennington III * equivalent 218fd0cd4cSWilliam A. Kennington III */ 228fd0cd4cSWilliam A. Kennington III enum class Enabled 238fd0cd4cSWilliam A. Kennington III { 248fd0cd4cSWilliam A. Kennington III Off = SD_EVENT_OFF, 258fd0cd4cSWilliam A. Kennington III On = SD_EVENT_ON, 268fd0cd4cSWilliam A. Kennington III OneShot = SD_EVENT_ONESHOT, 278fd0cd4cSWilliam A. Kennington III }; 288fd0cd4cSWilliam A. Kennington III 293c845caaSWilliam A. Kennington III /** @class Base 303c845caaSWilliam A. Kennington III * @brief The base class for all sources implementing common source methods 313c845caaSWilliam A. Kennington III * Not instantiated directly by end users 323c845caaSWilliam A. Kennington III */ 330a816c5dSWilliam A. Kennington III class Base 340a816c5dSWilliam A. Kennington III { 350a816c5dSWilliam A. Kennington III public: 3648c42751SWilliam A. Kennington III using Callback = std::function<void(Base& source)>; 3748c42751SWilliam A. Kennington III 3865db863eSWilliam A. Kennington III virtual ~Base(); 3965db863eSWilliam A. Kennington III 403c845caaSWilliam A. Kennington III /** @brief Gets the underlying sd_event_source 413c845caaSWilliam A. Kennington III * 423c845caaSWilliam A. Kennington III * @return The sd_event_source 433c845caaSWilliam A. Kennington III */ 444863b96bSWilliam A. Kennington III sd_event_source* get() const; 453c845caaSWilliam A. Kennington III 463c845caaSWilliam A. Kennington III /** @brief Gets the associated Event object 473c845caaSWilliam A. Kennington III * 483c845caaSWilliam A. Kennington III * @return The Event 493c845caaSWilliam A. Kennington III */ 50715c72f8SWilliam A. Kennington III const Event& get_event() const; 51715c72f8SWilliam A. Kennington III 523c845caaSWilliam A. Kennington III /** @brief Gets the description of the source 533c845caaSWilliam A. Kennington III * 543c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 553c845caaSWilliam A. Kennington III * @return The c-string description or a nullptr if none exists 563c845caaSWilliam A. Kennington III */ 57d9fd9815SWilliam A. Kennington III const char* get_description() const; 583c845caaSWilliam A. Kennington III 593c845caaSWilliam A. Kennington III /** @brief Sets the description of the source 603c845caaSWilliam A. Kennington III * 613c845caaSWilliam A. Kennington III * @param[in] description - The c-string description 623c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 633c845caaSWilliam A. Kennington III */ 64d9fd9815SWilliam A. Kennington III void set_description(const char* description) const; 653c845caaSWilliam A. Kennington III 663c845caaSWilliam A. Kennington III /** @brief Sets the callback associated with the source to be performed 673c845caaSWilliam A. Kennington III * before the event loop goes to sleep, waiting for new events 683c845caaSWilliam A. Kennington III * 693c845caaSWilliam A. Kennington III * @param[in] callback - Function run for preparation of the source 703c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 713c845caaSWilliam A. Kennington III */ 7248c42751SWilliam A. Kennington III void set_prepare(Callback&& callback); 733c845caaSWilliam A. Kennington III 743c845caaSWilliam A. Kennington III /** @brief Whether or not the source has any pending events that have 753c845caaSWilliam A. Kennington III * not been dispatched yet. 763c845caaSWilliam A. Kennington III * 773c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 783c845caaSWilliam A. Kennington III * @return 'true' if the source has pending events 793c845caaSWilliam A. Kennington III * 'false' otherwise 803c845caaSWilliam A. Kennington III */ 818fd0cd4cSWilliam A. Kennington III bool get_pending() const; 823c845caaSWilliam A. Kennington III 833c845caaSWilliam A. Kennington III /** @brief Gets the priority of the source relative to other sources 843c845caaSWilliam A. Kennington III * The lower the priority the more important the source 853c845caaSWilliam A. Kennington III * 863c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 873c845caaSWilliam A. Kennington III * @return A 64 bit integer representing the dispatch priority 883c845caaSWilliam A. Kennington III */ 89d9fd9815SWilliam A. Kennington III int64_t get_priority() const; 903c845caaSWilliam A. Kennington III 913c845caaSWilliam A. Kennington III /** @brief Sets the priority of the source relative to other sources 923c845caaSWilliam A. Kennington III * The lower the priority the more important the source 933c845caaSWilliam A. Kennington III * 943c845caaSWilliam A. Kennington III * @param[in] priority - A 64 bit integer representing the priority 953c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 963c845caaSWilliam A. Kennington III */ 97d9fd9815SWilliam A. Kennington III void set_priority(int64_t priority) const; 983c845caaSWilliam A. Kennington III 993c845caaSWilliam A. Kennington III /** @brief Determines the enablement value of the source 1003c845caaSWilliam A. Kennington III * 1013c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 1023c845caaSWilliam A. Kennington III * @return The enabled status of the source 1033c845caaSWilliam A. Kennington III */ 1048fd0cd4cSWilliam A. Kennington III Enabled get_enabled() const; 1053c845caaSWilliam A. Kennington III 1063c845caaSWilliam A. Kennington III /** @brief Sets the enablement value of the source 1073c845caaSWilliam A. Kennington III * 1083c845caaSWilliam A. Kennington III * @param[in] enabled - The new state of the source 1093c845caaSWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 1103c845caaSWilliam A. Kennington III */ 1118fd0cd4cSWilliam A. Kennington III void set_enabled(Enabled enabled) const; 11265db863eSWilliam A. Kennington III 1130a816c5dSWilliam A. Kennington III protected: 114cc6b12beSWilliam A. Kennington III Event event; 1150a816c5dSWilliam A. Kennington III 116bc4e4569SWilliam A. Kennington III /** @brief Constructs a basic event source wrapper 117bc4e4569SWilliam A. Kennington III * Owns the passed reference to the source 118bc4e4569SWilliam A. Kennington III * This ownership is exception safe and will properly free the 119bc4e4569SWilliam A. Kennington III * source in the case of an exception during construction 120bc4e4569SWilliam A. Kennington III * 121bc4e4569SWilliam A. Kennington III * @param[in] event - The event associated with the source 122bc4e4569SWilliam A. Kennington III * @param[in] source - The underlying sd_event_source wrapped 123bc4e4569SWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 124bc4e4569SWilliam A. Kennington III */ 125715c72f8SWilliam A. Kennington III Base(const Event& event, sd_event_source* source, std::false_type); 12648c42751SWilliam A. Kennington III 127cc6b12beSWilliam A. Kennington III // We can't ever copy an event_source because the callback 128cc6b12beSWilliam A. Kennington III // data has to be unique. 129cc6b12beSWilliam A. Kennington III Base(const Base& other) = delete; 130cc6b12beSWilliam A. Kennington III Base& operator=(const Base& other) = delete; 131cc6b12beSWilliam A. Kennington III // We don't want to allow any kind of slicing. 1322d943eadSWilliam A. Kennington III Base(Base&& other); 133cc6b12beSWilliam A. Kennington III Base& operator=(Base&& other); 134cc6b12beSWilliam A. Kennington III 135bc4e4569SWilliam A. Kennington III /** @brief Returns a reference to the prepare callback executed for this 136bc4e4569SWilliam A. Kennington III * source 137bc4e4569SWilliam A. Kennington III * 138bc4e4569SWilliam A. Kennington III * @return A reference to the callback, this should be checked to make sure 139bc4e4569SWilliam A. Kennington III * the callback is valid as there is no guarantee 140bc4e4569SWilliam A. Kennington III */ 141e0f1d199SWilliam A. Kennington III const Callback& get_prepare() const; 142e0f1d199SWilliam A. Kennington III 143bc4e4569SWilliam A. Kennington III /** @brief A helper for subclasses to trivially wrap a c++ style callback 144bc4e4569SWilliam A. Kennington III * to be called from the sd-event c library 145bc4e4569SWilliam A. Kennington III * 146bc4e4569SWilliam A. Kennington III * @param[in] name - The name of the callback for use in error messages 147bc4e4569SWilliam A. Kennington III * @param[in] source - The sd_event_source provided by sd-event 148bc4e4569SWilliam A. Kennington III * @param[in] userdata - The userdata provided by sd-event 149bc4e4569SWilliam A. Kennington III * @param[in] args... - Extra arguments to pass to the callaback 150bc4e4569SWilliam A. Kennington III * @return An negative errno on error, or 0 on success 151bc4e4569SWilliam A. Kennington III */ 152e0f1d199SWilliam A. Kennington III template <typename Callback, class Source, 153e0f1d199SWilliam A. Kennington III const Callback& (Source::*getter)() const, typename... Args> 154e0f1d199SWilliam A. Kennington III static int sourceCallback(const char* name, sd_event_source*, 155e0f1d199SWilliam A. Kennington III void* userdata, Args... args) 156e0f1d199SWilliam A. Kennington III { 157e0f1d199SWilliam A. Kennington III if (userdata == nullptr) 158e0f1d199SWilliam A. Kennington III { 159e0f1d199SWilliam A. Kennington III fprintf(stderr, "sdeventplus: %s: Missing userdata\n", name); 160e0f1d199SWilliam A. Kennington III return -EINVAL; 161e0f1d199SWilliam A. Kennington III } 162e0f1d199SWilliam A. Kennington III Source* source = reinterpret_cast<Source*>(userdata); 163e0f1d199SWilliam A. Kennington III return internal::performCallback(name, (source->*getter)(), 164e0f1d199SWilliam A. Kennington III std::ref(*source), args...); 165e0f1d199SWilliam A. Kennington III } 166e0f1d199SWilliam A. Kennington III 16748c42751SWilliam A. Kennington III private: 168e04cb03bSWilliam A. Kennington III static void drop(sd_event_source*&& source, 169e04cb03bSWilliam A. Kennington III const internal::SdEvent*& sdevent); 170e04cb03bSWilliam A. Kennington III 171*b555c4e6SWilliam A. Kennington III stdplus::Managed<sd_event_source*, const internal::SdEvent*>::Handle<drop> 172e04cb03bSWilliam A. Kennington III source; 17348c42751SWilliam A. Kennington III Callback prepare; 1742d943eadSWilliam A. Kennington III 175bc4e4569SWilliam A. Kennington III /** @brief A helper used to make sure the userdata for the sd-event 176bc4e4569SWilliam A. Kennington III * callback is set to the current source c++ object 177bc4e4569SWilliam A. Kennington III * 178bc4e4569SWilliam A. Kennington III * @throws SdEventError for underlying sd_event errors 179bc4e4569SWilliam A. Kennington III */ 1802d943eadSWilliam A. Kennington III void set_userdata(); 181bc4e4569SWilliam A. Kennington III 182bc4e4569SWilliam A. Kennington III /** @brief A wrapper around the callback that can be called from sd-event 183bc4e4569SWilliam A. Kennington III * 184bc4e4569SWilliam A. Kennington III * @param[in] source - The sd_event_source associated with the call 185bc4e4569SWilliam A. Kennington III * @param[in] userdata - The provided userdata for the source 186bc4e4569SWilliam A. Kennington III * @return 0 on success or a negative errno otherwise 187bc4e4569SWilliam A. Kennington III */ 188e0f1d199SWilliam A. Kennington III static int prepareCallback(sd_event_source* source, void* userdata); 1890a816c5dSWilliam A. Kennington III }; 1900a816c5dSWilliam A. Kennington III 1910a816c5dSWilliam A. Kennington III } // namespace source 1920a816c5dSWilliam A. Kennington III } // namespace sdeventplus 193