Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | #include "utils.hpp" |
| 5 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 9 | #include <utility> |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace inventory |
| 14 | { |
| 15 | namespace manager |
| 16 | { |
| 17 | |
| 18 | class Manager; |
| 19 | |
| 20 | /** @brief make_action |
| 21 | * |
| 22 | * Adapt an action function object. |
| 23 | * |
| 24 | * @param[in] action - The action being adapted. |
| 25 | * @returns - The adapted action. |
| 26 | * |
| 27 | * @tparam T - The type of the action being adapted. |
| 28 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 29 | template <typename T> |
| 30 | auto make_action(T&& action) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 31 | { |
| 32 | return Action(std::forward<T>(action)); |
| 33 | } |
| 34 | |
| 35 | /** @brief make_filter |
| 36 | * |
| 37 | * Adapt a filter function object. |
| 38 | * |
| 39 | * @param[in] filter - The filter being adapted. |
| 40 | * @returns - The adapted filter. |
| 41 | * |
| 42 | * @tparam T - The type of the filter being adapted. |
| 43 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 44 | template <typename T> |
| 45 | auto make_filter(T&& filter) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 46 | { |
| 47 | return Filter(std::forward<T>(filter)); |
| 48 | } |
| 49 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 50 | /** @brief make_path_condition |
| 51 | * |
| 52 | * Adapt a path_condition function object. |
| 53 | * |
| 54 | * @param[in] filter - The functor being adapted. |
| 55 | * @returns - The adapted functor. |
| 56 | * |
| 57 | * @tparam T - The type of the functor being adapted. |
| 58 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 59 | template <typename T> |
| 60 | auto make_path_condition(T&& condition) |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 61 | { |
| 62 | return PathCondition(std::forward<T>(condition)); |
| 63 | } |
| 64 | |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 65 | /** @brief make_get_property |
| 66 | * |
| 67 | * Adapt a get_property function object. |
| 68 | * |
| 69 | * @param[in] method - The functor being adapted. |
| 70 | * @returns - The adapted functor. |
| 71 | * |
| 72 | * @tparam T - The return type of the function object. |
| 73 | * @tparam U - The type of the functor being adapted. |
| 74 | */ |
| 75 | template <typename T, typename U> |
| 76 | auto make_get_property(U&& method) |
| 77 | { |
| 78 | return GetProperty<T>(std::forward<U>(method)); |
| 79 | } |
| 80 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 81 | template <typename T, typename... Args> |
| 82 | auto callArrayWithStatus(T&& container, Args&&... args) |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 83 | { |
| 84 | for (auto f : container) |
| 85 | { |
| 86 | if (!f(std::forward<Args>(args)...)) |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 94 | namespace functor |
| 95 | { |
| 96 | |
| 97 | /** @brief Destroy objects action. */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 98 | inline auto destroyObjects(std::vector<const char*>&& paths, |
| 99 | std::vector<PathCondition>&& conditions) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 100 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 101 | return [=](auto& b, auto& m) { |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 102 | for (const auto& p : paths) |
| 103 | { |
| 104 | if (callArrayWithStatus(conditions, p, b, m)) |
| 105 | { |
| 106 | m.destroyObjects({p}); |
| 107 | } |
| 108 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 109 | }; |
| 110 | } |
| 111 | |
| 112 | /** @brief Create objects action. */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 113 | inline auto |
| 114 | createObjects(std::map<sdbusplus::message::object_path, Object>&& objs) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 115 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 116 | return [=](auto&, auto& m) { m.createObjects(objs); }; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** @brief Set a property action. |
| 120 | * |
| 121 | * Invoke the requested method with a reference to the requested |
| 122 | * sdbusplus server binding interface as a parameter. |
| 123 | * |
| 124 | * @tparam T - The sdbusplus server binding interface type. |
| 125 | * @tparam U - The type of the sdbusplus server binding member |
| 126 | * function that sets the property. |
| 127 | * @tparam V - The property value type. |
| 128 | * |
| 129 | * @param[in] paths - The DBus paths on which the property should |
| 130 | * be set. |
| 131 | * @param[in] iface - The DBus interface hosting the property. |
| 132 | * @param[in] member - Pointer to sdbusplus server binding member. |
| 133 | * @param[in] value - The value the property should be set to. |
| 134 | * |
| 135 | * @returns - A function object that sets the requested property |
| 136 | * to the requested value. |
| 137 | */ |
| 138 | template <typename T, typename U, typename V> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 139 | auto setProperty(std::vector<const char*>&& paths, |
| 140 | std::vector<PathCondition>&& conditions, const char* iface, |
| 141 | U&& member, V&& value) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 142 | { |
| 143 | // The manager is the only parameter passed to actions. |
| 144 | // Bind the path, interface, interface member function pointer, |
| 145 | // and value to a lambda. When it is called, forward the |
| 146 | // path, interface and value on to the manager member function. |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 147 | return [paths, conditions = conditions, iface, member, |
| 148 | value = std::forward<V>(value)](auto& b, auto& m) { |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 149 | for (auto p : paths) |
| 150 | { |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 151 | if (callArrayWithStatus(conditions, p, b, m)) |
| 152 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 153 | m.template invokeMethod<T>(p, iface, member, value); |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 154 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 155 | } |
| 156 | }; |
| 157 | } |
| 158 | |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 159 | /** @brief Get a property. |
| 160 | * |
| 161 | * Invoke the requested method with a reference to the requested |
| 162 | * sdbusplus server binding interface as a parameter. |
| 163 | * |
| 164 | * @tparam T - The sdbusplus server binding interface type. |
| 165 | * @tparam U - The type of the sdbusplus server binding member |
| 166 | * function that sets the property. |
| 167 | * |
| 168 | * @param[in] path - The DBus path to get the property from. |
| 169 | * @param[in] iface - The DBus interface hosting the property. |
| 170 | * @param[in] member - Pointer to sdbusplus server binding member. |
| 171 | * @param[in] prop - The property name to get the value from. |
| 172 | * |
| 173 | * @returns - A function object that gets the requested property. |
| 174 | */ |
| 175 | template <typename T, typename U> |
| 176 | inline auto getProperty(const char* path, const char* iface, U&& member, |
| 177 | const char* prop) |
| 178 | { |
| 179 | return [path, iface, member, prop](auto& mgr) { |
| 180 | return mgr.template invokeMethod<T>(path, iface, member, prop); |
| 181 | }; |
| 182 | } |
| 183 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 184 | /** @struct PropertyChangedCondition |
| 185 | * @brief Match filter functor that tests a property value. |
| 186 | * |
| 187 | * @tparam T - The type of the property being tested. |
| 188 | * @tparam U - The type of the condition checking functor. |
| 189 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 190 | template <typename T, typename U> |
| 191 | struct PropertyChangedCondition |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 192 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 193 | PropertyChangedCondition() = delete; |
| 194 | ~PropertyChangedCondition() = default; |
| 195 | PropertyChangedCondition(const PropertyChangedCondition&) = default; |
| 196 | PropertyChangedCondition& |
| 197 | operator=(const PropertyChangedCondition&) = default; |
| 198 | PropertyChangedCondition(PropertyChangedCondition&&) = default; |
| 199 | PropertyChangedCondition& operator=(PropertyChangedCondition&&) = default; |
| 200 | PropertyChangedCondition(const char* iface, const char* property, |
| 201 | U&& condition) : |
Patrick Williams | d8fba8b | 2024-08-16 15:20:15 -0400 | [diff] [blame] | 202 | _iface(iface), _property(property), |
| 203 | _condition(std::forward<U>(condition)) |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 204 | {} |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 205 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 206 | /** @brief Test a property value. |
| 207 | * |
| 208 | * Extract the property from the PropertiesChanged |
| 209 | * message and run the condition test. |
| 210 | */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 211 | bool operator()(sdbusplus::bus_t&, sdbusplus::message_t& msg, |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 212 | Manager&) const |
| 213 | { |
Patrick Williams | 55f9eae | 2020-05-13 17:57:04 -0500 | [diff] [blame] | 214 | std::map<std::string, std::variant<T>> properties; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 215 | const char* iface = nullptr; |
| 216 | |
| 217 | msg.read(iface); |
| 218 | if (!iface || strcmp(iface, _iface)) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 219 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 220 | return false; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 223 | msg.read(properties); |
| 224 | auto it = properties.find(_property); |
| 225 | if (it == properties.cend()) |
| 226 | { |
| 227 | return false; |
| 228 | } |
| 229 | |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame] | 230 | return _condition(std::forward<T>(std::get<T>(it->second))); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private: |
| 234 | const char* _iface; |
| 235 | const char* _property; |
| 236 | U _condition; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | /** @struct PropertyConditionBase |
| 240 | * @brief Match filter functor that tests a property value. |
| 241 | * |
| 242 | * Base class for PropertyCondition - factored out code that |
| 243 | * doesn't need to be templated. |
| 244 | */ |
| 245 | struct PropertyConditionBase |
| 246 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 247 | PropertyConditionBase() = delete; |
| 248 | virtual ~PropertyConditionBase() = default; |
| 249 | PropertyConditionBase(const PropertyConditionBase&) = default; |
| 250 | PropertyConditionBase& operator=(const PropertyConditionBase&) = default; |
| 251 | PropertyConditionBase(PropertyConditionBase&&) = default; |
| 252 | PropertyConditionBase& operator=(PropertyConditionBase&&) = default; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 253 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 254 | /** @brief Constructor |
| 255 | * |
| 256 | * The service argument can be nullptr. If something |
| 257 | * else is provided the function will call the the |
| 258 | * service directly. If omitted, the function will |
| 259 | * look up the service in the ObjectMapper. |
| 260 | * |
| 261 | * @param path - The path of the object containing |
| 262 | * the property to be tested. |
| 263 | * @param iface - The interface hosting the property |
| 264 | * to be tested. |
| 265 | * @param property - The property to be tested. |
| 266 | * @param service - The DBus service hosting the object. |
| 267 | */ |
| 268 | PropertyConditionBase(const char* path, const char* iface, |
| 269 | const char* property, const char* service) : |
Patrick Williams | d8fba8b | 2024-08-16 15:20:15 -0400 | [diff] [blame] | 270 | _path(path ? path : std::string()), _iface(iface), _property(property), |
| 271 | _service(service) |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 272 | {} |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 273 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 274 | /** @brief Forward comparison to type specific implementation. */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 275 | virtual bool eval(sdbusplus::message_t&) const = 0; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 276 | |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 277 | /** @brief Forward comparison to type specific implementation. */ |
| 278 | virtual bool eval(Manager&) const = 0; |
| 279 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 280 | /** @brief Test a property value. |
| 281 | * |
| 282 | * Make a DBus call and test the value of any property. |
| 283 | */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 284 | bool operator()(sdbusplus::bus_t&, sdbusplus::message_t&, Manager&) const; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 285 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 286 | /** @brief Test a property value. |
| 287 | * |
| 288 | * Make a DBus call and test the value of any property. |
| 289 | */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 290 | bool operator()(const std::string&, sdbusplus::bus_t&, Manager&) const; |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 291 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 292 | private: |
| 293 | std::string _path; |
| 294 | std::string _iface; |
| 295 | std::string _property; |
| 296 | const char* _service; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | /** @struct PropertyCondition |
| 300 | * @brief Match filter functor that tests a property value. |
| 301 | * |
| 302 | * @tparam T - The type of the property being tested. |
| 303 | * @tparam U - The type of the condition checking functor. |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 304 | * @tparam V - The getProperty functor return type. |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 305 | */ |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 306 | template <typename T, typename U, typename V> |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 307 | struct PropertyCondition final : public PropertyConditionBase |
| 308 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 309 | PropertyCondition() = delete; |
| 310 | ~PropertyCondition() = default; |
| 311 | PropertyCondition(const PropertyCondition&) = default; |
| 312 | PropertyCondition& operator=(const PropertyCondition&) = default; |
| 313 | PropertyCondition(PropertyCondition&&) = default; |
| 314 | PropertyCondition& operator=(PropertyCondition&&) = default; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 315 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 316 | /** @brief Constructor |
| 317 | * |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 318 | * The service & getProperty arguments can be nullptrs. |
| 319 | * If something else is provided the function will call the the |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 320 | * service directly. If omitted, the function will |
| 321 | * look up the service in the ObjectMapper. |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 322 | * The getProperty function will be called to retrieve a property |
| 323 | * value when given and the property is hosted by inventory manager. |
| 324 | * When not given, the condition will default to return that the |
| 325 | * condition failed and will not be executed. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 326 | * |
| 327 | * @param path - The path of the object containing |
| 328 | * the property to be tested. |
| 329 | * @param iface - The interface hosting the property |
| 330 | * to be tested. |
| 331 | * @param property - The property to be tested. |
| 332 | * @param condition - The test to run on the property. |
| 333 | * @param service - The DBus service hosting the object. |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 334 | * @param getProperty - The function to get a property value |
| 335 | * for the condition. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 336 | */ |
| 337 | PropertyCondition(const char* path, const char* iface, const char* property, |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 338 | U&& condition, const char* service, |
| 339 | GetProperty<V>&& getProperty = nullptr) : |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 340 | PropertyConditionBase(path, iface, property, service), |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 341 | _condition(std::forward<decltype(condition)>(condition)), |
| 342 | _getProperty(getProperty) |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 343 | {} |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 344 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 345 | /** @brief Test a property value. |
| 346 | * |
| 347 | * Make a DBus call and test the value of any property. |
| 348 | */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 349 | bool eval(sdbusplus::message_t& msg) const override |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 350 | { |
Patrick Williams | 55f9eae | 2020-05-13 17:57:04 -0500 | [diff] [blame] | 351 | std::variant<T> value; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 352 | msg.read(value); |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame] | 353 | return _condition(std::forward<T>(std::get<T>(value))); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 354 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 355 | |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 356 | /** @brief Retrieve a property value from inventory and test it. |
| 357 | * |
| 358 | * Get a property from the inventory manager and test the value. |
| 359 | * Default to fail the test where no function is given to get the |
| 360 | * property from the inventory manager. |
| 361 | */ |
| 362 | bool eval(Manager& mgr) const override |
| 363 | { |
| 364 | if (_getProperty) |
| 365 | { |
| 366 | auto variant = _getProperty(mgr); |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame] | 367 | auto value = std::get<T>(variant); |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 368 | return _condition(std::forward<T>(value)); |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 373 | private: |
| 374 | U _condition; |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 375 | GetProperty<V> _getProperty; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 376 | }; |
| 377 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 378 | /** @brief Implicit type deduction for constructing PropertyChangedCondition. */ |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 379 | template <typename T> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 380 | auto propertyChangedTo(const char* iface, const char* property, T&& val) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 381 | { |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 382 | auto condition = [val = std::forward<T>(val)](T&& arg) { |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 383 | return arg == val; |
| 384 | }; |
| 385 | using U = decltype(condition); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 386 | return PropertyChangedCondition<T, U>(iface, property, |
| 387 | std::move(condition)); |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** @brief Implicit type deduction for constructing PropertyCondition. */ |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 391 | template <typename T, typename V = InterfaceVariantType> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 392 | auto propertyIs(const char* path, const char* iface, const char* property, |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 393 | T&& val, const char* service = nullptr, |
| 394 | GetProperty<V>&& getProperty = nullptr) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 395 | { |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 396 | auto condition = [val = std::forward<T>(val)](T&& arg) { |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 397 | return arg == val; |
| 398 | }; |
| 399 | using U = decltype(condition); |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 400 | return PropertyCondition<T, U, V>(path, iface, property, |
| 401 | std::move(condition), service, |
| 402 | std::move(getProperty)); |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 403 | } |
| 404 | } // namespace functor |
| 405 | } // namespace manager |
| 406 | } // namespace inventory |
| 407 | } // namespace phosphor |