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
38
39namespace 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
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
uint8_t index
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()
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()
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
Ice::PropertiesPtr createProperties()
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition algorithm.h:330
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition ArmarXFwd.h:39
PropertyDefinitionsPtr propertyDefinitions
Component property definitions pointer.