obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but that tree was gitignored, so a clone of this repository could not build the firmware it ships. It is now committed here as ordinary files in its own folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP bridge's signature verification (--trust) used on the bench. Nothing is fetched from or pushed to the colleague's repository; this repository and its remotes carry everything. The folder's own .gitignore keeps build output, downloaded components and private key material out, as it did there; the committed file set is identical to that repository's tracked files. The ESP32-C5 is still flashed from obu-firmware/, which only takes vanetza-idf from microbu-esp32c5/, so the two stay separate folders. FLASHING.md says how to take a newer version of the colleague's tree (copy it over the folder, rebuild, test, commit).
86 lines
2.1 KiB
C++
86 lines
2.1 KiB
C++
#ifndef FACTORY_HPP_QLKNHPWZ
|
|
#define FACTORY_HPP_QLKNHPWZ
|
|
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
namespace vanetza
|
|
{
|
|
|
|
/**
|
|
* Factory for a group of classes implementing T
|
|
*/
|
|
template<typename T, typename... Args>
|
|
class Factory
|
|
{
|
|
public:
|
|
using Result = std::unique_ptr<T>;
|
|
using Function = std::function<Result(Args...)>;
|
|
|
|
Factory() : m_default(m_functions.end())
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Create an instance of T using a named implementation
|
|
* \param name of wanted implementation
|
|
* \return created instance or nullptr if not found
|
|
*/
|
|
Result create(const std::string& name, Args... args) const
|
|
{
|
|
std::unique_ptr<T> obj;
|
|
auto found = m_functions.find(name);
|
|
if (found != m_functions.end()) {
|
|
obj = found->second(std::forward<Args>(args)...);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
/**
|
|
* Create object using default implementation
|
|
* \return created instance or nullptr if no default is configured
|
|
*/
|
|
Result create(Args... args) const
|
|
{
|
|
std::unique_ptr<T> obj;
|
|
if (m_default != m_functions.end()) {
|
|
obj = m_default->second(std::forward<Args>(args)...);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
/**
|
|
* Add an implementation to factory
|
|
* \param name of implementation
|
|
* \param f function creating a new instance of this implementation
|
|
* \return true if added successfully, i.e. no previous addition with same name
|
|
*/
|
|
bool add(const std::string& name, Function f)
|
|
{
|
|
return m_functions.emplace(name, std::move(f)).second;
|
|
}
|
|
|
|
/**
|
|
* Set default implementation
|
|
* \param name selected default implementation
|
|
* \return true if an implementation exists with selected name
|
|
*/
|
|
bool configure_default(const std::string& name)
|
|
{
|
|
m_default = m_functions.find(name);
|
|
return m_default != m_functions.end();
|
|
}
|
|
|
|
private:
|
|
using map_type = std::map<std::string, Function>;
|
|
map_type m_functions;
|
|
typename map_type::const_iterator m_default;
|
|
};
|
|
|
|
} // namespace vanetza
|
|
|
|
#endif /* FACTORY_HPP_QLKNHPWZ */
|
|
|