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