#pragma once #include "config.hpp" #include "power_button_profile.hpp" #include #include namespace phosphor::button { using powerButtonProfileCreator = std::function(sdbusplus::bus_t& bus)>; /** * @class PowerButtonProfileFactory * * Creates the custom power button profile class if one is set with * the 'power-button-profile' meson option. * * The createProfile() method will return a nullptr if no custom * profile is enabled. */ class PowerButtonProfileFactory { public: static PowerButtonProfileFactory& instance() { static PowerButtonProfileFactory factory; return factory; } template void addToRegistry() { profileRegistry[std::string(T::getName())] = [](sdbusplus::bus_t& bus) { return std::make_unique(bus); }; } std::unique_ptr createProfile(sdbusplus::bus_t& bus) { // Find the creator method named after the // 'power-button-profile' option value. auto objectIter = profileRegistry.find(POWER_BUTTON_PROFILE); if (objectIter != profileRegistry.end()) { return objectIter->second(bus); } else { return nullptr; } } private: PowerButtonProfileFactory() = default; std::unordered_map profileRegistry; }; /** * @brief Registers a power button profile with the factory. * * Declare a static instance of this at the top of the profile * .cpp file like: * static PowerButtonProfileRegister register; */ template class PowerButtonProfileRegister { public: PowerButtonProfileRegister() { PowerButtonProfileFactory::instance().addToRegistry(); } }; } // namespace phosphor::button