#ifndef FACTORY_HPP_QLKNHPWZ #define FACTORY_HPP_QLKNHPWZ #include #include #include #include namespace vanetza { /** * Factory for a group of classes implementing T */ template class Factory { public: using Result = std::unique_ptr; using Function = std::function; 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 obj; auto found = m_functions.find(name); if (found != m_functions.end()) { obj = found->second(std::forward(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 obj; if (m_default != m_functions.end()) { obj = m_default->second(std::forward(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; map_type m_functions; typename map_type::const_iterator m_default; }; } // namespace vanetza #endif /* FACTORY_HPP_QLKNHPWZ */