Component.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 Kai Welke (welke at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 #include <ArmarXCore/core/ManagedIceObject.h> // for ManagedIceObject
35 
36 #include <Ice/Handle.h> // for Handle
37 
38 #include <string> // for string
39 
40 // Forward declarations
41 namespace Ice
42 {
44 }
45 
46 namespace armarx
47 {
48 
49  /**
50  * @defgroup Component ArmarX Component
51  * @ingroup DistributedProcessingGrp
52  * @brief Subclass of ManagedIceObjects which contains additional properties.
53  */
54  class Component;
55 
56  /// Component smart pointer type.
58 
59  class ComponentPlugin;
60 
61 
62  /**
63  * @class ComponentPropertyDefinitions
64  * @brief Default component property definition container.
65  * @ingroup Component
66  * @see properties
67  *
68  * Inherit from this class to extend your property definitions for components!
69  */
72  {
73  public:
74 
75  ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter = true);
76  };
77 
78 
79  /**
80  * @class Component
81  * @brief Baseclass for all ArmarX ManagedIceObjects requiring properties.
82  * @ingroup Component
83  *
84  * Components are the equivalents to ManagedIceObjects with additional properties.
85  * Usually, an ArmarX distributed application consists mainly of Component instances,
86  * which are configured on startup through properties specified in a config file
87  * (@see ArmarXCore-HowTos-generate-scenarios).
88  *
89  * It is also possible for components to be reconfigured at runtime.
90  * To prepare your component for this feature you need to override the
91  * Component::componentPropertiesUpdated() method, which is called every time properties change.
92  *
93  * The lifecycle of each Component is managed by an instance of ArmarXManager.
94  */
96  virtual public ManagedIceObject,
97  virtual public PropertyUser
98  {
99  public:
100  /**
101  * Factory method for a component. Sets the specified properties.
102  *
103  * The properties are identified using a config domain, a config name and a property name.
104  * The identifier of properties follows the rule:
105  *
106  * configDomain.configName.propertyName
107  *
108  * The configName is used as name of the well-known object in Ice.
109  * @see ManagedIceObject::ManagedIceObject(std::string name)
110  * This name can be changed by setting the property:
111  *
112  * configDomain.configName.ObjectName=<desiredname>
113  *
114  * @param configName name of the properties identifier
115  * @param properties properties for the object
116  * @param configDomain domain of the properties identifier
117  */
118  template <class T, class TPtr = IceInternal::Handle<T>>
119  static TPtr create(Ice::PropertiesPtr properties = Ice::createProperties(),
120  const std::string& configName = "", const std::string& configDomain = "ArmarX")
121  {
122  ARMARX_TRACE;
123  ARMARX_DEBUG_S << "create component (configName = " << configName
124  << ", configDomain = " << configDomain << " of type "
125  << GetTypeString<T>();
126  TPtr ptr = new T();
127  ptr->createdByComponentCreate = true;
128 
129  ComponentPtr compPtr = ptr;
130 
131  ARMARX_DEBUG_S << "initializing properties";
132  if (configName == "")
133  {
134  ptr->initializeProperties(compPtr->getDefaultName(), properties, configDomain);
135  }
136  else
137  {
138  ptr->initializeProperties(configName, properties, configDomain);
139  }
140  ARMARX_DEBUG_S << "call post ini properties hook";
141  compPtr->icePropertiesInitialized();
142  ARMARX_DEBUG_S << "returning component";
143  return ptr;
144  }
145 
146  /// @see PropertyUser::getProperty()
148 
149  /**
150  * @brief Implement this function if you would like to react to changes in the properties
151  * @param changedProperties Set of changed properties. Retrieve the values with getProperty()
152  */
153  virtual void componentPropertiesUpdated(const std::set<std::string>& changedProperties);
154 
155  virtual void preOnInitComponent() override;
156  virtual void preOnConnectComponent() override;
157 
158  /**
159  * @brief initializes the properties of this component.
160  * Must not be called after the component was added to the ArmarXManager.
161  * Use with caution!
162  * @param configName
163  * @param properties
164  * @param configDomain
165  */
166  void initializeProperties(const std::string& configName, Ice::PropertiesPtr const& properties, const std::string& configDomain);
167 
168  ///@see \ref PropertyUser
169  void injectPropertyDefinitions(PropertyDefinitionsPtr& props) override;
170 
171  /**
172  * @brief forces the flag to be set to true that the object instance was created by the Component::create function
173  * @note Do not call this function except you know that you are doing.
174  */
175  void forceComponentCreatedByComponentCreateFunc();
176 
177 
178  std::vector<PropertyUserPtr> getAdditionalPropertyUsers() const;
179 
180 
181  // Convenience wrappers for {offering|using|get}{Topic|Proxy}() with name from property.
182 
183  /**
184  * @brief Offer a topic whose name is specified by the given property.
185  * @param name Name of the property specifying the topic name.
186  * @see ManagedIceObject::offeringTopic()
187  * @see PropertyUser::getProperty()
188  */
189  void offeringTopicFromProperty(const std::string& propertyName);
190 
191  /**
192  * @brief Use a topic whose name is specified by the given property.
193  * @param name Name of the property specifying the topic name.
194  * @param orderedPublishing See ManagedIceObject::usingProxy().
195  * @see ManagedIceObject::usingTopic()
196  * @see PropertyUser::getProperty()
197  */
198  void usingTopicFromProperty(const std::string& propertyName, bool orderedPublishing = false);
199 
200  /**
201  * @brief Use a proxy whose name is specified by the given property.
202  * @param name Name of the property specifying the proxy name.
203  * @param endpoints See ManagedIceObject::usingProxy().
204  * @return See ManagedIceObject::usingProxy().
205  * @see ManagedIceObject::usingProxy()
206  * @see PropertyUser::getProperty()
207  */
208  bool usingProxyFromProperty(const std::string& propertyName, const std::string& endpoints = "");
209 
210  /**
211  * @brief Get a topic proxy whose name is specified by the given property.
212  * @param name Name of the property specifying the proxy name.
213  * @return The topic proxy. (See ManagedIceObject::getProxy().)
214  * @see ManagedIceObject::getTopic()
215  * @see PropertyUser::getProperty()
216  */
217  template <class TopicProxyType>
218  TopicProxyType getTopicFromProperty(const std::string& propertyName)
219  {
220  return getTopic<TopicProxyType>(getProperty<std::string>(propertyName));
221  }
222  template <class TopicProxyType>
223  void getTopicFromProperty(TopicProxyType& top, const std::string& propertyName)
224  {
225  top = getTopic<TopicProxyType>(getProperty<std::string>(propertyName));
226  }
227 
228  /**
229  * @brief Get a proxy whose name is specified by the given property.
230  * @param name Name of the property specifying the proxy name.
231  * @return The proxy. (See ManagedIceObject::getProxy().)
232  * @see ManagedIceObject::getProxy()
233  * @see PropertyUser::getProperty()
234  */
235  template <class ProxyType>
236  ProxyType getProxyFromProperty(const std::string& propertyName,
237  bool addToDependencies = false,
238  const std::string& endpoints = "",
239  bool throwOnProxyError = true)
240  {
241  return getProxy<ProxyType>(getProperty<std::string>(propertyName),
242  addToDependencies, endpoints, throwOnProxyError);
243  }
244  template <class ProxyType>
245  void getProxyFromProperty(ProxyType& proxy, const std::string& propertyName,
246  bool addToDependencies = false,
247  const std::string& endpoints = "",
248  bool throwOnProxyError = true)
249  {
250  proxy = getProxy<ProxyType>(getProperty<std::string>(propertyName),
251  addToDependencies, endpoints, throwOnProxyError);
252  }
253 
254 
255  protected:
256 
257  /// Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
258  Component();
259 
260 
261  /// @see PropertyUser::createPropertyDefinitions()
262  PropertyDefinitionsPtr createPropertyDefinitions() override;
263 
264  /**
265  * Retrieve config domain for this component as set in constructor. The configdomain
266  * defines which properties are used from the properties read from commandline or config file.
267  * The name of the used properties follows the rule:
268  *
269  * configDomain.configName.propertyName
270  */
271  std::string getConfigDomain();
272 
273  /**
274  * Retrieve config name for this component as set in constructor. The configname
275  * defines which properties are used from the properties read from commandline or config file.
276  * The name of the properties follows the rule:
277  *
278  * configDomain.configName.propertyName
279  */
280  std::string getConfigName();
281 
282  /**
283  * Retrieve config identifier for this component as set in constructor. The config identifier
284  * defines which properties are used from the properties read from commandline or config file.
285  * The name of the properties follows the rule:
286  *
287  * configDomain.configName.propertyName
288  *
289  * where the configIdentifier corresponds to
290  *
291  * configDomain.configName
292  */
293  std::string getConfigIdentifier();
294 
295 
296 
297  virtual void icePropertiesInitialized() {}
298 
299  /**
300  * @brief Add additional property users here that should show up in the application help text.
301  * This function needs to be called in the icePropertiesInitialized from the component user.
302  * @param subPropertyUser
303  */
304  void addPropertyUser(const PropertyUserPtr& subPropertyUser);
305 
306 
307  private:
308 
309  friend class ArmarXManager;
310 
311  /**
312  * This method is called when new properties are set with Component::setIceProperties().
313  * It calls Component::componentPropertiesUpdated(), which can be overriden by subclasses
314  * to perform actions when properties change.
315  */
316  void icePropertiesUpdated(const std::set<std::string>& changedProperties) override final;
317 
318 
319  // idenfitication of properties
320  std::string configDomain;
321  std::string configName;
322 
323  std::vector<PropertyUserPtr> additionalPropertyUsers;
324 
325 
326  /**
327  * @brief this flag is used by ArmarXManager::addObject to check whether a component was
328  * created using Component::create.
329  */
330  bool createdByComponentCreate;
331  };
332 
333 }
334 
GetTypeString.h
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
armarx::Component::getTopicFromProperty
TopicProxyType getTopicFromProperty(const std::string &propertyName)
Get a topic proxy whose name is specified by the given property.
Definition: Component.h:218
IceInternal::Handle< ::Ice::Properties >
armarx::Component::icePropertiesInitialized
virtual void icePropertiesInitialized()
Definition: Component.h:297
armarx::Component::getProxyFromProperty
ProxyType getProxyFromProperty(const std::string &propertyName, bool addToDependencies=false, const std::string &endpoints="", bool throwOnProxyError=true)
Get a proxy whose name is specified by the given property.
Definition: Component.h:236
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
ManagedIceObject.h
Ice::createProperties
Ice::PropertiesPtr createProperties()
PropertyUser.h
armarx::Component::create
static TPtr create(Ice::PropertiesPtr properties=Ice::createProperties(), const std::string &configName="", const std::string &configDomain="ArmarX")
Factory method for a component.
Definition: Component.h:119
armarx::ProxyType
ProxyType
Definition: ProxyPropertyDefinition.h:41
armarx::PropertyDefinitionContainer
PropertyDefinitionContainer.
Definition: PropertyDefinitionContainer.h:53
armarx::Component::getProxyFromProperty
void getProxyFromProperty(ProxyType &proxy, const std::string &propertyName, bool addToDependencies=false, const std::string &endpoints="", bool throwOnProxyError=true)
Definition: Component.h:245
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
Ice
Definition: DBTypes.cpp:64
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
PropertyDefinition.hpp
PropertyDefinitionContainer.h
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:179
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:37
ImportExport.h
armarx::Component::getTopicFromProperty
void getTopicFromProperty(TopicProxyType &top, const std::string &propertyName)
Definition: Component.h:223
armarx::ComponentPtr
IceInternal::Handle< Component > ComponentPtr
Component smart pointer type.
Definition: ArmarXFwd.h:45
ComponentPlugin.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::PropertyUser
Abstract PropertyUser class.
Definition: PropertyUser.h:62
armarx::ArmarXManager
Main class of an ArmarX process.
Definition: ArmarXManager.h:97
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
PropertyDefinition.h