PropertyUser.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 Jan Issac (jan dot issac at gmx dot de)
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 <atomic>
28#include <string>
29#include <vector>
30
31#include <IceUtil/Handle.h> // for Handle, HandleBase
32#include <IceUtil/Shared.h> // for Shared
33
36
37#include "Property.h" // for Property
39
40namespace armarx
41{
42 class PropertyUser;
43 using PropertyUserPtr = IceUtil::Handle<PropertyUser>;
45
46 using PropertyDefinitionsPtr = IceUtil::Handle<PropertyDefinitionContainer>;
47
48 /* ====================================================================== */
49 /* === PropertyUser Declaration ========================================= */
50 /* ====================================================================== */
51
52 /**
53 * @class PropertyUser
54 * @brief Abstract PropertyUser class
55 *
56 * PropertyUser provides the interface to create a PropertyDefinition as
57 * well as the access to each property which has been defined.
58 *
59 * Properties can be updated by calling PropertyUser::setIceProperties().
60 * A class inheriting from PropertyUser can react to property changes by
61 * overriding PropertyUser::propertiesUpdated();
62 */
63 class PropertyUser : public virtual IceUtil::Shared
64 {
65 public:
67
68 ~PropertyUser() override;
69
70 /**
71 * Property creation and retrieval
72 *
73 * @param name Requested property name (note: without prefix)
74 *
75 * @return Property instance
76 */
77 template <typename PropertyType>
78 Property<PropertyType> getProperty(const std::string& name);
79
80 /**
81 * Hack to allow using getProperty in `const`-modified methods.
82 */
83 template <typename PropertyType>
85 getProperty(const std::string& name) const
86 {
87 return const_cast<PropertyUser&>(*this).getProperty<PropertyType>(name);
88 }
89
90 template <class T>
91 void getProperty(T& val, const std::string& name) const;
92
93 template <class T>
94 void getProperty(std::atomic<T>& val, const std::string& name) const;
95
96 void updateProperties();
98 std::vector<std::string> getComponentProxyNames();
99 std::vector<std::string> getTopicProxyNames();
100 std::vector<std::string> getSubscribedTopicNames();
101
102 template <class ContainerT>
103 void getPropertyAsCSV(ContainerT& val,
104 const std::string& name,
105 const std::string& splitBy = ",;",
106 bool trimElements = true,
107 bool removeEmptyElements = true);
108
109 template <class T>
110 std::vector<T> getPropertyAsCSV(const std::string& name,
111 const std::string& splitBy = ",;",
112 bool trimElements = true,
113 bool removeEmptyElements = true);
114
115 bool hasProperty(const std::string& name);
116
117 /**
118 * Creates the property definition container.
119 * @note Implement this factory function to create your own definitions.
120 *
121 * @return Property definition container pointer
122 */
124
125 /**
126 * This method is called when new Properties are set via setIceProperties().
127 * Each class deriving from PropertyUser can overwrite this method in order
128 * to react to changed properties.
129 */
130 virtual void icePropertiesUpdated(const std::set<std::string>& changedProperties);
131
132 /**
133 * Returns the component's property definition container
134 *
135 * @return Property definition container
136 */
138
139 /**
140 * @brief Called after \ref createPropertyDefinitions by Component to
141 * inject propertes of \ref ComponentPlugin
142 *
143 */
145
146 /**
147 * Returns the set of Ice properties.
148 *
149 * @return Pointer to the Ice::Properties set.
150 */
152
153 /**
154 * Sets the Ice properties.
155 *
156 * @param properties Ice Properties.
157 */
158 virtual void setIceProperties(Ice::PropertiesPtr properties);
159
160 virtual void updateIceProperties(const std::map<std::string, std::string>& changes);
161
162 bool tryAddProperty(const std::string& propertyName, const std::string& value);
163
164 private:
165 /**
166 * Component related properties
167 */
168 Ice::PropertiesPtr properties;
169
170 struct Impl;
171 std::unique_ptr<Impl> impl;
172 };
173
174 /* ====================================================================== */
175 /* === PropertyUser implementation ====================================== */
176 /* ====================================================================== */
177
178 template <typename PropertyType>
180 PropertyUser::getProperty(const std::string& name)
181 {
183 return Property<PropertyType>(getPropertyDefinitions()->getDefintion<PropertyType>(name),
184 getPropertyDefinitions()->getPrefix(),
185 properties);
186 }
187
188 template <class T>
189 inline void
190 PropertyUser::getProperty(T& val, const std::string& name) const
191 {
194 if constexpr (plugin::value)
195 {
196 plugin::Get(*this, val, name);
197 }
198 else
199 {
200 val = getProperty<T>(name).getValue();
201 }
202 }
203
204 template <class T>
205 inline void
206 PropertyUser::getProperty(std::atomic<T>& val, const std::string& name) const
207 {
209 val = getProperty<T>(name).getValue();
210 }
211
212 template <class ContainerT>
213 inline void
215 const std::string& name,
216 const std::string& splitBy,
217 bool trimElements,
218 bool removeEmptyElements)
219 {
221 using ValueT = typename ContainerT::value_type;
222 const std::string csv = getProperty<std::string>(name);
223 if constexpr (std::is_same_v<ContainerT, std::vector<std::string>>)
224 {
225 val = Split(csv, splitBy, trimElements, removeEmptyElements);
226 }
227 else
228 {
229 const auto strvec = Split(csv, splitBy, trimElements, removeEmptyElements);
230 val.clear();
231 for (const auto& str : strvec)
232 {
233 if constexpr (std::is_same_v<ValueT, std::string>)
234 {
235 armarx::emplace(val, str);
236 }
237 else
238 {
240 }
241 }
242 }
243 }
244
245 template <class T>
246 inline std::vector<T>
247 PropertyUser::getPropertyAsCSV(const std::string& name,
248 const std::string& splitBy,
249 bool trimElements,
250 bool removeEmptyElements)
251 {
253 std::vector<T> val;
254 getPropertyAsCSV(val, name, splitBy, trimElements, removeEmptyElements);
255 return val;
256 }
257
258 /**
259 * PropertyUser smart pointer type
260 */
262
263 /**
264 * UserProperty list type
265 */
266 using PropertyUserList = std::vector<PropertyUserPtr>;
267} // namespace armarx
std::string str(const T &t)
Abstract PropertyUser class.
Ice::PropertiesPtr getIceProperties() const
Returns the set of Ice properties.
bool tryAddProperty(const std::string &propertyName, const std::string &value)
PropertyDefinitionsPtr getPropertyDefinitions()
Returns the component's property definition container.
bool hasProperty(const std::string &name)
std::vector< std::string > getComponentProxyNames()
void getPropertyAsCSV(ContainerT &val, const std::string &name, const std::string &splitBy=",;", bool trimElements=true, bool removeEmptyElements=true)
virtual void updateIceProperties(const std::map< std::string, std::string > &changes)
void updateProxies(IceManagerPtr)
virtual void setIceProperties(Ice::PropertiesPtr properties)
Sets the Ice properties.
std::vector< std::string > getSubscribedTopicNames()
virtual PropertyDefinitionsPtr createPropertyDefinitions()=0
Creates the property definition container.
virtual void injectPropertyDefinitions(PropertyDefinitionsPtr &)
Called after createPropertyDefinitions by Component to inject propertes of ComponentPlugin.
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
virtual void icePropertiesUpdated(const std::set< std::string > &changedProperties)
This method is called when new Properties are set via setIceProperties().
std::vector< std::string > getTopicProxyNames()
Property< PropertyType > getProperty(const std::string &name) const
Hack to allow using getProperty in const-modified methods.
Provides access to Ice properties with extended capabilities.
Definition Property.h:79
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
auto emplace(Cont &cont, Params &&... params) -> decltype(cont.emplace_back(std::forward< Params >(params)...))
Definition emplace.h:33
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
std::vector< PropertyUserPtr > PropertyUserList
UserProperty list type.
T PropertyDefinition_lexicalCastTo(std::string const &input)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceUtil::Handle< class PropertyUser > PropertyUserPtr
PropertyUser smart pointer type.
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition ArmarXFwd.h:39
#define ARMARX_TRACE
Definition trace.h:77