AbstractFactoryMethod.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Nils Adermann (naderman at naderman dot de)
20 * @date 2009
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <string>
28 #include <map>
29 #include <vector>
30 #include <memory>
32 
33 namespace armarx
34 {
35  /**
36  * A template that can be used as a superclass of a class hierarchy that
37  * wants to provide a factory method which allows instantiation of objects
38  * based on a string identifier.
39  *
40  * The first template argument is the base class of your class hierarchy.
41  * The second argument is the parameter type for the initialisation function
42  * each subclass has to provide. If you need multiple constructor arguments
43  * it is recommended to use a boost::tuple.
44  */
45  template <typename Base, typename constructorArg, typename SharedPointer = std::shared_ptr<Base> >
47  {
48  /**
49  * Classname of this instance, so that one can save in a string which classtype this instance is and recreate it.
50  */
51  std::string className;
52 
53  public:
54  using BaseFactory = Base;
55  using ConstructorArg = constructorArg;
56  using SharedPointerType = SharedPointer;
57  /**
58  * @brief getClassName returns the name that this instance was generated
59  * with.
60  * @return name of class
61  */
62  std::string getClassName()
63  {
64  return className;
65  }
66 
67  /**
68  * The function pointer type of subclass initialisation functions.
69  * This matches the createInstance method.
70  */
71  typedef SharedPointerType(*initialisationFunction)(constructorArg);
72 
73  /**
74  * Function which can be used to retrieve an object specified by string name.
75  */
76  static SharedPointerType fromName(const std::string& name, constructorArg params)
77  {
78  if (subTypes()->find(name) == subTypes()->end())
79  {
80  ARMARX_DEBUG << "factory does not manage a type of name " << name;
81  return SharedPointerType();
82  }
83  ARMARX_DEBUG << "calling initializer for name " << name;
84  SharedPointerType result = subTypes()->at(name)(params);
85  ARMARX_DEBUG << "done initializing object for name " << name;
86  if (result)
87  {
88  result->className = name;
89  }
90  return result;
91  }
92 
93 
94  /**
95  * @brief getAvailableClasses retrieves a list of all registered classes
96  * as their string-representation.
97  * @return list of available classes by their name.
98  */
99  static std::vector<std::string> getAvailableClasses()
100  {
101  std::vector<std::string> result;
102  typename std::map<std::string, initialisationFunction>::iterator it = subTypes()->begin();
103 
104  for (; it != subTypes()->end(); ++it)
105  {
106  result.push_back(it->first);
107  }
108 
109  return result;
110 
111  }
112 
113  /**
114  * Returns the class's name. This is used to identify the class which the
115  * user requests an instance of.
116  */
117  static std::string getName()
118  {
119  return "AbstractFactoryMethod";
120  }
121 
122  /**
123  * Initialisation function which needs to be provided by every subclass.
124  * It calls the constructor and returns a shared_ptr to the resulting
125  * object.
126  */
127  static SharedPointerType createInstance(constructorArg)
128  {
129  return SharedPointerType();
130  }
131 
132 
133 
134  /**
135  * Statically called by subclasses to register their name and initialisation
136  * function so they can be found by {@link fromName fromName}.
137  */
138  static void registerClass(const std::string& name, initialisationFunction init)
139  {
140  ARMARX_VERBOSE << typeid(Base).name() << "Registering factory '" << name << "' for function of type: "
141  << armarx::GetTypeString<Base>();
142 
143  (*subTypes())[name] = init;
144  }
145 
146  /**
147  * A helper struct to allow static initialisation of the subclass lookup table.
148  */
150  {
151  SubClassRegistry(const std::string& name, initialisationFunction init)
152  {
153  Base::registerClass(name, init);
154  }
155  };
156 
157  private:
158  /**
159  * Static wrapper method for accessing subTypes map.
160  * This method is necessary to make certain that the map is initialised
161  * before use. This can only be guaranteed through a static local variable
162  * in a function.
163  */
164  static std::shared_ptr<std::map<std::string, initialisationFunction> > subTypes()
165  {
166  static std::shared_ptr<std::map<std::string, initialisationFunction> > subTypes(new std::map<std::string, initialisationFunction>);
167 
168  return subTypes;
169  }
170  };
171 }
172 
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::AbstractFactoryMethod
A template that can be used as a superclass of a class hierarchy that wants to provide a factory meth...
Definition: AbstractFactoryMethod.h:46
armarx::AbstractFactoryMethod::getClassName
std::string getClassName()
getClassName returns the name that this instance was generated with.
Definition: AbstractFactoryMethod.h:62
armarx::AbstractFactoryMethod::fromName
static SharedPointerType fromName(const std::string &name, constructorArg params)
Function which can be used to retrieve an object specified by string name.
Definition: AbstractFactoryMethod.h:76
armarx::AbstractFactoryMethod::registerClass
static void registerClass(const std::string &name, initialisationFunction init)
Statically called by subclasses to register their name and initialisation function so they can be fou...
Definition: AbstractFactoryMethod.h:138
IceInternal::Handle< XMLStateOffererFactoryBase >
armarx::AbstractFactoryMethod::SubClassRegistry::SubClassRegistry
SubClassRegistry(const std::string &name, initialisationFunction init)
Definition: AbstractFactoryMethod.h:151
armarx::AbstractFactoryMethod::SubClassRegistry
A helper struct to allow static initialisation of the subclass lookup table.
Definition: AbstractFactoryMethod.h:149
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::AbstractFactoryMethod::SharedPointerType
SharedPointer SharedPointerType
Definition: AbstractFactoryMethod.h:56
armarx::AbstractFactoryMethod< XMLStateOffererFactoryBase, StatechartGroupXmlReaderPtr, XMLStateOffererFactoryBasePtr >::ConstructorArg
StatechartGroupXmlReaderPtr ConstructorArg
Definition: AbstractFactoryMethod.h:55
armarx::AbstractFactoryMethod::initialisationFunction
SharedPointerType(* initialisationFunction)(constructorArg)
The function pointer type of subclass initialisation functions.
Definition: AbstractFactoryMethod.h:71
armarx::AbstractFactoryMethod::getAvailableClasses
static std::vector< std::string > getAvailableClasses()
getAvailableClasses retrieves a list of all registered classes as their string-representation.
Definition: AbstractFactoryMethod.h:99
armarx::XMLStateOffererFactoryBase
Definition: XMLRemoteStateOfferer.h:43
Logging.h
armarx::AbstractFactoryMethod::createInstance
static SharedPointerType createInstance(constructorArg)
Initialisation function which needs to be provided by every subclass.
Definition: AbstractFactoryMethod.h:127
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::AbstractFactoryMethod::getName
static std::string getName()
Returns the class's name.
Definition: AbstractFactoryMethod.h:117