Skip to content
Snippets Groups Projects
Commit b5b1898d authored by Jeremie Deray's avatar Jeremie Deray
Browse files

replace ProcessorFactory class with new factory typedef

parent c7f68315
No related branches found
No related tags found
1 merge request!84replace ProcessorFactory class with new Factory typedef
...@@ -217,7 +217,6 @@ SET(SRCS ...@@ -217,7 +217,6 @@ SET(SRCS
node_base.cpp node_base.cpp
problem.cpp problem.cpp
processor_base.cpp processor_base.cpp
processor_factory.cpp
processor_imu.cpp processor_imu.cpp
processor_odom_2D.cpp processor_odom_2D.cpp
processor_odom_3D.cpp processor_odom_3D.cpp
......
/**
* \file processor_factory.cpp
*
* Created on: May 4, 2016
* \author: jsola
*/
#include "processor_factory.h"
#include <iostream>
#include <iomanip>
namespace wolf
{
bool ProcessorFactory::registerCreator(const std::string& _processor_type, CreateProcessorCallback createFn)
{
bool reg = callbacks_.insert(CallbackMap::value_type(_processor_type, createFn)).second;
if (reg)
std::cout << std::setw(22) << std::left << "ProcessorFactory" << " : registered " << _processor_type << std::endl;
else
std::cout << "ProcessorFactory : processor " << _processor_type << " already registered. Skipping. " << std::endl;
return reg;
}
bool ProcessorFactory::unregisterCreator(const std::string& _processor_type)
{
return callbacks_.erase(_processor_type) == 1;
}
ProcessorBase* ProcessorFactory::create(const std::string& _processor_type, const std::string& _name, const ProcessorParamsBase* _params)
{
CallbackMap::const_iterator creator_callback_it = callbacks_.find(_processor_type);
if (creator_callback_it == callbacks_.end())
{
// not found
throw std::runtime_error("Unknown Processor type");
}
// Invoke the creation function
return (creator_callback_it->second)(_name, _params);
}
ProcessorFactory& ProcessorFactory::get() // Unique point of access;
{
static ProcessorFactory instance_; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance_;
}
} /* namespace wolf */
...@@ -16,6 +16,7 @@ struct ProcessorParamsBase; ...@@ -16,6 +16,7 @@ struct ProcessorParamsBase;
// wolf // wolf
#include "wolf.h" #include "wolf.h"
#include "factory.h"
// std // std
#include <string> #include <string>
...@@ -166,32 +167,18 @@ namespace wolf ...@@ -166,32 +167,18 @@ namespace wolf
* *
* You can also check the code in the example file ````src/examples/test_wolf_factories.cpp````. * You can also check the code in the example file ````src/examples/test_wolf_factories.cpp````.
*/ */
class ProcessorFactory
{
public:
typedef ProcessorBase* (*CreateProcessorCallback)(const std::string& _unique_name, const ProcessorParamsBase* _params);
private:
typedef std::map<std::string, CreateProcessorCallback> CallbackMap;
public:
bool registerCreator(const std::string& _processor_type, CreateProcessorCallback createFn);
bool unregisterCreator(const std::string& _processor_type);
ProcessorBase* create(const std::string& _processor_type, const std::string& _unique_name, const ProcessorParamsBase* _params = nullptr);
private:
CallbackMap callbacks_;
// Singleton --------------------------------------------------- typedef Factory<ProcessorBase, const std::string&, const ProcessorParamsBase*> ProcessorFactory;
// This class is a singleton. The code below guarantees this.
// See: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern template<>
public: inline std::string ProcessorFactory::getClass()
static ProcessorFactory& get(); // Unique point of access {
return "ProcessorFactory";
}
public: // see http://stackoverflow.com/questions/1008019/c-singleton-design-pattern #define WOLF_REGISTER_PROCESSOR(ProcessorType, ProcessorName) \
ProcessorFactory(const ProcessorFactory&) = delete; namespace wolf{ namespace{ const bool ProcessorName##Registered = \
void operator=(ProcessorFactory const&) = delete; ProcessorFactory::get().registerCreator(ProcessorType, ProcessorName::create); }};\
private:
ProcessorFactory() { }
~ProcessorFactory() { }
};
} /* namespace wolf */ } /* namespace wolf */
......
...@@ -41,9 +41,5 @@ ProcessorBase* ProcessorIMU::create(const std::string& _unique_name, const Proce ...@@ -41,9 +41,5 @@ ProcessorBase* ProcessorIMU::create(const std::string& _unique_name, const Proce
// Register in the SensorFactory // Register in the SensorFactory
#include "processor_factory.h" #include "processor_factory.h"
namespace wolf {
namespace WOLF_REGISTER_PROCESSOR("IMU", ProcessorIMU)
{
const bool registered_prc_imu = ProcessorFactory::get().registerCreator("IMU", ProcessorIMU::create);
}
} // namespace wolf
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment