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 
32 #include "rviz/config.h"
33 
34 namespace rviz
35 {
36 
38 // Config::Node internal data storage class
40 
42 {
43 public:
44  Node();
45  ~Node();
46 
47  void setType( Config::Type new_type );
48  void deleteData();
49 
50  typedef QMap<QString, NodePtr> ChildMap;
51  typedef QList<NodePtr> ChildList;
52 
54  union
55  {
56  ChildMap* map;
57  ChildList* list;
58  QVariant* value;
59  } data_;
60 };
61 
63  : type_( Empty )
64 {
65  data_.map = NULL;
66 }
67 
69 {
70  deleteData();
71 }
72 
74 {
75  switch( type_ )
76  {
77  case Map: delete data_.map; break;
78  case List: delete data_.list; break;
79  case Value: delete data_.value; break;
80  default:
81  break;
82  }
83  data_.map = NULL;
84 }
85 
87 {
88  if( type_ == new_type )
89  {
90  return;
91  }
92  deleteData();
93  type_ = new_type;
94  switch( type_ )
95  {
96  case Map: data_.map = new ChildMap; break;
97  case List: data_.list = new ChildList; break;
98  case Value: data_.value = new QVariant; break;
99  default: break;
100  }
101 }
102 
104 // Config wrapper class
106 
108  : node_( new Config::Node() )
109 {}
110 
111 Config::Config( const Config& source )
112  : node_( source.node_ )
113 {}
114 
115 Config::Config( QVariant value )
116  : node_( new Config::Node() )
117 {
118  setValue( value );
119 }
120 
122  : node_( node )
123 {}
124 
125 void Config::copy( const Config& source )
126 {
127  if( !source.isValid() )
128  {
129  node_ = NodePtr();
130  return;
131  }
132 
133  setType( source.getType() );
134  switch( source.getType() )
135  {
136  case Map:
137  {
138  MapIterator iter = source.mapIterator();
139  while( iter.isValid() )
140  {
141  mapMakeChild( iter.currentKey() ).copy( iter.currentChild() );
142  iter.advance();
143  }
144  break;
145  }
146  case List:
147  {
148  int num_children = source.listLength();
149  for( int i = 0; i < num_children; i++ )
150  {
151  listAppendNew().copy( source.listChildAt( i ));
152  }
153  }
154  case Value:
155  setValue( source.getValue() );
156  break;
157  default:
158  break;
159  }
160 }
161 
163 {
164  return Config( NodePtr() );
165 }
166 
168 {
169  return isValid() ? node_->type_ : Invalid;
170 }
171 
172 void Config::setType( Type new_type )
173 {
174  if( new_type == Invalid )
175  {
176  node_ = NodePtr();
177  }
178  else
179  {
180  makeValid();
181  node_->setType( new_type );
182  }
183 }
184 
185 void Config::mapSetValue( const QString& key, QVariant value )
186 {
187  mapMakeChild( key ).setValue( value );
188 }
189 
190 Config Config::mapMakeChild( const QString& key )
191 {
192  Config child;
193 
194  makeValid();
195  node_->setType( Map );
196  (*node_->data_.map)[ key ] = child.node_;
197 
198  return child;
199 }
200 
201 Config Config::mapGetChild( const QString& key ) const
202 {
203  if( node_.get() == NULL || node_->type_ != Map )
204  {
205  return invalidConfig();
206  }
207  Node::ChildMap::const_iterator iter = node_->data_.map->find( key );
208  if( iter == node_->data_.map->end() )
209  {
210  return invalidConfig();
211  }
212  else
213  {
214  return Config( iter.value() );
215  }
216 }
217 
218 bool Config::mapGetValue( const QString& key, QVariant *value_out ) const
219 {
220  Config child = mapGetChild( key );
221  if( child.getType() == Value ) // getType() checks for validity as well.
222  {
223  *value_out = child.getValue();
224  return true;
225  }
226  return false;
227 }
228 
229 bool Config::mapGetInt( const QString& key, int *value_out ) const
230 {
231  QVariant v;
232  if( mapGetValue( key, &v ) && (v.type() == QVariant::Int || v.type() == QVariant::String ))
233  {
234  bool ok;
235  int i = v.toInt( &ok );
236  if( ok )
237  {
238  *value_out = i;
239  return true;
240  }
241  }
242  return false;
243 }
244 
245 bool Config::mapGetFloat( const QString& key, float *value_out ) const
246 {
247  QVariant v;
248  if( mapGetValue( key, &v ) && (int(v.type()) == int(QMetaType::Float) || v.type() == QVariant::Double || v.type() == QVariant::String ))
249  {
250  bool ok;
251  float f = v.toFloat( &ok );
252  if( ok )
253  {
254  *value_out = f;
255  return true;
256  }
257  QString as_string = v.toString();
258  // Try as European, e.g. 1.234,56 rather than 1,234.56
259  QLocale german(QLocale::German);
260  f = german.toFloat(as_string, &ok);
261  if ( ok )
262  {
263  *value_out = f;
264  return true;
265  }
266  }
267  return false;
268 }
269 
270 bool Config::mapGetBool( const QString& key, bool *value_out ) const
271 {
272  QVariant v;
273  if( mapGetValue( key, &v ) && (v.type() == QVariant::Bool || v.type() == QVariant::String ))
274  {
275  *value_out = v.toBool();
276  return true;
277  }
278  return false;
279 }
280 
281 bool Config::mapGetString( const QString& key, QString *value_out ) const
282 {
283  QVariant v;
284  if( mapGetValue( key, &v ) && v.type() == QVariant::String )
285  {
286  *value_out = v.toString();
287  return true;
288  }
289  return false;
290 }
291 
293 {
294  if( node_.get() == NULL )
295  {
296  node_.reset( new Node() );
297  }
298 }
299 
300 bool Config::isValid() const
301 {
302  return node_.get() != NULL;
303 }
304 
305 void Config::setValue( const QVariant& value )
306 {
307  makeValid();
308  node_->setType( Value );
309  *node_->data_.value = value;
310 }
311 
312 QVariant Config::getValue() const
313 {
314  return ( isValid() && node_->type_ == Value ) ? *node_->data_.value : QVariant();
315 }
316 
318 {
319  return ( isValid() && node_->type_ == List ) ? node_->data_.list->size() : 0;
320 }
321 
323 {
324  if( isValid() && node_->type_ == List && i >= 0 && i < node_->data_.list->size() )
325  {
326  return Config( node_->data_.list->at( i ));
327  }
328  else
329  {
330  return invalidConfig();
331  }
332 }
333 
335 {
336  Config child;
337 
338  setType( List );
339  node_->data_.list->append( child.node_ );
340 
341  return child;
342 }
343 
345 {
346  // Create a new (invalid) iterator.
347  Config::MapIterator iter;
348 
349  if( node_.get() == NULL || node_->type_ != Map )
350  {
351  // Force the node to be invalid, since this node does not have a map.
352  iter.node_.reset();
353  }
354  else
355  {
356  // Copy this config's node reference into the iterator's node reference.
357  iter.node_ = node_;
358  iter.start();
359  }
360  return iter;
361 }
362 
364  : iterator_valid_( false )
365 {}
366 
368 {
369  if( node_.get() == NULL || node_->type_ != Config::Map )
370  {
371  iterator_valid_ = false;
372  return;
373  }
374  if( !iterator_valid_ )
375  {
376  iterator_ = node_->data_.map->begin();
377  iterator_valid_ = true;
378  }
379  else
380  {
381  iterator_++;
382  }
383 }
384 
386 {
387  if( node_.get() == NULL || node_->type_ != Config::Map )
388  {
389  iterator_valid_ = false;
390  return false;
391  }
392  if( !iterator_valid_ )
393  {
394  return false;
395  }
396  else
397  {
398  return iterator_ != node_->data_.map->end();
399  }
400 }
401 
403 {
404  if( node_.get() == NULL || node_->type_ != Config::Map )
405  {
406  iterator_valid_ = false;
407  return;
408  }
409  iterator_ = node_->data_.map->begin();
410  iterator_valid_ = true;
411 }
412 
414 {
415  if( node_.get() == NULL || node_->type_ != Config::Map || !iterator_valid_ )
416  {
417  iterator_valid_ = false;
418  return QString();
419  }
420  return iterator_.key();
421 }
422 
424 {
425  if( node_.get() == NULL || node_->type_ != Config::Map || !iterator_valid_ )
426  {
427  iterator_valid_ = false;
428  return Config();
429  }
430  return Config( iterator_.value() );
431 }
432 
433 } // end namespace rviz
#define NULL
Definition: global.h:37
Config::Type type_
Definition: config.cpp:53
void setValue(const QVariant &value)
Ensures this is a valid Config object, sets the type to Value then sets the value.
Definition: config.cpp:305
void setType(Config::Type new_type)
Definition: config.cpp:86
Config::NodePtr node_
Definition: config.h:307
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:167
static Config invalidConfig()
Definition: config.cpp:162
f
Config()
Default constructor. Creates an empty config object.
Definition: config.cpp:107
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:317
Config listChildAt(int i) const
Return the i&#39;th child in the list, if the referenced Node has type List. Returns an Invalid Config if...
Definition: config.cpp:322
ChildList * list
Definition: config.cpp:57
bool mapGetFloat(const QString &key, float *value_out) const
Convenience function for looking up a named float.
Definition: config.cpp:245
MapIterator()
Private constructor enforces that MapIterators are only made by their friend the Config class...
Definition: config.cpp:363
bool isValid()
Return true if the iterator currently points to a valid entry, false if not.
Definition: config.cpp:385
void makeValid()
If the node pointer is NULL, this sets it to a new empty node.
Definition: config.cpp:292
bool isValid() const
Returns true if the internal Node reference is valid, false if not. Same as (getType() != Invalid)...
Definition: config.cpp:300
boost::shared_ptr< Node > NodePtr
Definition: config.h:128
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:281
void start()
Resets the iterator to the start of the map.
Definition: config.cpp:402
void mapSetValue(const QString &key, QVariant value)
Set a named child to the given value.
Definition: config.cpp:185
Configuration data storage class.
Definition: config.h:125
QString currentKey()
Return the name of the current map entry.
Definition: config.cpp:413
Type
Possible types a Config Node can have are Map, List, Value, and Empty. Invalid means the Config objec...
Definition: config.h:149
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:312
Config mapMakeChild(const QString &key)
Create a child node stored with the given key, and return the child.
Definition: config.cpp:190
Iterator class for looping over all entries in a Map type Config Node.
Definition: config.h:282
bool mapGetBool(const QString &key, bool *value_out) const
Convenience function for looking up a named boolean.
Definition: config.cpp:270
NodePtr node_
Definition: config.h:329
bool mapGetInt(const QString &key, int *value_out) const
Convenience function for looking up a named integer.
Definition: config.cpp:229
MapIterator mapIterator() const
Return a new iterator for looping over key/value pairs.
Definition: config.cpp:344
ChildMap * map
Definition: config.cpp:56
Config listAppendNew()
Ensure the referenced Node is of type List, append a new Empty Node to the end of the list...
Definition: config.cpp:334
Config currentChild()
Return a Config reference to the current map entry.
Definition: config.cpp:423
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:201
QMap< QString, Config::NodePtr >::const_iterator iterator_
Definition: config.h:308
bool mapGetValue(const QString &key, QVariant *value_out) const
Convenience function for looking up a named value.
Definition: config.cpp:218
void deleteData()
Definition: config.cpp:73
QVariant * value
Definition: config.cpp:58
friend class Config
Definition: config.h:310
QList< NodePtr > ChildList
Definition: config.cpp:51
void copy(const Config &source)
Make this a deep copy of source.
Definition: config.cpp:125
void setType(Type new_type)
Set the type of this Config Node.
Definition: config.cpp:172
union rviz::Config::Node::@0 data_
QMap< QString, NodePtr > ChildMap
Definition: config.cpp:50
void advance()
Advance iterator to next entry.
Definition: config.cpp:367


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:50