diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ffe5b8ef000f2c247950e07f3507a8955f32854a..f0dcb97c07c4e58cbb0cb1e2e488c2b0488936b0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -228,7 +228,6 @@ SET(SRCS processor_tracker_landmark_dummy.cpp sensor_base.cpp sensor_camera.cpp - sensor_factory.cpp sensor_gps.cpp sensor_gps_fix.cpp sensor_imu.cpp diff --git a/src/factory.h b/src/factory.h index f750cd0476be4de59c4b5fc023de4c3f8bfbafec..34026967fc14fc8ef01ce8db003859e6d5e9ee3b 100644 --- a/src/factory.h +++ b/src/factory.h @@ -228,17 +228,18 @@ namespace wolf * - Problem::installProcessor() : to install processors in WOLF Problem. * */ -template<class TypeBase, class TypeInput> +template<class TypeBase, typename... TypeInput> class Factory { public: - typedef TypeBase* (*CreatorCallback)(const TypeInput & _input); // example of creator callback (see typedefs below) + // example of creator callback (see typedefs below) + typedef TypeBase* (*CreatorCallback)(TypeInput... _input); private: typedef std::map<std::string, CreatorCallback> CallbackMap; public: bool registerCreator(const std::string& _type, CreatorCallback createFn); bool unregisterCreator(const std::string& _type); - TypeBase* create(const std::string& _type, const TypeInput& _input = ""); + TypeBase* create(const std::string& _type, TypeInput... _input); std::string getClass(); private: @@ -257,8 +258,8 @@ class Factory ~Factory() { } }; -template<class TypeBase, class TypeInput> -inline bool Factory<TypeBase, TypeInput>::registerCreator(const std::string& _type, CreatorCallback createFn) +template<class TypeBase, typename... TypeInput> +inline bool Factory<TypeBase, TypeInput...>::registerCreator(const std::string& _type, CreatorCallback createFn) { bool reg = callbacks_.insert(typename CallbackMap::value_type(_type, createFn)).second; if (reg) @@ -269,33 +270,34 @@ inline bool Factory<TypeBase, TypeInput>::registerCreator(const std::string& _ty return reg; } -template<class TypeBase, class TypeInput> -inline bool Factory<TypeBase, TypeInput>::unregisterCreator(const std::string& _type) +template<class TypeBase, typename... TypeInput> +inline bool Factory<TypeBase, TypeInput...>::unregisterCreator(const std::string& _type) { return callbacks_.erase(_type) == 1; } -template<class TypeBase, class TypeInput> -inline TypeBase* Factory<TypeBase, TypeInput>::create(const std::string& _type, const TypeInput& _input) +template<class TypeBase, typename... TypeInput> +inline TypeBase* Factory<TypeBase, TypeInput...>::create(const std::string& _type, TypeInput... _input) { typename CallbackMap::const_iterator creator_callback_it = callbacks_.find(_type); + if (creator_callback_it == callbacks_.end()) // not found throw std::runtime_error("Unknown type. Possibly you tried to use an unregistered creator."); + // Invoke the creation function - TypeBase* p = (creator_callback_it->second)(_input); - return p; + return (creator_callback_it->second)(std::forward<TypeInput>(_input)...); } -template<class TypeBase, class TypeInput> -inline Factory<TypeBase, TypeInput>& Factory<TypeBase, TypeInput>::get() +template<class TypeBase, typename... TypeInput> +inline Factory<TypeBase, TypeInput...>& Factory<TypeBase, TypeInput...>::get() { static Factory instance_; return instance_; } -template<class TypeBase, class TypeInput> -inline std::string Factory<TypeBase, TypeInput>::getClass() +template<class TypeBase, typename... TypeInput> +inline std::string Factory<TypeBase, TypeInput...>::getClass() { return "Factory<class TypeBase>"; } @@ -312,24 +314,24 @@ inline std::string Factory<TypeBase, TypeInput>::getClass() namespace wolf { -typedef Factory<IntrinsicsBase, std::string> IntrinsicsFactory; -typedef Factory<ProcessorParamsBase, std::string> ProcessorParamsFactory; -typedef Factory<LandmarkBase, YAML::Node> LandmarkFactory; +typedef Factory<IntrinsicsBase, const std::string&> IntrinsicsFactory; +typedef Factory<ProcessorParamsBase, const std::string&> ProcessorParamsFactory; +typedef Factory<LandmarkBase, const YAML::Node&> LandmarkFactory; template<> -inline std::string Factory<IntrinsicsBase, std::string>::getClass() +inline std::string Factory<IntrinsicsBase, const std::string&>::getClass() { return "IntrinsicsFactory"; } template<> -inline std::string Factory<ProcessorParamsBase, std::string>::getClass() +inline std::string Factory<ProcessorParamsBase, const std::string&>::getClass() { return "ProcessorParamsFactory"; } template<> -inline std::string Factory<LandmarkBase, YAML::Node>::getClass() +inline std::string Factory<LandmarkBase, const YAML::Node&>::getClass() { return "LandmarkFactory"; } diff --git a/src/sensor_factory.cpp b/src/sensor_factory.cpp deleted file mode 100644 index ef701239009ee3cbbf8a4569b732c9e0c3b73079..0000000000000000000000000000000000000000 --- a/src/sensor_factory.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/** - * \file sensor_factory.cpp - * - * Created on: Apr 25, 2016 - * \author: jsola - */ - -#include "sensor_factory.h" - -#include <iostream> -#include <iomanip> - -namespace wolf -{ - -bool SensorFactory::registerCreator(const std::string& _sensor_type, CreateSensorCallback createFn) -{ - bool reg = callbacks_.insert(CallbackMap::value_type(_sensor_type, createFn)).second; - if (reg) - std::cout << std::setw(22) << std::left << "SensorFactory" << " : registered " << _sensor_type << std::endl; - else - std::cout << "SensorFactory : sensor " << _sensor_type << " already registered. Skipping. " << std::endl; - return reg; -} - -bool SensorFactory::unregisterCreator(const std::string& _sensor_type) -{ - return callbacks_.erase(_sensor_type) == 1; -} - -SensorBase* SensorFactory::create(const std::string& _sensor_type, const std::string& _name, const Eigen::VectorXs& _extrinsics, const IntrinsicsBase* _intrinsics) -{ - CallbackMap::const_iterator creator_callback_it = callbacks_.find(_sensor_type); - if (creator_callback_it == callbacks_.end()) - { - // not found - throw std::runtime_error("Unknown Sensor type"); - } - // Invoke the creation function - return (creator_callback_it->second)(_name, _extrinsics, _intrinsics); -} - -// Singleton --------------------------------------------------- -// This class is a singleton. The code below guarantees this. - -SensorFactory& SensorFactory::get() // Unique point of access; -{ - static SensorFactory instance_; - return instance_; -} - -} /* namespace wolf */ diff --git a/src/sensor_factory.h b/src/sensor_factory.h index 62c3ef6ce1260bfb19289ab9d48ecea4c542d7c3..1bcb563b9d89b72263991730773b2158cc3a7ce0 100644 --- a/src/sensor_factory.h +++ b/src/sensor_factory.h @@ -16,6 +16,7 @@ struct IntrinsicsBase; // wolf #include "wolf.h" +#include "factory.h" // std #include <string> @@ -211,32 +212,22 @@ namespace wolf * * You can also check the code in the example file ````src/examples/test_wolf_factories.cpp````. */ -class SensorFactory -{ - public: - typedef SensorBase* (*CreateSensorCallback)(const std::string & _unique_name, const Eigen::VectorXs& _extrinsics, const IntrinsicsBase* _intrinsics); - private: - typedef std::map<std::string, CreateSensorCallback> CallbackMap; - public: - bool registerCreator(const std::string& _sensor_type, CreateSensorCallback createFn); - bool unregisterCreator(const std::string& _sensor_type); - SensorBase* create(const std::string& _sensor_type, const std::string& _unique_name, const Eigen::VectorXs& _extrinsics, const IntrinsicsBase* _intrinsics = nullptr); - private: - CallbackMap callbacks_; - // Singleton --------------------------------------------------- - // This class is a singleton. The code below guarantees this. - // See: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern - public: - static SensorFactory& get(); // Unique point of access +typedef Factory<SensorBase, + const std::string&, + const Eigen::VectorXs&, const IntrinsicsBase*> SensorFactory; + +template<> +inline std::string Factory<SensorBase, + const std::string&, + const Eigen::VectorXs&, const IntrinsicsBase*>::getClass() +{ + return "SensorFactory"; +} - public: // see http://stackoverflow.com/questions/1008019/c-singleton-design-pattern - SensorFactory(const SensorFactory&) = delete; - void operator=(SensorFactory const&) = delete; - private: - SensorFactory() { } - ~SensorFactory() { } -}; +#define WOLF_REGISTER_SENSOR(SensorType, SensorName) \ + namespace wolf{ namespace{ const bool SensorName##Registered = \ + SensorFactory::get().registerCreator(SensorType, SensorName::create); }}\ } /* namespace wolf */ diff --git a/src/sensor_laser_2D.cpp b/src/sensor_laser_2D.cpp index 92e5cf14a8c7431b7ad36c6c46625cae030bac5b..aa89b092c94edd820e15e2a6c9ed938afef77c6a 100644 --- a/src/sensor_laser_2D.cpp +++ b/src/sensor_laser_2D.cpp @@ -87,7 +87,9 @@ namespace wolf { //} namespace { -const bool registered_laser = SensorFactory::get().registerCreator("LASER 2D", SensorLaser2D::create); +//const bool registered_laser = SensorFactory::get().registerCreator("LASER 2D", SensorLaser2D::create); //const bool registered_laser_params = IntrinsicsFactory::get().registerCreator("LASER 2D", createIntrinsicsLaser2D); + +WOLF_REGISTER_SENSOR("LASER 2D", SensorLaser2D) } } // namespace wolf