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