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
27#include <string> // for string
28
29#include <Ice/Handle.h> // for Handle
30
32#include <ArmarXCore/core/ManagedIceObject.h> // for ManagedIceObject
39
40// Forward declarations
41namespace Ice
42{
44}
45
46namespace 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 * @class ComponentPropertyDefinitions
63 * @brief Default component property definition container.
64 * @ingroup Component
65 * @see properties
66 *
67 * Inherit from this class to extend your property definitions for components!
68 */
70 {
71 public:
72 ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter = true);
73 };
74
75 /**
76 * @class Component
77 * @brief Baseclass for all ArmarX ManagedIceObjects requiring properties.
78 * @ingroup Component
79 *
80 * Components are the equivalents to ManagedIceObjects with additional properties.
81 * Usually, an ArmarX distributed application consists mainly of Component instances,
82 * which are configured on startup through properties specified in a config file
83 * (@see ArmarXCore-HowTos-generate-scenarios).
84 *
85 * It is also possible for components to be reconfigured at runtime.
86 * To prepare your component for this feature you need to override the
87 * Component::componentPropertiesUpdated() method, which is called every time properties change.
88 *
89 * The lifecycle of each Component is managed by an instance of ArmarXManager.
90 */
92 virtual public ManagedIceObject,
93 virtual public PropertyUser
94 {
95 public:
96 /**
97 * Factory method for a component. Sets the specified properties.
98 *
99 * The properties are identified using a config domain, a config name and a property name.
100 * The identifier of properties follows the rule:
101 *
102 * configDomain.configName.propertyName
103 *
104 * The configName is used as name of the well-known object in Ice.
105 * @see ManagedIceObject::ManagedIceObject(std::string name)
106 * This name can be changed by setting the property:
107 *
108 * configDomain.configName.ObjectName=<desiredname>
109 *
110 * @param configName name of the properties identifier
111 * @param properties properties for the object
112 * @param configDomain domain of the properties identifier
113 */
114 template <class T, class TPtr = IceInternal::Handle<T>>
115 static TPtr
117 const std::string& configName = "",
118 const std::string& configDomain = "ArmarX")
119 {
121 ARMARX_DEBUG_S << "create component (configName = " << configName
122 << ", configDomain = " << configDomain << " of type "
123 << GetTypeString<T>();
124 TPtr ptr = new T();
125 ptr->createdByComponentCreate = true;
126
127 ComponentPtr compPtr = ptr;
128
129 ARMARX_DEBUG_S << "initializing properties";
130 if (configName == "")
131 {
132 ptr->initializeProperties(compPtr->getDefaultName(), properties, configDomain);
133 }
134 else
135 {
136 ptr->initializeProperties(configName, properties, configDomain);
137 }
138 ARMARX_DEBUG_S << "call post ini properties hook";
139 compPtr->icePropertiesInitialized();
140 ARMARX_DEBUG_S << "returning component";
141 return ptr;
142 }
143
144 /// @see PropertyUser::getProperty()
146
147 /**
148 * @brief Implement this function if you would like to react to changes in the properties
149 * @param changedProperties Set of changed properties. Retrieve the values with getProperty()
150 */
151 virtual void componentPropertiesUpdated(const std::set<std::string>& changedProperties);
152
153 virtual void preOnInitComponent() override;
154 virtual void preOnConnectComponent() override;
155
156 /**
157 * @brief initializes the properties of this component.
158 * Must not be called after the component was added to the ArmarXManager.
159 * Use with caution!
160 * @param configName
161 * @param properties
162 * @param configDomain
163 */
164 void initializeProperties(const std::string& configName,
165 Ice::PropertiesPtr const& properties,
166 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,
199 bool orderedPublishing = false);
200
201 /**
202 * @brief Use a proxy whose name is specified by the given property.
203 * @param name Name of the property specifying the proxy name.
204 * @param endpoints See ManagedIceObject::usingProxy().
205 * @return See ManagedIceObject::usingProxy().
206 * @see ManagedIceObject::usingProxy()
207 * @see PropertyUser::getProperty()
208 */
209 bool usingProxyFromProperty(const std::string& propertyName,
210 const std::string& endpoints = "");
211
212 /**
213 * @brief Get a topic proxy whose name is specified by the given property.
214 * @param name Name of the property specifying the proxy name.
215 * @return The topic proxy. (See ManagedIceObject::getProxy().)
216 * @see ManagedIceObject::getTopic()
217 * @see PropertyUser::getProperty()
218 */
219 template <class TopicProxyType>
220 TopicProxyType
221 getTopicFromProperty(const std::string& propertyName)
222 {
224 }
225
226 template <class TopicProxyType>
227 void
228 getTopicFromProperty(TopicProxyType& top, const std::string& propertyName)
229 {
231 }
232
233 /**
234 * @brief Get a proxy whose name is specified by the given property.
235 * @param name Name of the property specifying the proxy name.
236 * @return The proxy. (See ManagedIceObject::getProxy().)
237 * @see ManagedIceObject::getProxy()
238 * @see PropertyUser::getProperty()
239 */
240 template <class ProxyType>
242 getProxyFromProperty(const std::string& propertyName,
243 bool addToDependencies = false,
244 const std::string& endpoints = "",
245 bool throwOnProxyError = true)
246 {
248 addToDependencies,
249 endpoints,
250 throwOnProxyError);
251 }
252
253 template <class ProxyType>
254 void
256 const std::string& propertyName,
257 bool addToDependencies = false,
258 const std::string& endpoints = "",
259 bool throwOnProxyError = true)
260 {
261 proxy = getProxy<ProxyType>(getProperty<std::string>(propertyName),
262 addToDependencies,
263 endpoints,
264 throwOnProxyError);
265 }
266
267
268 protected:
269 /// Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
270 Component();
271
272
273 /// @see PropertyUser::createPropertyDefinitions()
274 PropertyDefinitionsPtr createPropertyDefinitions() override;
275
276 /**
277 * Retrieve config domain for this component as set in constructor. The configdomain
278 * defines which properties are used from the properties read from commandline or config file.
279 * The name of the used properties follows the rule:
280 *
281 * configDomain.configName.propertyName
282 */
283 std::string getConfigDomain();
284
285 /**
286 * Retrieve config name for this component as set in constructor. The configname
287 * defines which properties are used from the properties read from commandline or config file.
288 * The name of the properties follows the rule:
289 *
290 * configDomain.configName.propertyName
291 */
292 std::string getConfigName();
293
294 /**
295 * Retrieve config identifier for this component as set in constructor. The config identifier
296 * defines which properties are used from the properties read from commandline or config file.
297 * The name of the properties follows the rule:
298 *
299 * configDomain.configName.propertyName
300 *
301 * where the configIdentifier corresponds to
302 *
303 * configDomain.configName
304 */
305 std::string getConfigIdentifier();
306
307 virtual void
311
312 /**
313 * @brief Add additional property users here that should show up in the application help text.
314 * This function needs to be called in the icePropertiesInitialized from the component user.
315 * @param subPropertyUser
316 */
317 void addPropertyUser(const PropertyUserPtr& subPropertyUser);
318
319
320 private:
321 friend class ArmarXManager;
322
323 /**
324 * This method is called when new properties are set with Component::setIceProperties().
325 * It calls Component::componentPropertiesUpdated(), which can be overriden by subclasses
326 * to perform actions when properties change.
327 */
328 void icePropertiesUpdated(const std::set<std::string>& changedProperties) override final;
329
330
331 // idenfitication of properties
332 std::string configDomain;
333 std::string configName;
334
335 std::vector<PropertyUserPtr> additionalPropertyUsers;
336
337
338 /**
339 * @brief this flag is used by ArmarXManager::addObject to check whether a component was
340 * created using Component::create.
341 */
342 bool createdByComponentCreate;
343 };
344
345} // namespace armarx
#define ARMARXCORE_IMPORT_EXPORT
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition Component.cpp:66
friend class ArmarXManager
Definition Component.h:321
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:116
TopicProxyType getTopicFromProperty(const std::string &propertyName)
Get a topic proxy whose name is specified by the given property.
Definition Component.h:221
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:242
void getProxyFromProperty(ProxyType &proxy, const std::string &propertyName, bool addToDependencies=false, const std::string &endpoints="", bool throwOnProxyError=true)
Definition Component.h:255
virtual void icePropertiesInitialized()
Definition Component.h:308
Property< PropertyType > getProperty(const std::string &name)
void getTopicFromProperty(TopicProxyType &top, const std::string &propertyName)
Definition Component.h:228
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
ManagedIceObject(ManagedIceObject const &other)
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
#define ARMARX_DEBUG_S
The logging level for output that is only interesting while debugging.
Definition Logging.h:205
Ice::PropertiesPtr createProperties()
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceUtil::Handle< class PropertyUser > PropertyUserPtr
PropertyUser smart pointer type.
IceInternal::Handle< Component > ComponentPtr
Component smart pointer type.
Definition ArmarXFwd.h:45
#define ARMARX_TRACE
Definition trace.h:77