Registrar.h
Go to the documentation of this file.
1 // kate: replace-tabs off; indent-width 4; indent-mode normal
2 // vim: ts=4:sw=4:noexpandtab
3 /*
4 
5 Copyright (c) 2010--2012,
6 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
7 You can contact the authors at <f dot pomerleau at gmail dot com> and
8 <stephane at magnenat dot net>
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of the <organization> nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef __POINTMATCHER_REGISTRAR_H
37 #define __POINTMATCHER_REGISTRAR_H
38 
39 #include "Parametrizable.h"
40 #include "PointMatcher.h"
41 
42 #include <yaml-cpp/yaml.h>
43 #include <boost/format.hpp>
44 #include <boost/typeof/typeof.hpp>
45 
46 namespace PointMatcherSupport
47 {
49  void getNameParamsFromYAML(const YAML::Node& module, std::string& name, Parametrizable::Parameters& params);
50 
52  struct InvalidElement: std::runtime_error
53  {
54  InvalidElement(const std::string& reason);
55  };
56 
58  template<typename Interface>
59  struct Registrar
60  {
61  public:
62  typedef Interface TargetType;
63 
66  {
68  virtual ~ClassDescriptor() {}
70  virtual std::shared_ptr<Interface> createInstance(const std::string& className, const Parametrizable::Parameters& params) const = 0;
72  virtual const std::string description() const = 0;
74  virtual const Parametrizable::ParametersDoc availableParameters() const = 0;
75  };
76 
78  template<typename C>
80  {
81  virtual std::shared_ptr<Interface> createInstance(const std::string& className, const Parametrizable::Parameters& params) const
82  {
83  std::shared_ptr<C> instance = std::make_shared<C>(params);
84 
85  // check that all parameters were set
86  for (const auto& param : params)
87  {
88  if (instance->parametersUsed.find(param.first) == instance->parametersUsed.end()){
90  (boost::format("Parameter %1% for module %2% was set but is not used") % param.first % className).str()
91  );
92  }
93  }
94  return instance;
95  }
96 
97  virtual const std::string description() const
98  {
99  return C::description();
100  }
101 
103  {
104  return C::availableParameters();
105  }
106  };
107 
109  template<typename C>
111  {
112  virtual std::shared_ptr<Interface> createInstance(const std::string& className, const Parametrizable::Parameters& params) const
113  {
114  for (const auto& param : params)
116  (boost::format("Parameter %1% was set but module %2% does not use any parameter") % param.first % className).str()
117  );
118 
119  return std::make_shared<C>();
120  }
121 
122  virtual const std::string description() const
123  {
124  return C::description();
125  }
126 
128  {
130  }
131  };
132 
133  protected:
134  typedef std::map<std::string, std::shared_ptr<ClassDescriptor>> DescriptorMap;
136 
137  public:
139  void reg(const std::string &name, std::shared_ptr<ClassDescriptor> descriptor)
140  {
141  classes.insert(std::make_pair(name, descriptor));
142  }
143 
145  std::shared_ptr<ClassDescriptor> getDescriptor(const std::string& name) const
146  {
147  auto it = classes.find(name);
148 
149  if (it == classes.end())
150  {
151  std::cerr << "No element named " << name << " is registered. Known ones are:\n";
152  dump(std::cerr);
153  throw InvalidElement(
154  (boost::format("Trying to instanciate unknown element %1% from registrar") % name).str()
155  );
156  }
157  return it->second;
158  }
159 
161  std::shared_ptr<Interface> create(const std::string& name, const Parametrizable::Parameters& params = Parametrizable::Parameters()) const
162  {
163  return getDescriptor(name)->createInstance(name, params);
164  }
165 
167  std::shared_ptr<Interface> createFromYAML(const YAML::Node& module) const
168  {
171 
173 
174  return create(name, params);
175  }
176 
179  {
180  return getDescriptor(name)->description();
181  }
182 
184  void dump(std::ostream &stream) const
185  {
186  for (const auto& it : classes)
187  stream << "- " << it.first << "\n";
188  }
189 
191  typename DescriptorMap::const_iterator begin() const
192  {
193  return classes.begin();
194  }
195 
197  typename DescriptorMap::const_iterator end() const
198  {
199  return classes.end();
200  }
201  };
202 
203  #define REG(name) name##Registrar
204  #define DEF_REGISTRAR(name) PointMatcherSupport::Registrar< name > name##Registrar;
205  #define DEF_REGISTRAR_IFACE(name, ifaceName) PointMatcherSupport::Registrar< ifaceName > name##Registrar;
206  #define ADD_TO_REGISTRAR(name, elementName, element) { \
207  typedef typename PointMatcherSupport::Registrar< name >::template GenericClassDescriptor< element > Desc; \
208  name##Registrar.reg(# elementName, std::make_shared<Desc>() ); \
209  }
210  #define ADD_TO_REGISTRAR_NO_PARAM(name, elementName, element) { \
211  typedef typename PointMatcherSupport::Registrar< name >::template GenericClassDescriptorNoParam< element > Desc; \
212  name##Registrar.reg(# elementName, std::make_shared<Desc>() ); \
213  }
214 } // namespace PointMatcherSupport
215 
216 #endif // __POINTMATCHER_REGISTRAR_H
PointMatcherSupport::Registrar::getDescription
const std::string getDescription(const std::string &name) const
Get the description of a class.
Definition: Registrar.h:178
PointMatcherSupport::Registrar::GenericClassDescriptorNoParam::availableParameters
virtual const Parametrizable::ParametersDoc availableParameters() const
Return the available parameters for this class.
Definition: Registrar.h:127
PointMatcherSupport::Registrar::GenericClassDescriptor::description
virtual const std::string description() const
Return the description of this class.
Definition: Registrar.h:97
PointMatcherSupport::InvalidElement::InvalidElement
InvalidElement(const std::string &reason)
Construct an invalid-element exception.
Definition: Registry.cpp:55
PointMatcherSupport::Registrar::ClassDescriptor
The interface for class descriptors.
Definition: Registrar.h:65
icp_customized.name
string name
Definition: icp_customized.py:45
PointMatcherSupport::Registrar::TargetType
Interface TargetType
alias to recover the template parameter
Definition: Registrar.h:62
PointMatcherSupport::Registrar::classes
DescriptorMap classes
known classes that can be constructed
Definition: Registrar.h:135
PointMatcherSupport::Registrar::ClassDescriptor::createInstance
virtual std::shared_ptr< Interface > createInstance(const std::string &className, const Parametrizable::Parameters &params) const =0
Create an instance of Interface using params.
PointMatcherSupport::Registrar::getDescriptor
std::shared_ptr< ClassDescriptor > getDescriptor(const std::string &name) const
Return a descriptor following a name, throw an exception if name is invalid.
Definition: Registrar.h:145
Parametrizable.h
PointMatcherSupport::Registrar::ClassDescriptor::availableParameters
virtual const Parametrizable::ParametersDoc availableParameters() const =0
Return the available parameters for this class.
testing::internal::string
::std::string string
Definition: gtest.h:1979
PointMatcherSupport::Registrar::GenericClassDescriptor::availableParameters
virtual const Parametrizable::ParametersDoc availableParameters() const
Return the available parameters for this class.
Definition: Registrar.h:102
PointMatcherSupport::Registrar::createFromYAML
std::shared_ptr< Interface > createFromYAML(const YAML::Node &module) const
Create an instance from a YAML node.
Definition: Registrar.h:167
PointMatcherSupport::Parametrizable::ParametersDoc
std::vector< ParameterDoc > ParametersDoc
The documentation of all parameters.
Definition: Parametrizable.h:187
align_sequence.params
params
Definition: align_sequence.py:13
PointMatcherSupport::Registrar::create
std::shared_ptr< Interface > create(const std::string &name, const Parametrizable::Parameters &params=Parametrizable::Parameters()) const
Create an instance.
Definition: Registrar.h:161
PointMatcherSupport::Registrar::end
DescriptorMap::const_iterator end() const
end for const iterator over classes descriptions
Definition: Registrar.h:197
PointMatcherSupport::Registrar::GenericClassDescriptorNoParam::createInstance
virtual std::shared_ptr< Interface > createInstance(const std::string &className, const Parametrizable::Parameters &params) const
Create an instance of Interface using params.
Definition: Registrar.h:112
PointMatcherSupport::getNameParamsFromYAML
void getNameParamsFromYAML(const YAML::Node &module, std::string &name, Parametrizable::Parameters &params)
Retrieve name and parameters from a yaml node.
Definition: Registrar.cpp:7
InvalidParameter
Parametrizable::InvalidParameter InvalidParameter
Definition: pypoint_matcher_helper.h:42
PointMatcherSupport::Registrar::begin
DescriptorMap::const_iterator begin() const
begin for const iterator over classes descriptions
Definition: Registrar.h:191
PointMatcherSupport::Registrar::GenericClassDescriptor::createInstance
virtual std::shared_ptr< Interface > createInstance(const std::string &className, const Parametrizable::Parameters &params) const
Create an instance of Interface using params.
Definition: Registrar.h:81
PointMatcherSupport::Registrar::ClassDescriptor::~ClassDescriptor
virtual ~ClassDescriptor()
Virtual destructor, do nothing.
Definition: Registrar.h:68
PointMatcherSupport::Registrar::GenericClassDescriptorNoParam
A descriptor for a class C that does not provide any parameter.
Definition: Registrar.h:110
PointMatcherSupport::Registrar::dump
void dump(std::ostream &stream) const
Print the list of registered classes to stream.
Definition: Registrar.h:184
PointMatcherSupport::Registrar::ClassDescriptor::description
virtual const std::string description() const =0
Return the description of this class.
PointMatcherSupport::Registrar::GenericClassDescriptor
A descriptor for a class C that provides parameters.
Definition: Registrar.h:79
PointMatcherSupport
Functions and classes that are not dependant on scalar type are defined in this namespace.
Definition: Bibliography.cpp:45
PointMatcherSupport::Registrar::DescriptorMap
std::map< std::string, std::shared_ptr< ClassDescriptor > > DescriptorMap
descriptors for sub-classes of Interface, indexed by their names
Definition: Registrar.h:134
PointMatcher.h
public interface
PointMatcherSupport::Parametrizable::Parameters
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
Definition: Parametrizable.h:199
PointMatcherSupport::Registrar::GenericClassDescriptorNoParam::description
virtual const std::string description() const
Return the description of this class.
Definition: Registrar.h:122
PointMatcherSupport::InvalidElement
An exception thrown when one tries to instanciate an element that does not exist in the registrar.
Definition: Registrar.h:52
PointMatcherSupport::Registrar
A factor for subclasses of Interface.
Definition: Registrar.h:59
PointMatcherSupport::Registrar::reg
void reg(const std::string &name, std::shared_ptr< ClassDescriptor > descriptor)
Register a class by storing an instance of a descriptor helper class.
Definition: Registrar.h:139


libpointmatcher
Author(s):
autogenerated on Mon Jan 1 2024 03:24:43