obu-firmware builds against the vanetza-idf C-ITS library, which until now came from the colleague's microbu-esp32c5 tree beside the repository and was not tracked here, so a clone of this repository could not build the firmware it ships. The library alone is now part of obu-firmware, as obu-firmware/external/vanetza-idf: their external/vanetza-idf at commit cf4b99f, unchanged (9775 files; see its PROVENANCE.md). CMake takes it from there by default; -DVANETZA_IDF_DIR still points the build elsewhere. The rest of the colleague's tree (their own VAM firmware, PKI tooling, station-link Python tools, the V2X2MAP bridge) stays out of this repository and gitignored; nothing is pushed to their repository. NOTES.md, docs/06, TODO.md and the pcap verifier's usage line point at the new location.
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 */
|
|
|