config.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QLocale>
31 #include <utility>
32 
33 
34 #include <rviz/config.h>
35 
36 namespace rviz
37 {
39 // Config::Node internal data storage class
41 
43 {
44 public:
45  Node();
46  ~Node();
47 
48  void setType(Config::Type new_type);
49  void deleteData();
50 
51  typedef QMap<QString, NodePtr> ChildMap;
52  typedef QList<NodePtr> ChildList;
53 
55  union
56  {
59  QVariant* value;
60  } data_;
61 };
62 
63 // The default constructor should create an invalid node.
64 // This allows Property::save() to validate whether anything was actually saved.
65 Config::Node::Node() : type_(Invalid)
66 {
67  data_.map = nullptr;
68 }
69 
71 {
72  deleteData();
73 }
74 
76 {
77  switch (type_)
78  {
79  case Map:
80  delete data_.map;
81  break;
82  case List:
83  delete data_.list;
84  break;
85  case Value:
86  delete data_.value;
87  break;
88  default:
89  break;
90  }
91  data_.map = nullptr;
92 }
93 
95 {
96  if (type_ == new_type)
97  {
98  return;
99  }
100  deleteData();
101  type_ = new_type;
102  switch (type_)
103  {
104  case Map:
105  data_.map = new ChildMap;
106  break;
107  case List:
108  data_.list = new ChildList;
109  break;
110  case Value:
111  data_.value = new QVariant;
112  break;
113  default:
114  break;
115  }
116 }
117 
119 // Config wrapper class
121 
123 {
124 }
125 
126 Config::Config(const QVariant& value) : node_(new Config::Node())
127 {
128  setValue(value);
129 }
130 
131 Config::Config(NodePtr node) : node_(std::move(node))
132 {
133 }
134 
135 void Config::copy(const Config& source)
136 {
137  setType(source.getType());
138  switch (source.getType())
139  {
140  case Map:
141  {
142  MapIterator iter = source.mapIterator();
143  while (iter.isValid())
144  {
145  mapMakeChild(iter.currentKey()).copy(iter.currentChild());
146  iter.advance();
147  }
148  break;
149  }
150  case List:
151  {
152  int num_children = source.listLength();
153  for (int i = 0; i < num_children; i++)
154  {
155  listAppendNew().copy(source.listChildAt(i));
156  }
157  break;
158  }
159  case Value:
160  setValue(source.getValue());
161  break;
162  default:
163  break;
164  }
165 }
166 
168 {
169  return Config(NodePtr());
170 }
171 
173 {
174  return isValid() ? node_->type_ : Invalid;
175 }
176 
177 void Config::setType(Type new_type)
178 {
179  makeValid();
180  node_->setType(new_type);
181 }
182 
183 void Config::mapSetValue(const QString& key, const QVariant& value)
184 {
185  mapMakeChild(key).setValue(value);
186 }
187 
188 Config Config::mapMakeChild(const QString& key)
189 {
190  Config child;
191 
192  makeValid();
193  node_->setType(Map);
194  (*node_->data_.map)[key] = child.node_;
195 
196  return child;
197 }
198 
199 void Config::mapRemoveChild(const QString& key)
200 {
201  if (getType() != Map)
202  return;
203 
204  (*node_->data_.map).remove(key);
205 }
206 
207 Config Config::mapGetChild(const QString& key) const
208 {
209  if (node_.get() == nullptr || node_->type_ != Map)
210  {
211  return invalidConfig();
212  }
213  Node::ChildMap::const_iterator iter = node_->data_.map->find(key);
214  if (iter == node_->data_.map->end())
215  {
216  return invalidConfig();
217  }
218  else
219  {
220  return Config(iter.value());
221  }
222 }
223 
224 bool Config::mapGetValue(const QString& key, QVariant* value_out) const
225 {
226  Config child = mapGetChild(key);
227  if (child.getType() == Value) // getType() checks for validity as well.
228  {
229  *value_out = child.getValue();
230  return true;
231  }
232  return false;
233 }
234 
235 bool Config::mapGetInt(const QString& key, int* value_out) const
236 {
237  QVariant v;
238  if (mapGetValue(key, &v) && (v.type() == QVariant::Int || v.type() == QVariant::String))
239  {
240  bool ok;
241  int i = v.toInt(&ok);
242  if (ok)
243  {
244  *value_out = i;
245  return true;
246  }
247  }
248  return false;
249 }
250 
251 bool Config::mapGetFloat(const QString& key, float* value_out) const
252 {
253  QVariant v;
254  if (mapGetValue(key, &v) && (int(v.type()) == int(QMetaType::Float) || v.type() == QVariant::Double ||
255  v.type() == QVariant::String))
256  {
257  bool ok;
258  float f = v.toFloat(&ok);
259  if (ok)
260  {
261  *value_out = f;
262  return true;
263  }
264  QString as_string = v.toString();
265  // Try as European, e.g. 1.234,56 rather than 1,234.56
266  QLocale german(QLocale::German);
267  f = german.toFloat(as_string, &ok);
268  if (ok)
269  {
270  *value_out = f;
271  return true;
272  }
273  }
274  return false;
275 }
276 
277 bool Config::mapGetBool(const QString& key, bool* value_out) const
278 {
279  QVariant v;
280  if (mapGetValue(key, &v) && (v.type() == QVariant::Bool || v.type() == QVariant::String))
281  {
282  *value_out = v.toBool();
283  return true;
284  }
285  return false;
286 }
287 
288 bool Config::mapGetString(const QString& key, QString* value_out) const
289 {
290  QVariant v;
291  if (mapGetValue(key, &v) && v.type() == QVariant::String)
292  {
293  *value_out = v.toString();
294  return true;
295  }
296  return false;
297 }
298 
300 {
301  if (node_.get() == nullptr)
302  {
303  node_.reset(new Node());
304  }
305 }
306 
307 bool Config::isValid() const
308 {
309  return node_.get() != nullptr;
310 }
311 
312 void Config::setValue(const QVariant& value)
313 {
314  makeValid();
315  node_->setType(Value);
316  *node_->data_.value = value;
317 }
318 
319 QVariant Config::getValue() const
320 {
321  return (isValid() && node_->type_ == Value) ? *node_->data_.value : QVariant();
322 }
323 
325 {
326  return (isValid() && node_->type_ == List) ? node_->data_.list->size() : 0;
327 }
328 
330 {
331  if (isValid() && node_->type_ == List && i >= 0 && i < node_->data_.list->size())
332  {
333  return Config(node_->data_.list->at(i));
334  }
335  else
336  {
337  return invalidConfig();
338  }
339 }
340 
342 {
343  Config child;
344 
345  setType(List);
346  node_->data_.list->append(child.node_);
347 
348  return child;
349 }
350 
352 {
353  // Create a new (invalid) iterator.
354  Config::MapIterator iter;
355 
356  if (node_.get() == nullptr || node_->type_ != Map)
357  {
358  // Force the node to be invalid, since this node does not have a map.
359  iter.node_.reset();
360  }
361  else
362  {
363  // Copy this config's node reference into the iterator's node reference.
364  iter.node_ = node_;
365  iter.start();
366  }
367  return iter;
368 }
369 
370 Config::MapIterator::MapIterator() : iterator_valid_(false)
371 {
372 }
373 
375 {
376  if (node_.get() == nullptr || node_->type_ != Config::Map)
377  {
378  iterator_valid_ = false;
379  return;
380  }
381  if (!iterator_valid_)
382  {
383  iterator_ = node_->data_.map->begin();
384  iterator_valid_ = true;
385  }
386  else
387  {
388  iterator_++;
389  }
390 }
391 
393 {
394  if (node_.get() == nullptr || node_->type_ != Config::Map)
395  {
396  iterator_valid_ = false;
397  return false;
398  }
399  if (!iterator_valid_)
400  {
401  return false;
402  }
403  else
404  {
405  return iterator_ != node_->data_.map->end();
406  }
407 }
408 
410 {
411  if (node_.get() == nullptr || node_->type_ != Config::Map)
412  {
413  iterator_valid_ = false;
414  return;
415  }
416  iterator_ = node_->data_.map->begin();
417  iterator_valid_ = true;
418 }
419 
421 {
422  if (node_.get() == nullptr || node_->type_ != Config::Map || !iterator_valid_)
423  {
424  iterator_valid_ = false;
425  return QString();
426  }
427  return iterator_.key();
428 }
429 
431 {
432  if (node_.get() == nullptr || node_->type_ != Config::Map || !iterator_valid_)
433  {
434  iterator_valid_ = false;
435  return Config();
436  }
437  return Config(iterator_.value());
438 }
439 
440 } // end namespace rviz
rviz::Config::isValid
bool isValid() const
Returns true if the internal Node reference is valid, false if not. Same as (getType() !...
Definition: config.cpp:307
boost::shared_ptr< Node >
rviz::Config::MapIterator::isValid
bool isValid()
Return true if the iterator currently points to a valid entry, false if not.
Definition: config.cpp:392
rviz::Config::MapIterator::node_
Config::NodePtr node_
Definition: config.h:316
rviz::Config::Config
Config()
Default constructor. Creates an empty config object.
Definition: config.cpp:122
rviz::Config::Node::ChildList
QList< NodePtr > ChildList
Definition: config.cpp:52
rviz::Config::Node::setType
void setType(Config::Type new_type)
Definition: config.cpp:94
rviz::Config::MapIterator::start
void start()
Resets the iterator to the start of the map.
Definition: config.cpp:409
rviz::Config::listLength
int listLength() const
Returns the length of the List in this Node, or 0 if this Node does not have type List.
Definition: config.cpp:324
rviz::Config::MapIterator::MapIterator
MapIterator()
Private constructor enforces that MapIterators are only made by their friend the Config class.
Definition: config.cpp:370
rviz::Config::MapIterator::currentKey
QString currentKey()
Return the name of the current map entry.
Definition: config.cpp:420
ok
ROSCPP_DECL bool ok()
f
f
rviz::Config::Node
Definition: config.cpp:42
rviz::Config::Type
Type
Possible types a Config Node can have are Map, List, Value, and Empty. Invalid means the Config objec...
Definition: config.h:148
rviz::Config::Node::~Node
~Node()
Definition: config.cpp:70
rviz::Config::mapGetChild
Config mapGetChild(const QString &key) const
If the referenced Node is a Map and it has a child with the given key, return a reference to the chil...
Definition: config.cpp:207
rviz::Config::List
@ List
Definition: config.h:151
rviz::Config::makeValid
void makeValid()
If the node pointer is NULL, this sets it to a new empty node.
Definition: config.cpp:299
rviz
Definition: add_display_dialog.cpp:54
rviz::Config::mapMakeChild
Config mapMakeChild(const QString &key)
Create a child node stored with the given key, and return the child.
Definition: config.cpp:188
rviz::Config::Node::deleteData
void deleteData()
Definition: config.cpp:75
rviz::Config::Node::list
ChildList * list
Definition: config.cpp:58
rviz::Config::mapGetValue
bool mapGetValue(const QString &key, QVariant *value_out) const
Convenience function for looking up a named value.
Definition: config.cpp:224
rviz::Config::listAppendNew
Config listAppendNew()
Ensure the referenced Node is of type List, append a new Empty Node to the end of the list,...
Definition: config.cpp:341
rviz::Config::Node::map
ChildMap * map
Definition: config.cpp:57
rviz::Config::Value
@ Value
Definition: config.h:152
rviz::Config::mapGetBool
bool mapGetBool(const QString &key, bool *value_out) const
Convenience function for looking up a named boolean.
Definition: config.cpp:277
rviz::Config::Node::value
QVariant * value
Definition: config.cpp:59
rviz::Config::Node::Node
Node()
Definition: config.cpp:65
rviz::Config::copy
void copy(const Config &source)
Make this a deep copy of source.
Definition: config.cpp:135
rviz::Config::node_
NodePtr node_
Definition: config.h:338
rviz::Config::getType
Type getType() const
Return the Type of the referenced Node, or Invalid if this Config does not refer to a Node at all.
Definition: config.cpp:172
rviz::Config::listChildAt
Config listChildAt(int i) const
Return the i'th child in the list, if the referenced Node has type List. Returns an Invalid Config if...
Definition: config.cpp:329
rviz::Config::MapIterator::currentChild
Config currentChild()
Return a Config reference to the current map entry.
Definition: config.cpp:430
rviz::Config::setType
void setType(Type new_type)
Set the type of this Config Node.
Definition: config.cpp:177
std
rviz::Config::Invalid
@ Invalid
Definition: config.h:154
rviz::Config::mapGetString
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:288
rviz::Config::NodePtr
boost::shared_ptr< Node > NodePtr
Definition: config.h:127
rviz::Config::Map
@ Map
Definition: config.h:150
config.h
rviz::Config::mapGetInt
bool mapGetInt(const QString &key, int *value_out) const
Convenience function for looking up a named integer.
Definition: config.cpp:235
rviz::Config::Node::data_
union rviz::Config::Node::@0 data_
rviz::Config::mapRemoveChild
void mapRemoveChild(const QString &key)
Remove child node with given key from the Map.
Definition: config.cpp:199
rviz::Config::MapIterator
Iterator class for looping over all entries in a Map type Config Node.
Definition: config.h:291
rviz::Config::mapGetFloat
bool mapGetFloat(const QString &key, float *value_out) const
Convenience function for looking up a named float.
Definition: config.cpp:251
rviz::Config::mapIterator
MapIterator mapIterator() const
Return a new iterator for looping over key/value pairs.
Definition: config.cpp:351
rviz::Config::MapIterator::advance
void advance()
Advance iterator to next entry.
Definition: config.cpp:374
rviz::Config::setValue
void setValue(const QVariant &value)
Ensures this is a valid Config object, sets the type to Value then sets the value.
Definition: config.cpp:312
rviz::Config
Configuration data storage class.
Definition: config.h:124
rviz::Config::mapSetValue
void mapSetValue(const QString &key, const QVariant &value)
Set a named child to the given value.
Definition: config.cpp:183
rviz::Config::invalidConfig
static Config invalidConfig()
Definition: config.cpp:167
rviz::Config::getValue
QVariant getValue() const
If this config object is valid and is a Value type, this returns its value. Otherwise it returns an i...
Definition: config.cpp:319
rviz::Config::Node::ChildMap
QMap< QString, NodePtr > ChildMap
Definition: config.cpp:51
rviz::Config::Node::type_
Config::Type type_
Definition: config.cpp:54


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri May 10 2024 02:32:22