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 
40 namespace armarx
41 {
42  class PropertyUser;
44  class PropertyDefinitionContainer;
45 
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:
66  PropertyUser();
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>
179  Property<PropertyType>
180  PropertyUser::getProperty(const std::string& name)
181  {
182  ARMARX_TRACE;
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  {
192  ARMARX_TRACE;
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  {
208  ARMARX_TRACE;
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  {
220  ARMARX_TRACE;
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  {
239  armarx::emplace(val, PropertyDefinition_lexicalCastTo<ValueT>(str));
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  {
252  ARMARX_TRACE;
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
armarx::PropertyUser::icePropertiesUpdated
virtual void icePropertiesUpdated(const std::set< std::string > &changedProperties)
This method is called when new Properties are set via setIceProperties().
Definition: PropertyUser.cpp:214
armarx::PropertyUser::PropertyUser
PropertyUser()
Definition: PropertyUser.cpp:50
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::PropertyUser::updateProxies
void updateProxies(IceManagerPtr)
Definition: PropertyUser.cpp:157
armarx::PropertyUser::injectPropertyDefinitions
virtual void injectPropertyDefinitions(PropertyDefinitionsPtr &)
Called after createPropertyDefinitions by Component to inject propertes of ComponentPlugin.
Definition: PropertyUser.cpp:81
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
armarx::PropertyUser::hasProperty
bool hasProperty(const std::string &name)
Definition: PropertyUser.cpp:234
armarx::PropertyUser::tryAddProperty
bool tryAddProperty(const std::string &propertyName, const std::string &value)
Definition: PropertyUser.cpp:203
armarx::PropertyUserPtr
IceUtil::Handle< class PropertyUser > PropertyUserPtr
PropertyUser smart pointer type.
Definition: forward_declarations.h:36
IceInternal::Handle<::Ice::Properties >
armarx::PropertyUser::updateProperties
void updateProperties()
Definition: PropertyUser.cpp:151
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name) const
Hack to allow using getProperty in const-modified methods.
Definition: PropertyUser.h:85
armarx::PropertyUser::getComponentProxyNames
std::vector< std::string > getComponentProxyNames()
Definition: PropertyUser.cpp:163
armarx::PropertyUser::getPropertyAsCSV
void getPropertyAsCSV(ContainerT &val, const std::string &name, const std::string &splitBy=",;", bool trimElements=true, bool removeEmptyElements=true)
Definition: PropertyUser.h:214
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::PropertyUserList
std::vector< PropertyUserPtr > PropertyUserList
UserProperty list type.
Definition: PropertyUser.h:266
emplace.h
armarx::PropertyUser::getTopicProxyNames
std::vector< std::string > getTopicProxyNames()
Definition: PropertyUser.cpp:177
Property.h
armarx::PropertyUser::~PropertyUser
~PropertyUser() override
Definition: PropertyUser.cpp:54
Get
Eigen::Vector3f Get(const Eigen::Vector3f &p1, const Eigen::Vector3f &p2, float t)
Definition: MoveTCPToTarget.cpp:55
armarx::PropertyUser::updateIceProperties
virtual void updateIceProperties(const std::map< std::string, std::string > &changes)
Definition: PropertyUser.cpp:112
armarx::Property
Provides access to Ice properties with extended capabilities.
Definition: Property.h:78
IceManager.h
armarx::PropertyUser::createPropertyDefinitions
virtual PropertyDefinitionsPtr createPropertyDefinitions()=0
Creates the property definition container.
armarx::emplace
auto emplace(Cont &cont, Params &&... params) -> decltype(cont.emplace_back(std::forward< Params >(params)...))
Definition: emplace.h:33
PropertyDefinitionContainer.h
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:180
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::PropertyUser::getPropertyDefinitions
PropertyDefinitionsPtr getPropertyDefinitions()
Returns the component's property definition container.
Definition: PropertyUser.cpp:60
armarx::PropertyUser::getIceProperties
Ice::PropertiesPtr getIceProperties() const
Returns the set of Ice properties.
Definition: PropertyUser.cpp:221
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::PropertyUser
Abstract PropertyUser class.
Definition: PropertyUser.h:63
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::PropertyUser::setIceProperties
virtual void setIceProperties(Ice::PropertiesPtr properties)
Sets the Ice properties.
Definition: PropertyUser.cpp:87
armarx::meta::properties::GetPropertyPlugin
Definition: PropertyDefinition.h:54
armarx::PropertyUser::getSubscribedTopicNames
std::vector< std::string > getSubscribedTopicNames()
Definition: PropertyUser.cpp:191
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27