#pragma once #include #include #include #include "utils.hpp" namespace phosphor { namespace inventory { namespace manager { class Manager; namespace details { using FilterBase = holder::CallableBase < bool, sdbusplus::message::message&, Manager& >; using FilterBasePtr = std::shared_ptr; template using Filter = holder::CallableHolder < T, bool, sdbusplus::message::message&, Manager& >; /** @struct Event * @brief Event object interface. */ struct Event { enum class Type { DBUS_SIGNAL, }; virtual ~Event() = default; Event(const Event&) = default; Event& operator=(const Event&) = default; Event(Event&&) = default; Event& operator=(Event&&) = default; explicit Event(Type t) : type(t) {} Type type; }; using EventBasePtr = std::shared_ptr; /** @struct DbusSignal * @brief DBus signal event. * * DBus signal events are an association of a match signature * and filtering function object. */ struct DbusSignal final : public Event, public std::tuple> { virtual ~DbusSignal() = default; DbusSignal(const DbusSignal&) = default; DbusSignal& operator=(const DbusSignal&) = delete; DbusSignal(DbusSignal&&) = default; DbusSignal& operator=(DbusSignal&&) = default; /** @brief Import from signature and filter constructor. * * @param[in] sig - The DBus match signature. * @param[in] filter - A DBus signal match callback filtering function. */ DbusSignal(const char* sig, const std::vector& filters) : Event(Type::DBUS_SIGNAL), std::tuple>( sig, std::move(filters)) {} }; /** @brief make_filter * * Adapt a filter function object. * * @param[in] filter - The filter being adapted. * @returns - The adapted filter. * * @tparam T - The type of the filter being adapted. */ template auto make_filter(T&& filter) { return Filter::template make_shared>( std::forward(filter)); } } // namespace details namespace filters { namespace details { namespace property_condition { /** @struct PropertyChangedCondition * @brief Match filter functor that tests a property value. * * @tparam T - The type of the property being tested. * @tparam U - The type of the condition checking functor. */ template struct PropertyChangedCondition { PropertyChangedCondition() = delete; ~PropertyChangedCondition() = default; PropertyChangedCondition(const PropertyChangedCondition&) = default; PropertyChangedCondition& operator=(const PropertyChangedCondition&) = delete; PropertyChangedCondition(PropertyChangedCondition&&) = default; PropertyChangedCondition& operator=(PropertyChangedCondition&&) = default; PropertyChangedCondition(const char* iface, const char* property, U&& condition) : _iface(iface), _property(property), _condition(std::forward(condition)) { } /** @brief Test a property value. * * Extract the property from the PropertiesChanged * message and run the condition test. */ bool operator()(sdbusplus::message::message& msg, Manager&) const { std::map < std::string, sdbusplus::message::variant> properties; const char* iface = nullptr; msg.read(iface); if (!iface || strcmp(iface, _iface)) { return false; } msg.read(properties); auto it = properties.find(_property); if (it == properties.cend()) { return false; } return _condition( std::forward(it->second.template get())); } private: const char* _iface; const char* _property; U _condition; }; } // namespace property_condition } // namespace details /** @brief Implicit type deduction for constructing PropertyChangedCondition. */ template auto propertyChangedTo( const char* iface, const char* property, T&& val) { auto condition = [val = std::move(val)](T && arg) { return arg == val; }; using U = decltype(condition); return details::property_condition::PropertyChangedCondition( iface, property, std::move(condition)); } } // namespace filters } // namespace manager } // namespace inventory } // namespace phosphor // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4