ComponentFactories.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
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 #pragma once
25 
26 #include <functional>
27 
28 #include "Component.h"
29 #include "util/Registrar.h"
30 
31 /**
32  * \page ComponentFactoriesDoc Component factories
33  * ArmarX Components can be registered with a name to the \ref armarx::ComponentFactory.
34  * This factory can be used to create components given a name used for registration.
35  *
36  * Use the class \ref armarx::RegisterComponent to perform registration.
37  *
38  * Retrieve a Component factory by calling:
39  * \code
40  * ComponentFactory::get("nameUsedForRegistration")
41  * \endcode
42  * This offers you a function-object behaving like \ref armarx::Component::create.
43  *
44  * As a difference:
45  * The first parameter can also be an Ice::PropertyDict (aka.: std::map<std::string, std::string>) instead of a Properties object.
46  *
47  * @see armarx::RegisterComponent
48  */
49 
50 /**
51  * \defgroup ComponentFactories
52  * \ingroup core-utility
53  */
54 
55 namespace armarx
56 {
57  struct ComponentCreatorObject;
58  /**
59  * @brief holds all registered components
60  * \ingroup ComponentFactories
61  */
63 
64  /**
65  * @brief The ComponentCreatorObject stores the create call for a Component.
66  * It is used with the component factory.
67  * Use the class \ref armarx::RegisterComponent to perform registration.
68  * @see RegisterComponent
69  * \ingroup ComponentFactories
70  */
72  {
73  /**
74  * @brief Calls armarx::Component::create<T> for the type passed to createComponentCreator
75  * @see armarx::Component::create
76  * @return The created component.
77  */
80  const std::string& configName = "",
81  const std::string& configDomain = "ArmarX") const
82  {
83  return create(properties, configName, configDomain);
84  }
85 
86  /**
87  * @brief Creates ice properties form the given dictionary and calls armarx::Component::create<T> for the type passed to createComponentCreator
88  * @param dict The properties given as Ice::PropertyDict
89  * @see armarx::Component::create
90  * @return The created component.
91  */
93  operator()(Ice::PropertyDict dict,
94  const std::string& configName = "",
95  const std::string& configDomain = "ArmarX") const
96  {
97  auto prop = Ice::createProperties();
98  for (const auto& elem : dict)
99  {
100  prop->setProperty(elem.first, elem.second);
101  }
102  return create(std::move(prop), configName, configDomain);
103  }
104 
105  private:
106  template <class ComponentT>
107  friend struct RegisterComponent;
108 
109  /**
110  * @brief The armarx::Component::create function for the represented type
111  */
112  std::function<ComponentPtr(Ice::PropertiesPtr, const std::string&, const std::string&)>
113  create;
114  };
115 
116  /**
117  * @brief A class performing component registration on its construction.
118  *
119  * Register per calling
120  * \code{.cpp}
121  * RegisterComponent<ComponentT> registerVar{ComponentName};
122  * \endcode
123  *
124  * \ingroup ComponentFactories
125  */
126  template <class ComponentT>
128  {
129  RegisterComponent(std::string registrationName)
130  {
131  ComponentCreatorObject componentCreator;
132  componentCreator.create = armarx::Component::create<ComponentT>;
133  ComponentFactory::RegisterElement{registrationName, componentCreator};
134  }
135  };
136 } // namespace armarx
armarx::ComponentCreatorObject::operator()
ComponentPtr operator()(Ice::PropertiesPtr properties=Ice::createProperties(), const std::string &configName="", const std::string &configDomain="ArmarX") const
Calls armarx::Component::create<T> for the type passed to createComponentCreator.
Definition: ComponentFactories.h:79
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ComponentCreatorObject::operator()
ComponentPtr operator()(Ice::PropertyDict dict, const std::string &configName="", const std::string &configDomain="ArmarX") const
Creates ice properties form the given dictionary and calls armarx::Component::create<T> for the type ...
Definition: ComponentFactories.h:93
armarx::Registrar::RegisterElement
Registers the object passed to the constructor.
Definition: Registrar.h:71
Ice::createProperties
Ice::PropertiesPtr createProperties()
Registrar.h
armarx::RegisterComponent::RegisterComponent
RegisterComponent(std::string registrationName)
Definition: ComponentFactories.h:129
Component.h
armarx::ComponentCreatorObject
The ComponentCreatorObject stores the create call for a Component.
Definition: ComponentFactories.h:71
armarx::Registrar
Stores key object pairs.
Definition: Registrar.h:62
armarx::ComponentPtr
IceInternal::Handle< Component > ComponentPtr
Component smart pointer type.
Definition: ArmarXFwd.h:45
armarx::RegisterComponent
A class performing component registration on its construction.
Definition: ComponentFactories.h:127
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27