PropertyDefinitionContainer.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 Mirko Waechter (waechter at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25
27
28
29// STD/STL
30#include <map>
31#include <string>
32
33#include <SimoxUtility/algorithm/string/string_tools.h>
34
35// Ice
36#include <Ice/Properties.h>
37
38// ArmarXCore
48
49namespace armarx
50{
51
52
54 {
55 setDescription(prefix + " properties");
56
57 this->prefix = prefix + ".";
58 }
59
61 {
62 // Delete property definitions.
63 for (auto it = definitions.begin(); it != definitions.end(); ++it)
64 {
65 delete it->second;
66 }
67
68 // Delete proxy definitions,
69 for (auto it = proxies.begin(); it != proxies.end(); ++it)
70 {
71 delete it->second;
72 }
73 }
74
75 void
77 {
78 if (properties)
79 {
80 for (auto& [name, definition] : definitions)
81 {
82 definition->writeValueToSetter(getPrefix(), properties);
83 }
84 }
85 }
86
87 void
89 {
90 if (properties)
91 {
92 for (auto& [name, definition] : proxies)
93 {
94 ARMARX_CHECK(definitions.find(name) != definitions.end())
95 << "`" << name << "` must be in definitions and proxies";
96
97 // Get proxy name, fetch proxy and write to passed setter.
98 const std::string proxy_name =
100 definition->write_proxy_to_setter(ice_manager, proxy_name);
101 }
102 }
103 }
104
105 std::map<std::string, ProxyPropertyDefinitionBase*>
110
111 std::vector<std::string>
116
122
123 void
128
131 {
132 // check definition existance
133 if (definitions.find(name) == definitions.end())
134 {
135 throw armarx::LocalException(name + " property is not defined.");
136 }
137
139 return def;
140 }
141
142 std::string
144 {
145 formatter.setPrefix(prefix);
146 std::string definitionStr;
147
148 DefinitionContainer::iterator it = definitions.begin();
149 Ice::PropertyDict propertiesMap;
150
151 if (properties)
152 {
153 propertiesMap = properties->getPropertiesForPrefix(getPrefix());
154 }
155 while (it != definitions.end())
156 {
157 definitionStr += it->second->toString(formatter, getValue(it->first));
158 propertiesMap.erase(it->first);
159 it++;
160 }
161
162 if (properties)
163 {
164 formatter.setPrefix("");
165 }
166
167 Ice::StringSeq values;
168 for (Ice::PropertyDict::iterator property = propertiesMap.begin();
169 property != propertiesMap.end();
170 property++)
171 {
172 definitionStr += formatter.formatDefinition(
173 property->first, "", "", "", "", "", "", "", values, property->second);
174 }
175
176 return definitionStr;
177 }
178
179 std::string
184
185 void
187 {
188 this->description = description;
189 }
190
191 void
193 {
194 this->prefix = prefix;
195 }
196
197 std::string
202
203 bool
205 {
206 Ice::PropertyDict selectedProperties = properties->getPropertiesForPrefix(getPrefix());
207 return (selectedProperties.count(name) > 0);
208 }
209
210 std::string
212 {
213 if (properties)
214 {
215 return properties->getPropertiesForPrefix(getPrefix())[name];
216 }
217 else
218 {
219 return "";
220 }
221 }
222
223 std::map<std::string, std::string>
225 {
226 std::map<std::string, std::string> result;
227 for (const auto& elem : definitions)
228 {
229 std::string value;
230 if (properties)
231 {
232 value = properties->getProperty(this->prefix + elem.first);
233 }
234 else
235 {
236 ARMARX_INFO << " properties NULL";
237 }
238 if (value.empty())
239 {
240 PropertyDefinitionBase* def = elem.second;
241 value = def->getDefaultAsString();
242 // PropertyDefinition<std::string>* def = dynamic_cast<PropertyDefinition<std::string>*>(elem.second);
243 // result[elem.first] = Property<std::string>(
244 // *def,
245 // prefix,
246 // properties).getValue();
247 }
248
249 result[prefix + elem.first] = value;
250 }
251 return result;
252 }
253
254 std::string
256 long size,
257 std::string delim)
258 {
259 std::stringstream ss;
260 ss << description << "\n";
261 ss << "(Format: Sequence of ";
262 if (size < 0)
263 {
264 ss << "n";
265 }
266 else
267 {
268 ss << size;
269 }
270 ss << " numbers delimited by '" << delim << "'.)";
271
272 return ss.str();
273 }
274
275 std::string
276 PropertyDefinitionContainer_ice_class_name(std::string const& full_type_name)
277 {
278 std::vector<std::string> splt = simox::alg::split(full_type_name, ":");
279 std::string class_name = splt.at(splt.size() - 1);
280
281 if (class_name == "ComponentInterface" and splt.size() >= 2)
282 {
283 /*
284 * The specific interface of a modern class with name like:
285 * package::components::name::ComponentInterface.
286 * The component's name is the last namespace, i.e. the item before the class name.
287 */
288 class_name = splt.at(splt.size() - 2);
289 }
290 else
291 {
292 // Remove common suffix patterns.
293 std::vector<std::string> trims = {"InterfaceListener",
294 "_interface_listener",
295 "Interface",
296 "_interface",
297 "Listener",
298 "_listener"};
299 for (const std::string& suffix : trims)
300 {
301 if (ends_with(class_name, suffix))
302 {
303 class_name.erase(class_name.length() - suffix.length());
304 break;
305 }
306 }
307 }
308
309 return class_name;
310 }
311
312
313} // namespace armarx
Common interface of any property definition.
virtual std::string getDefaultAsString()=0
std::string description
Property User description.
void setDescription(const std::string &description)
Sets the detailed description of the property user.
PropertyDefinition< PropertyType > & getDefintion(const std::string &name)
static std::string eigenVectorPropertyDescription(const std::string &description, long size, std::string delim)
std::map< std::string, ProxyPropertyDefinitionBase * > proxies
std::vector< std::string > getSubscribedTopicDefinitions()
DefinitionContainer definitions
Property definitions container.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
void setProperties(Ice::PropertiesPtr properties)
std::map< std::string, std::string > getPropertyValues(const std::string &prefix="") const
PropertyDefinitionBase * getDefinitionBase(const std::string &name)
std::string getValue(const std::string &name)
std::string toString(PropertyDefinitionFormatter &formatter)
std::map< std::string, ProxyPropertyDefinitionBase * > getProxyDefinitions()
std::string getDescription() const
Returns the detailed description of the property user.
PropertyDefinitionFormatter is the base class for all formatters of PropertyDefinitions.
virtual void setPrefix(std::string prefix)
virtual std::string formatDefinition(std::string name, std::string description, std::string min, std::string max, std::string default_, std::string casesensitivity, std::string requirement, std::string reged, std::vector< std::string > values, std::string value)=0
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
bool ends_with(const std::string &haystack, const std::string &needle)
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition ArmarXFwd.h:39
std::string PropertyDefinitionContainer_ice_class_name(std::string const &full_type_name)