Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 3 | #include "button_config.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 4 | #include "button_interface.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 5 | |
| 6 | #include <phosphor-logging/elog-errors.hpp> |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 7 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 8 | #include <unordered_map> |
| 9 | |
| 10 | using buttonIfCreatorMethod = std::function<std::unique_ptr<ButtonIface>( |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 11 | sdbusplus::bus_t& bus, EventPtr& event, ButtonConfig& buttonCfg)>; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * @brief This is abstract factory for the creating phosphor buttons objects |
| 15 | * based on the button / formfactor type given. |
| 16 | */ |
| 17 | |
| 18 | class ButtonFactory |
| 19 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 20 | public: |
| 21 | static ButtonFactory& instance() |
| 22 | { |
| 23 | static ButtonFactory buttonFactoryObj; |
| 24 | return buttonFactoryObj; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @brief this method creates a key and value pair element |
| 29 | * for the given button interface where key is the form factor |
| 30 | * name and the value is lambda method to return |
| 31 | * the instance of the button interface. |
| 32 | * This key value pair is stored in the Map buttonIfaceRegistry. |
| 33 | */ |
| 34 | |
| 35 | template <typename T> |
| 36 | void addToRegistry() |
| 37 | { |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 38 | buttonIfaceRegistry[T::getFormFactorName()] = |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 39 | [](sdbusplus::bus_t& bus, EventPtr& event, |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 40 | ButtonConfig& buttonCfg) { |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 41 | return std::make_unique<T>(bus, T::getDbusObjectPath().c_str(), |
| 42 | event, buttonCfg); |
Patrick Williams | d36b6b1 | 2024-08-16 15:20:34 -0400 | [diff] [blame] | 43 | }; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 44 | } |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 45 | |
| 46 | template <typename T> |
| 47 | void addToRegistry(size_t index) |
| 48 | { |
| 49 | auto indexStr = std::to_string(index); |
| 50 | buttonIfaceRegistry[T::getFormFactorName() + indexStr] = |
| 51 | [=](sdbusplus::bus_t& bus, EventPtr& event, |
| 52 | ButtonConfig& buttonCfg) { |
| 53 | return std::make_unique<T>( |
| 54 | bus, (T::getDbusObjectPath() + indexStr).c_str(), event, |
| 55 | buttonCfg); |
| 56 | }; |
| 57 | } |
| 58 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 59 | /** |
| 60 | * @brief this method returns the button interface object |
| 61 | * corresponding to the button formfactor name provided |
| 62 | */ |
Patrick Williams | d36b6b1 | 2024-08-16 15:20:34 -0400 | [diff] [blame] | 63 | std::unique_ptr<ButtonIface> |
| 64 | createInstance(const std::string& name, sdbusplus::bus_t& bus, |
| 65 | EventPtr& event, ButtonConfig& buttonCfg) |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 66 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 67 | // find matching name in the registry and call factory method. |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 68 | auto objectIter = buttonIfaceRegistry.find(name); |
| 69 | if (objectIter != buttonIfaceRegistry.end()) |
| 70 | { |
| 71 | return objectIter->second(bus, event, buttonCfg); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | return nullptr; |
| 76 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private: |
| 80 | // This map is the registry for keeping supported button interface types. |
| 81 | std::unordered_map<std::string, buttonIfCreatorMethod> buttonIfaceRegistry; |
| 82 | }; |
| 83 | |
| 84 | template <class T> |
| 85 | class ButtonIFRegister |
| 86 | { |
| 87 | public: |
| 88 | ButtonIFRegister() |
| 89 | { |
| 90 | // register the class factory function |
| 91 | ButtonFactory::instance().addToRegistry<T>(); |
| 92 | } |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 93 | |
| 94 | explicit ButtonIFRegister(size_t count) |
| 95 | { |
| 96 | // register the class factory function |
| 97 | // The index, 'countIter', starts at 1 and increments, |
| 98 | // representing slot_1 through slot_N. |
| 99 | for (size_t countIter = 1; countIter <= count; countIter++) |
| 100 | { |
| 101 | ButtonFactory::instance().addToRegistry<T>(countIter); |
| 102 | } |
| 103 | } |
Patrick Williams | 0d038f5 | 2023-05-10 07:50:40 -0500 | [diff] [blame] | 104 | }; |