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