Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
rviz::Config Class Reference

Configuration data storage class. More...

#include <config.h>

Classes

class  MapIterator
 Iterator class for looping over all entries in a Map type Config Node. More...
 
class  Node
 

Public Types

enum  Type {
  Map, List, Value, Empty,
  Invalid
}
 Possible types a Config Node can have are Map, List, Value, and Empty. Invalid means the Config object does not point to a Node at all. More...
 

Public Member Functions

 Config ()
 Default constructor. Creates an empty config object. More...
 
 Config (const Config &source)=default
 Copy constructor. Copies only the reference to the data, not the data itself. More...
 
 Config (const QVariant &value)
 Convenience constructor, makes a Value type Config object with the given value. More...
 
void copy (const Config &source)
 Make this a deep copy of source. More...
 
Type getType () const
 Return the Type of the referenced Node, or Invalid if this Config does not refer to a Node at all. More...
 
QVariant getValue () const
 If this config object is valid and is a Value type, this returns its value. Otherwise it returns an invalid QVariant. More...
 
bool isValid () const
 Returns true if the internal Node reference is valid, false if not. Same as (getType() != Invalid). More...
 
Config listAppendNew ()
 Ensure the referenced Node is of type List, append a new Empty Node to the end of the list, and return a reference to the new Node. More...
 
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 the type is not List or if i is not a valid index into it. More...
 
int listLength () const
 Returns the length of the List in this Node, or 0 if this Node does not have type List. More...
 
bool mapGetBool (const QString &key, bool *value_out) const
 Convenience function for looking up a named boolean. More...
 
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 child. If the reference is invalid or the Node has a different Type, return an invalid Config. More...
 
bool mapGetFloat (const QString &key, float *value_out) const
 Convenience function for looking up a named float. More...
 
bool mapGetInt (const QString &key, int *value_out) const
 Convenience function for looking up a named integer. More...
 
bool mapGetString (const QString &key, QString *value_out) const
 Convenience function for looking up a named string. More...
 
bool mapGetValue (const QString &key, QVariant *value_out) const
 Convenience function for looking up a named value. More...
 
MapIterator mapIterator () const
 Return a new iterator for looping over key/value pairs. More...
 
Config mapMakeChild (const QString &key)
 Create a child node stored with the given key, and return the child. More...
 
void mapRemoveChild (const QString &key)
 Remove child node with given key from the Map. More...
 
void mapSetValue (const QString &key, const QVariant &value)
 Set a named child to the given value. More...
 
void setType (Type new_type)
 Set the type of this Config Node. More...
 
void setValue (const QVariant &value)
 Ensures this is a valid Config object, sets the type to Value then sets the value. More...
 

Private Types

typedef boost::shared_ptr< NodeNodePtr
 

Private Member Functions

 Config (NodePtr node)
 
void makeValid ()
 If the node pointer is NULL, this sets it to a new empty node. More...
 

Static Private Member Functions

static Config invalidConfig ()
 

Private Attributes

NodePtr node_
 

Friends

class MapIterator
 

Detailed Description

Configuration data storage class.

The purpose of the Config class is to provide a flexible place to store configuration data during saving and loading which is independent of the particular storage format (like YAML or XML or INI). The data is stored in a tree structure, supporting both named and numerically-indexed children. Leaves in the tree store QVariants, with convenience functions for int, float, QString, and bool types.

Config instances are references to an internal "Node" class which actually stores the data and the tree structure. Nodes are reference-counted and deletion is handled automatically. This makes it safe to hold a reference to a portion of a Config tree to use later, because the internal Nodes beneath the saved reference will not be destroyed when the root of the tree goes out of scope.

Config objects can be used on their own for generic hierarchical data storage, but they are intended to be used with reader and writer classes. Currently there is just YAML support, as that is all that RViz supports right now. Those classes are YamlConfigReader and YamlConfigWriter.

Typical use for reading looks like this:

YamlConfigReader reader; Config config; reader.readFile( config, "my_file.yaml" ); if( !reader.error() ) { int height, width; if( config.mapGetString( "Height", &height ) && config.mapGetString( "Width", &width )) { resize( width, height ); }

Config file_list_config = config.mapGetChild( "Files" ); filenames_.clear(); int num_files = file_list_config.listLength(); for( int i = 0; i < num_files; i++ ) { filenames_.push_back( file_list_config.listChildAt( i ).getValue().toString() ); } } else { printf( "%s", qPrintable( reader.errorMessage() )); }

For writing, the same program might use this:

Config config; config.mapSetValue( "Height", height() ); config.mapSetValue( "Width", width() ); Config file_list_config = config.mapMakeChild( "Files" ); for( int i = 0; i < filenames_.size(); i++ ) { file_list_config.listAppendNew().setValue( filenames_[ i ]); }

YamlConfigWriter writer; writer.writeFile( config, "my_file.yaml" );

if( writer.error() ) { printf( "%s", qPrintable( writer.errorMessage() )); }

setType() can be used to set the type of a given node (Map, List, Value, or Empty), but it is often unnecessary. All functions which add or change data (mapSetValue(), mapMakeChild(), setValue(), and listAppendNew()) internally call setType() to ensure the node has the right type for the operation. If setType() is called with the same type that the node already has, nothing happens. If it needs to change the type of the node, any data stored in the node is destroyed (except for child nodes which are referenced by other existing Config objects).

Definition at line 124 of file config.h.

Member Typedef Documentation

◆ NodePtr

Definition at line 127 of file config.h.

Member Enumeration Documentation

◆ Type

Possible types a Config Node can have are Map, List, Value, and Empty. Invalid means the Config object does not point to a Node at all.

Invalid Config objects are returned by data access functions when the data does not exist, like listChildAt(7) on a list of length 3, or mapGetChild("foo") on a Value Node.

Enumerator
Map 
List 
Value 
Empty 
Invalid 

Definition at line 148 of file config.h.

Constructor & Destructor Documentation

◆ Config() [1/4]

rviz::Config::Config ( )

Default constructor. Creates an empty config object.

Definition at line 122 of file config.cpp.

◆ Config() [2/4]

rviz::Config::Config ( const Config source)
default

Copy constructor. Copies only the reference to the data, not the data itself.

◆ Config() [3/4]

rviz::Config::Config ( const QVariant &  value)

Convenience constructor, makes a Value type Config object with the given value.

Definition at line 126 of file config.cpp.

◆ Config() [4/4]

rviz::Config::Config ( NodePtr  node)
private

Definition at line 131 of file config.cpp.

Member Function Documentation

◆ copy()

void rviz::Config::copy ( const Config source)

Make this a deep copy of source.

Definition at line 135 of file config.cpp.

◆ getType()

Config::Type rviz::Config::getType ( ) const

Return the Type of the referenced Node, or Invalid if this Config does not refer to a Node at all.

Definition at line 172 of file config.cpp.

◆ getValue()

QVariant rviz::Config::getValue ( ) const

If this config object is valid and is a Value type, this returns its value. Otherwise it returns an invalid QVariant.

Definition at line 319 of file config.cpp.

◆ invalidConfig()

Config rviz::Config::invalidConfig ( )
staticprivate

Definition at line 167 of file config.cpp.

◆ isValid()

bool rviz::Config::isValid ( ) const

Returns true if the internal Node reference is valid, false if not. Same as (getType() != Invalid).

Definition at line 307 of file config.cpp.

◆ listAppendNew()

Config rviz::Config::listAppendNew ( )

Ensure the referenced Node is of type List, append a new Empty Node to the end of the list, and return a reference to the new Node.

Definition at line 341 of file config.cpp.

◆ listChildAt()

Config rviz::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 the type is not List or if i is not a valid index into it.

Definition at line 329 of file config.cpp.

◆ listLength()

int rviz::Config::listLength ( ) const

Returns the length of the List in this Node, or 0 if this Node does not have type List.

Definition at line 324 of file config.cpp.

◆ makeValid()

void rviz::Config::makeValid ( )
private

If the node pointer is NULL, this sets it to a new empty node.

Definition at line 299 of file config.cpp.

◆ mapGetBool()

bool rviz::Config::mapGetBool ( const QString &  key,
bool *  value_out 
) const

Convenience function for looking up a named boolean.

If a Value Node with the given @key is a child of this Node, and the Value is either a bool or a string-ified bool, set value_out to the bool and return true.

If the Config is invalid or the Node is not a Map, returns an Invalid Config.

Definition at line 277 of file config.cpp.

◆ mapGetChild()

Config rviz::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 child. If the reference is invalid or the Node has a different Type, return an invalid Config.

Definition at line 207 of file config.cpp.

◆ mapGetFloat()

bool rviz::Config::mapGetFloat ( const QString &  key,
float *  value_out 
) const

Convenience function for looking up a named float.

If a Value Node with the given @key is a child of this Node, and the Value is either a float, a double or a string-ified float or double, set value_out to the float and return true.

If the Config is invalid or the Node is not a Map, returns an Invalid Config.

Definition at line 251 of file config.cpp.

◆ mapGetInt()

bool rviz::Config::mapGetInt ( const QString &  key,
int *  value_out 
) const

Convenience function for looking up a named integer.

If a Value Node with the given @key is a child of this Node, and the Value is either an int or a string-ified int, set value_out to the integer and return true.

If the Config is invalid or the Node is not a Map, returns an Invalid Config.

Definition at line 235 of file config.cpp.

◆ mapGetString()

bool rviz::Config::mapGetString ( const QString &  key,
QString *  value_out 
) const

Convenience function for looking up a named string.

If a Value Node with the given @key is a child of this Node, and the Value is a string, set value_out to the string and return true.

If the Config is invalid or the Node is not a Map, returns an Invalid Config.

Definition at line 288 of file config.cpp.

◆ mapGetValue()

bool rviz::Config::mapGetValue ( const QString &  key,
QVariant *  value_out 
) const

Convenience function for looking up a named value.

If a Value Node with the given @key is a child of this Node, set value_out to the given value and return true.

If the Config is invalid or the Node is not a Map, returns an Invalid Config.

Definition at line 224 of file config.cpp.

◆ mapIterator()

Config::MapIterator rviz::Config::mapIterator ( ) const

Return a new iterator for looping over key/value pairs.

The returned MapIterator is initialized to point at the start of the map.

If this Config is Invalid or if its Node is not a Map, this returns a MapIterator for which isValid() always returns false.

Definition at line 351 of file config.cpp.

◆ mapMakeChild()

Config rviz::Config::mapMakeChild ( const QString &  key)

Create a child node stored with the given key, and return the child.

This forces the referenced Node to have type Map.

Definition at line 188 of file config.cpp.

◆ mapRemoveChild()

void rviz::Config::mapRemoveChild ( const QString &  key)

Remove child node with given key from the Map.

Definition at line 199 of file config.cpp.

◆ mapSetValue()

void rviz::Config::mapSetValue ( const QString &  key,
const QVariant &  value 
)

Set a named child to the given value.

Since QVariant has constructors for int, float, bool, QString, and other supported types, you can call mapSetValue() directly with your data in most cases:

config.mapSetValue( "Size", 13 ); config.mapSetValue( "Name", "Humphrey" );

mapSetValue( key, value ) is the same as mapMakeChild( key ).setValue( value ).

This forces the referenced Node to have type Map.

Definition at line 183 of file config.cpp.

◆ setType()

void rviz::Config::setType ( Type  new_type)

Set the type of this Config Node.

If new_type is Invalid, this de-references the node and makes the Config object invalid. If the new type is different from the old type, this deletes the existing data in the Node and changes the Node's type to @new_type. If this does not change the type of the Node, no data is deleted and nothing is changed.

If this Config is currently invalid and new_type is not Invalid, this will create a new Node and reference it.

Definition at line 177 of file config.cpp.

◆ setValue()

void rviz::Config::setValue ( const QVariant &  value)

Ensures this is a valid Config object, sets the type to Value then sets the value.

Definition at line 312 of file config.cpp.

Friends And Related Function Documentation

◆ MapIterator

friend class MapIterator
friend

Definition at line 340 of file config.h.

Member Data Documentation

◆ node_

NodePtr rviz::Config::node_
private

Definition at line 338 of file config.h.


The documentation for this class was generated from the following files:


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10