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. | |
Config (const Config &source) | |
Copy constructor. Copies only the reference to the data, not the data itself. | |
Config (QVariant value) | |
Convenience constructor, makes a Value type Config object with the given value. | |
void | copy (const Config &source) |
Make this a deep copy of source. | |
Type | getType () const |
Return the Type of the referenced Node, or Invalid if this Config does not refer to a Node at all. | |
QVariant | getValue () const |
If this config object is valid and is a Value type, this returns its value. Otherwise it returns an invalid QVariant. | |
bool | isValid () const |
Returns true if the internal Node reference is valid, false if not. Same as (getType() != Invalid). | |
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. | |
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. | |
int | listLength () const |
Returns the length of the List in this Node, or 0 if this Node does not have type List. | |
bool | mapGetBool (const QString &key, bool *value_out) const |
Convenience function for looking up a named boolean. | |
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. | |
bool | mapGetFloat (const QString &key, float *value_out) const |
Convenience function for looking up a named float. | |
bool | mapGetInt (const QString &key, int *value_out) const |
Convenience function for looking up a named integer. | |
bool | mapGetString (const QString &key, QString *value_out) const |
Convenience function for looking up a named string. | |
bool | mapGetValue (const QString &key, QVariant *value_out) const |
Convenience function for looking up a named value. | |
MapIterator | mapIterator () const |
Return a new iterator for looping over key/value pairs. | |
Config | mapMakeChild (const QString &key) |
Create a child node stored with the given key, and return the child. | |
void | mapSetValue (const QString &key, QVariant value) |
Set a named child to the given value. | |
void | setType (Type new_type) |
Set the type of this Config Node. | |
void | setValue (const QVariant &value) |
Ensures this is a valid Config object, sets the type to Value then sets the value. | |
Private Types | |
typedef boost::shared_ptr< Node > | NodePtr |
Private Member Functions | |
Config (NodePtr node) | |
void | makeValid () |
If the node pointer is NULL, this sets it to a new empty node. | |
Static Private Member Functions | |
static Config | invalidConfig () |
Private Attributes | |
NodePtr | node_ |
Friends | |
class | MapIterator |
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).
typedef boost::shared_ptr<Node> rviz::Config::NodePtr [private] |
enum rviz::Config::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.
Default constructor. Creates an empty config object.
Definition at line 107 of file config.cpp.
rviz::Config::Config | ( | const Config & | source | ) |
Copy constructor. Copies only the reference to the data, not the data itself.
Definition at line 111 of file config.cpp.
rviz::Config::Config | ( | QVariant | value | ) |
Convenience constructor, makes a Value type Config object with the given value.
Definition at line 115 of file config.cpp.
rviz::Config::Config | ( | NodePtr | node | ) | [private] |
Definition at line 121 of file config.cpp.
void rviz::Config::copy | ( | const Config & | source | ) |
Make this a deep copy of source.
Definition at line 125 of file config.cpp.
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 167 of file config.cpp.
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 312 of file config.cpp.
Config rviz::Config::invalidConfig | ( | ) | [static, private] |
Definition at line 162 of file config.cpp.
bool rviz::Config::isValid | ( | ) | const |
Returns true if the internal Node reference is valid, false if not. Same as (getType() != Invalid).
Definition at line 300 of file config.cpp.
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 334 of file config.cpp.
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 322 of file config.cpp.
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 317 of file config.cpp.
void rviz::Config::makeValid | ( | ) | [private] |
If the node pointer is NULL, this sets it to a new empty node.
Definition at line 292 of file config.cpp.
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 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 270 of file config.cpp.
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 201 of file config.cpp.
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 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 245 of file config.cpp.
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 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 229 of file config.cpp.
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 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 281 of file config.cpp.
bool rviz::Config::mapGetValue | ( | const QString & | key, |
QVariant * | value_out | ||
) | const |
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 344 of file config.cpp.
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 190 of file config.cpp.
void rviz::Config::mapSetValue | ( | const QString & | key, |
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 185 of file config.cpp.
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 . 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 172 of file config.cpp.
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 305 of file config.cpp.
friend class MapIterator [friend] |
NodePtr rviz::Config::node_ [private] |