blob: 1c513ef358b3e16a3effbad8bcb18226b5963654 [file] [log] [blame]
#pragma once
#include "button_interface.hpp"
#include "gpio.hpp"
#include <phosphor-logging/elog-errors.hpp>
#include <unordered_map>
using buttonIfCreatorMethod = std::function<std::unique_ptr<ButtonIface>(
sdbusplus::bus::bus& bus, EventPtr& event, buttonConfig& buttonCfg)>;
/**
* @brief This is abstract factory for the creating phosphor buttons objects
* based on the button / formfactor type given.
*/
class ButtonFactory
{
public:
static ButtonFactory& instance()
{
static ButtonFactory buttonFactoryObj;
return buttonFactoryObj;
}
/**
* @brief this method creates a key and value pair element
* for the given button interface where key is the form factor
* name and the value is lambda method to return
* the instance of the button interface.
* This key value pair is stored in the Map buttonIfaceRegistry.
*/
template <typename T>
void addToRegistry()
{
buttonIfaceRegistry[std::string(T::getFormFactorName())] =
[](sdbusplus::bus::bus& bus, EventPtr& event,
buttonConfig& buttonCfg) {
return std::make_unique<T>(bus, T::getDbusObjectPath(), event,
buttonCfg);
};
}
/**
* @brief this method returns the button interface object
* corresponding to the button formfactor name provided
*/
std::unique_ptr<ButtonIface> createInstance(std::string name,
sdbusplus::bus::bus& bus,
EventPtr& event,
buttonConfig& buttonCfg)
{
// find matching name in the registry and call factory method.
auto objectIter = buttonIfaceRegistry.find(name);
if (objectIter != buttonIfaceRegistry.end())
{
return objectIter->second(bus, event, buttonCfg);
}
else
{
return nullptr;
}
}
private:
// This map is the registry for keeping supported button interface types.
std::unordered_map<std::string, buttonIfCreatorMethod> buttonIfaceRegistry;
};
template <class T>
class ButtonIFRegister
{
public:
ButtonIFRegister()
{
// register the class factory function
ButtonFactory::instance().addToRegistry<T>();
}
};