36 #ifndef __POINTMATCHER_REGISTRAR_H 37 #define __POINTMATCHER_REGISTRAR_H 41 #include <boost/format.hpp> 42 #include <boost/typeof/typeof.hpp> 45 #ifdef SYSTEM_YAML_CPP 55 #endif // HAVE_YAML_CPP 59 #ifdef SYSTEM_YAML_CPP 60 namespace YAML = ::YAML;
62 namespace YAML = ::YAML_PM;
75 template<
typename Interface>
100 std::shared_ptr<C> instance = std::make_shared<C>(
params);
103 for (
const auto& param : params)
105 if (instance->parametersUsed.find(param.first) == instance->parametersUsed.end()){
107 (boost::format(
"Parameter %1% for module %2% was set but is not used") % param.first % className).str()
116 return C::description();
121 return C::availableParameters();
131 for (
const auto& param : params)
133 (boost::format(
"Parameter %1% was set but module %2% dos not use any parameter") % param.first % className).str()
136 return std::make_shared<C>();
141 return C::description();
151 typedef std::map<std::string, std::shared_ptr<ClassDescriptor>>
DescriptorMap;
158 classes.insert(std::make_pair(name, descriptor));
164 auto it = classes.find(name);
166 if (it == classes.end())
168 std::cerr <<
"No element named " << name <<
" is registered. Known ones are:\n";
171 (boost::format(
"Trying to instanciate unknown element %1% from registrar") % name).str()
180 return getDescriptor(name)->createInstance(name,
params);
191 return create(name, params);
197 return getDescriptor(name)->description();
201 void dump(std::ostream &stream)
const 203 for (
const auto& it : classes)
204 stream <<
"- " << it.first <<
"\n";
208 typename DescriptorMap::const_iterator
begin()
const 210 return classes.begin();
214 typename DescriptorMap::const_iterator
end()
const 216 return classes.end();
220 #define REG(name) name##Registrar 221 #define DEF_REGISTRAR(name) PointMatcherSupport::Registrar< name > name##Registrar; 222 #define DEF_REGISTRAR_IFACE(name, ifaceName) PointMatcherSupport::Registrar< ifaceName > name##Registrar; 223 #define ADD_TO_REGISTRAR(name, elementName, element) { \ 224 typedef typename PointMatcherSupport::Registrar< name >::template GenericClassDescriptor< element > Desc; \ 225 name##Registrar.reg(# elementName, std::make_shared<Desc>() ); \ 227 #define ADD_TO_REGISTRAR_NO_PARAM(name, elementName, element) { \ 228 typedef typename PointMatcherSupport::Registrar< name >::template GenericClassDescriptorNoParam< element > Desc; \ 229 name##Registrar.reg(# elementName, std::make_shared<Desc>() ); \ 233 #endif // __POINTMATCHER_REGISTRAR_H DescriptorMap::const_iterator end() const
end for const iterator over classes descriptions
An exception thrown when one tries to instanciate an element that does not exist in the registrar...
std::shared_ptr< Interface > create(const std::string &name, const Parametrizable::Parameters ¶ms=Parametrizable::Parameters()) const
Create an instance.
virtual ~ClassDescriptor()
Virtual destructor, do nothing.
A factor for subclasses of Interface.
virtual const std::string description() const
Return the description of this class.
virtual const Parametrizable::ParametersDoc availableParameters() const
Return the available parameters for this class.
A descriptor for a class C that provides parameters.
The interface for class descriptors.
A descriptor for a class C that does not provide any parameter.
Interface TargetType
alias to recover the template parameter
virtual std::shared_ptr< Interface > createInstance(const std::string &className, const Parametrizable::Parameters ¶ms) const
Create an instance of Interface using params.
DescriptorMap classes
known classes that can be constructed
std::shared_ptr< Interface > createFromYAML(const YAML::Node &module) const
Create an instance from a YAML node.
void reg(const std::string &name, std::shared_ptr< ClassDescriptor > descriptor)
Register a class by storing an instance of a descriptor helper class.
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
std::map< std::string, std::shared_ptr< ClassDescriptor > > DescriptorMap
descriptors for sub-classes of Interface, indexed by their names
Functions and classes that are not dependant on scalar type are defined in this namespace.
void getNameParamsFromYAML(const YAML::Node &module, std::string &name, Parametrizable::Parameters ¶ms)
Retrieve name and parameters from a yaml node.
virtual std::shared_ptr< Interface > createInstance(const std::string &className, const Parametrizable::Parameters ¶ms) const
Create an instance of Interface using params.
DescriptorMap::const_iterator begin() const
begin for const iterator over classes descriptions
std::vector< ParameterDoc > ParametersDoc
The documentation of all parameters.
virtual const Parametrizable::ParametersDoc availableParameters() const
Return the available parameters for this class.
std::shared_ptr< ClassDescriptor > getDescriptor(const std::string &name) const
Return a descriptor following a name, throw an exception if name is invalid.
Parametrizable::InvalidParameter InvalidParameter
void dump(std::ostream &stream) const
Print the list of registered classes to stream.
virtual const std::string description() const
Return the description of this class.
const std::string getDescription(const std::string &name) const
Get the description of a class.