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 (BOOST_AUTO(it, params.begin()); it != params.end() ;++it)
105 if (instance->parametersUsed.find(it->first) == instance->parametersUsed.end()){
107 (boost::format(
"Parameter %1% for module %2% was set but is not used") % it->first % className).str()
116 return C::description();
120 return C::availableParameters();
130 for (BOOST_AUTO(it, params.begin()); it != params.end() ;++it)
132 (boost::format(
"Parameter %1% was set but module %2% dos not use any parameter") % it->first % className).str()
134 return std::make_shared<C>();
138 return C::description();
154 for (BOOST_AUTO(it, classes.begin()); it != classes.end(); ++it)
160 classes[name] = descriptor;
166 BOOST_AUTO(it, classes.find(name));
167 if (it == classes.end())
169 std::cerr <<
"No element named " << name <<
" is registered. Known ones are:\n";
172 (boost::format(
"Trying to instanciate unknown element %1% from registrar") % name).str()
181 return getDescriptor(name)->createInstance(name, params);
192 return create(name, params);
198 return getDescriptor(name)->description();
202 void dump(std::ostream &stream)
const 204 for (BOOST_AUTO(it, classes.begin()); it != classes.end(); ++it)
205 stream <<
"- " << it->first <<
"\n";
209 typename DescriptorMap::const_iterator
begin()
const 211 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, new 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, new 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.
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
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.
const ClassDescriptor * getDescriptor(const std::string &name) const
Return a descriptor following a name, throw an exception if name is invalid.
~Registrar()
Destructor, remove all classes descriptors.
void reg(const std::string &name, ClassDescriptor *descriptor)
Register a class by storing an instance of a descriptor helper class.
An exception thrown when one tries to fetch the value of an unexisting parameter. ...
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.
Base * createInstance(const std::string &derived_class_name, ClassLoader *loader)
std::map< std::string, ClassDescriptor * > DescriptorMap
descriptors for sub-classes of Interface, indexed by their names
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.