PropertyUser.cpp
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 
26 #include "ArmarXCore/core/exceptions/Exception.h" // for LocalException
27 #include "ArmarXCore/core/util/StringHelpers.h" // for Contains
29 #include "PropertyUser.h"
30 #include "../../logging/Logging.h"
31 
32 #include <Ice/Initialize.h>
33 
34 #include <sstream>
35 #include <mutex>
36 
37 namespace armarx
38 {
40  {
41  /**
42  * Component property definitions pointer
43  */
45  std::mutex mutex;
46  };
47 
49  : impl(new Impl)
50  {
51 
52  }
53 
55  {
56  // pass
57  }
58 
59 
62  {
63  if (impl->propertyDefinitions.get() == nullptr)
64  {
65  impl->propertyDefinitions = createPropertyDefinitions();
66  injectPropertyDefinitions(impl->propertyDefinitions);
67  if (properties)
68  {
69  impl->propertyDefinitions->setProperties(properties);
70  }
71  if (armarx::Contains(impl->propertyDefinitions->getPrefix(), ".."))
72  {
73  throw LocalException("Property prefix contains '..'. Did you call 'getProperty' in a Component constructor?");
74  }
75  }
76 
77  return impl->propertyDefinitions;
78  }
79 
80 
81  void
83  {
84  // pass
85  }
86 
87 
88  void
90  {
91  if (properties)
92  {
93  std::set<std::string> changedPropertyNames;
94  for (auto& prop : properties->getPropertiesForPrefix(""))
95  {
96  auto propName = prop.first;
97  auto index = propName.find_last_of('.');
98  std::string name = propName.substr(index + 1);
99  changedPropertyNames.insert(name);
100  }
101  this->properties = properties->clone();
102  if (impl->propertyDefinitions)
103  {
104  impl->propertyDefinitions->setProperties(this->properties);
105  }
106  // if (signalUpdates)
107  {
108  icePropertiesUpdated(changedPropertyNames);
109  }
110  }
111  }
112 
113 
114  void
115  PropertyUser::updateIceProperties(const std::map<std::string, std::string>& changes)
116  {
117  std::unique_lock lock(impl->mutex);
119  std::set<std::string> changedPropertyNames;
120  for (auto& prop : changes)
121  {
122  std::string propName = prop.first;
123  try
124  {
125  auto index = propName.find_last_of('.');
126  std::string name = propName.substr(index + 1);
127  std::string prefix = propName.substr(0, index + 1);
128  if (prefix != definitions->getPrefix())
129  {
130  continue;
131  }
132  if (!definitions->getDefinitionBase(name)->isConstant())
133  {
134  this->properties->setProperty(propName, prop.second);
135  changedPropertyNames.insert(name);
136  ARMARX_VERBOSE << "Updating MUTABLE Property: " << prop.first << " with value '" << prop.second << "'";
137  }
138  else
139  {
140  ARMARX_WARNING << "Can not set CONST Property: " << prop.first;
141  }
142  }
143  catch (const armarx::LocalException& e)
144  {
145  ARMARX_IMPORTANT << "MISSING Property: " << e.what();
146  }
147  }
148 
149  icePropertiesUpdated(changedPropertyNames);
150  }
151 
152 
153  void
155  {
156  getPropertyDefinitions()->writeValues();
157  }
158 
159 
160  void
162  {
163  getPropertyDefinitions()->writeProxyValues(ice_manager);
164  }
165 
166 
167  std::vector<std::string>
169  {
170  std::vector<std::string> proxy_names;
171  for (auto& [prop_name, def] : getPropertyDefinitions()->getProxyDefinitions())
172  {
173  if (def->getProxyType() == ProxyType::component)
174  {
175  proxy_names.push_back(getProperty<std::string>(prop_name).getValue());
176  }
177  }
178  return proxy_names;
179  }
180 
181 
182  std::vector<std::string>
184  {
185  std::vector<std::string> proxy_names;
186  for (auto& [prop_name, def] : getPropertyDefinitions()->getProxyDefinitions())
187  {
188  if (def->getProxyType() == ProxyType::topic)
189  {
190  proxy_names.push_back(getProperty<std::string>(prop_name).getValue());
191  }
192  }
193  return proxy_names;
194  }
195 
196 
197  std::vector<std::string>
199  {
200  std::vector<std::string> topic_names;
201  for (const std::string& prop_name : getPropertyDefinitions()->getSubscribedTopicDefinitions())
202  {
203  topic_names.push_back(getProperty<std::string>(prop_name));
204  }
205  return topic_names;
206  }
207 
208 
209  bool PropertyUser::tryAddProperty(const std::string& propertyName, const std::string& value)
210  {
211  if (properties->getProperty(propertyName).empty())
212  {
213  properties->setProperty(propertyName, value);
214  return true;
215  }
216  return false;
217  }
218 
219 
220  void
221  PropertyUser::icePropertiesUpdated(const std::set<std::string>& changedProperties)
222  {
223  // intentionally empty, since subclasses are supposed to overwrite this method
224  (void) changedProperties;
225  }
226 
227 
230  {
231  if (properties)
232  {
233  return properties->clone();// cloning is necessary here
234  }
235  else
236  {
237  return Ice::createProperties();
238  }
239  }
240 
241 
242  bool
243  armarx::PropertyUser::hasProperty(const std::string& name)
244  {
245  return getPropertyDefinitions()->hasDefinition(name);
246  }
247 }
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
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::PropertyUser::Impl
Definition: PropertyUser.cpp:39
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::PropertyUser::updateProxies
void updateProxies(IceManagerPtr)
Definition: PropertyUser.cpp:161
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::ProxyType::component
@ component
armarx::PropertyUser::injectPropertyDefinitions
virtual void injectPropertyDefinitions(PropertyDefinitionsPtr &)
Called after createPropertyDefinitions by Component to inject propertes of ComponentPlugin.
Definition: PropertyUser.cpp:82
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:295
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
StringHelpers.h
IceInternal::Handle< ::Ice::Properties >
armarx::PropertyUser::updateProperties
void updateProperties()
Definition: PropertyUser.cpp:154
armarx::PropertyUser::getComponentProxyNames
std::vector< std::string > getComponentProxyNames()
Definition: PropertyUser.cpp:168
armarx::PropertyUser::Impl::propertyDefinitions
PropertyDefinitionsPtr propertyDefinitions
Component property definitions pointer.
Definition: PropertyUser.cpp:44
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::PropertyUser::getTopicProxyNames
std::vector< std::string > getTopicProxyNames()
Definition: PropertyUser.cpp:183
armarx::PropertyUser::~PropertyUser
~PropertyUser() override
Definition: PropertyUser.cpp:54
Ice::createProperties
Ice::PropertiesPtr createProperties()
PropertyUser.h
PropertyDefinitionConfigFormatter.h
armarx::PropertyUser::updateIceProperties
virtual void updateIceProperties(const std::map< std::string, std::string > &changes)
Definition: PropertyUser.cpp:115
armarx::ProxyType::topic
@ topic
armarx::PropertyUser::createPropertyDefinitions
virtual PropertyDefinitionsPtr createPropertyDefinitions()=0
Creates the property definition container.
PropertyDefinitionContainer.h
IceUtil::Handle< class PropertyDefinitionContainer >
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
armarx::PropertyUser::Impl::mutex
std::mutex mutex
Definition: PropertyUser.cpp:45
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyUser::setIceProperties
virtual void setIceProperties(Ice::PropertiesPtr properties)
Sets the Ice properties.
Definition: PropertyUser.cpp:89
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
Exception.h