A class for storing options and configuration information. More...
#include <yarp/os/Property.h>
Classes | |
class | Private |
Public Member Functions | |
Property () | |
Constructor. More... | |
Property (int hash_size) | |
Constructor. More... | |
Property (const char *str) | |
Initialize from a string, using fromString(). More... | |
Property (const Property &rhs) | |
Copy constructor. More... | |
Property (Property &&rhs) noexcept | |
Move constructor. More... | |
Property (std::initializer_list< std::pair< std::string, yarp::os::Value >> values) | |
Initializer list constructor. More... | |
~Property () override | |
Destructor. More... | |
Property & | operator= (const Property &prop) |
Copy assignment operator. More... | |
Property & | operator= (Property &&prop) noexcept |
Move assignment operator. More... | |
bool | check (const std::string &key) const override |
Check if there exists a property of the given name. More... | |
void | put (const std::string &key, const std::string &value) |
Associate the given key with the given string. More... | |
void | put (const std::string &key, const Value &value) |
Associate the given key with the given value. More... | |
void | put (const std::string &key, Value *value) |
Associate the given key with the given value. More... | |
void | put (const std::string &key, int value) |
Associate the given key with the given integer. More... | |
void | put (const std::string &key, double value) |
Associate the given key with the given floating point number. More... | |
Property & | addGroup (const std::string &key) |
Add a nested group. More... | |
void | unput (const std::string &key) |
Remove the association from the given key to a value, if present. More... | |
Value & | find (const std::string &key) const override |
Gets a value corresponding to a given keyword. More... | |
Bottle & | findGroup (const std::string &key) const override |
Gets a list corresponding to a given keyword. More... | |
void | clear () |
Remove all associations. More... | |
void | fromString (const std::string &txt, bool wipe=true) |
Interprets a string as a list of properties. More... | |
void | fromCommand (int argc, char *argv[], bool skipFirst=true, bool wipe=true) |
Interprets a list of command arguments as a list of properties. More... | |
void | fromCommand (int argc, const char *argv[], bool skipFirst=true, bool wipe=true) |
Interprets a list of command arguments as a list of properties. More... | |
void | fromArguments (const char *arguments, bool wipe=true) |
Interprets a list of command arguments as a list of properties. More... | |
bool | fromConfigFile (const std::string &fname, bool wipe=true) |
Interprets a file as a list of properties. More... | |
bool | fromConfigFile (const std::string &fname, Searchable &env, bool wipe=true) |
Variant of fromConfigFile(fname, wipe) that includes extra "environment variables". More... | |
bool | fromConfigDir (const std::string &dirname, const std::string §ion=std::string(), bool wipe=true) |
Interprets all files in a directory as lists of properties as described in fromConfigFile(). More... | |
void | fromConfig (const char *txt, bool wipe=true) |
Parses text in the configuration format described in fromConfigFile(). More... | |
void | fromConfig (const char *txt, Searchable &env, bool wipe=true) |
Variant of fromConfig(const char*, bool) that includes extra "environment variables". More... | |
void | fromQuery (const char *url, bool wipe=true) |
Parses text in a url. More... | |
std::string | toString () const override |
Return a standard text representation of the content of the object. More... | |
bool | read (ConnectionReader &reader) override |
Read this object from a network connection. More... | |
bool | write (ConnectionWriter &writer) const override |
Write this object to a network connection. More... | |
virtual bool | check (const std::string &key) const=0 |
Check if there exists a property of the given name. More... | |
virtual bool | check (const std::string &key, const std::string &comment) const |
Check if there exists a property of the given name. More... | |
virtual bool | check (const std::string &key, Value *&result, const std::string &comment="") const |
Gets a value corresponding to a given keyword. More... | |
virtual Value | check (const std::string &key, const Value &fallback, const std::string &comment="") const |
Gets a value corresponding to a given keyword. More... | |
virtual Bottle & | findGroup (const std::string &key) const=0 |
Gets a list corresponding to a given keyword. More... | |
Bottle & | findGroup (const std::string &key, const std::string &comment) const |
Gets a list corresponding to a given keyword. More... | |
Public Member Functions inherited from yarp::os::Searchable | |
Searchable () | |
Default constructor. More... | |
Searchable (const Searchable &rhs)=default | |
Copy constructor. More... | |
Searchable (Searchable &&rhs) noexcept=default | |
Move constructor. More... | |
virtual | ~Searchable () |
Destructor. More... | |
Searchable & | operator= (const Searchable &rhs)=default |
Copy assignment operator. More... | |
Searchable & | operator= (Searchable &&rhs) noexcept=default |
Move assignment operator. More... | |
virtual bool | check (const std::string &key, const std::string &comment) const |
Check if there exists a property of the given name. More... | |
Bottle & | findGroup (const std::string &key, const std::string &comment) const |
Gets a list corresponding to a given keyword. More... | |
virtual bool | check (const std::string &key, Value *&result, const std::string &comment="") const |
Gets a value corresponding to a given keyword. More... | |
virtual Value | check (const std::string &key, const Value &fallback, const std::string &comment="") const |
Gets a value corresponding to a given keyword. More... | |
virtual bool | isNull () const |
Checks if the object is invalid. More... | |
Public Member Functions inherited from yarp::os::Portable | |
virtual Type | getType () const |
Public Member Functions inherited from yarp::os::PortReader | |
virtual | ~PortReader () |
Destructor. More... | |
virtual Type | getReadType () const |
Public Member Functions inherited from yarp::os::PortWriter | |
virtual | ~PortWriter () |
Destructor. More... | |
virtual void | onCompletion () const |
This is called when the port has finished all writing operations. More... | |
virtual void | onCommencement () const |
This is called when the port is about to begin writing operations. More... | |
virtual yarp::os::Type | getWriteType () const |
Additional Inherited Members | |
Static Public Member Functions inherited from yarp::os::Portable | |
static bool | copyPortable (const PortWriter &writer, PortReader &reader) |
Copy one portable to another, via writing and reading. More... | |
A class for storing options and configuration information.
Use put() to add keyword/value pairs, and get() or check() to look them up afterwards. It can read from configuration files using the fromConfigFile() method, and from command line options using the fromCommand() method, and from any Searchable object (include Bottle objects) using the fromString() method. Property objects can be searched efficiently.
Definition at line 34 of file Property.h.
Property::Property | ( | ) |
Constructor.
Definition at line 919 of file Property.cpp.
Property::Property | ( | int | hash_size | ) |
Constructor.
hash_size | a scalar controlling efficiency of the hash map storing the data. Set to 0 for default size. The bigger this number, the more memory used, but the more efficient the map. |
Definition at line 927 of file Property.cpp.
Property::Property | ( | const char * | str | ) |
Initialize from a string, using fromString().
Definition at line 936 of file Property.cpp.
Property::Property | ( | const Property & | rhs | ) |
Copy constructor.
Definition at line 944 of file Property.cpp.
|
noexcept |
Move constructor.
Definition at line 952 of file Property.cpp.
Property::Property | ( | std::initializer_list< std::pair< std::string, yarp::os::Value >> | values | ) |
Initializer list constructor.
[in] | values,list | of std::pair with which initialize the Property. |
Definition at line 962 of file Property.cpp.
|
override |
Destructor.
Definition at line 972 of file Property.cpp.
Property & yarp::os::Property::addGroup | ( | const std::string & | key | ) |
Add a nested group.
key | the key |
Definition at line 1215 of file Property.cpp.
|
overridevirtual |
Check if there exists a property of the given name.
key | the name to check for |
Implements yarp::os::Searchable.
Definition at line 1024 of file Property.cpp.
virtual bool yarp::os::Searchable::check |
Check if there exists a property of the given name.
key | the name to check for |
bool yarp::os::Searchable::check |
Check if there exists a property of the given name.
key | the name to check for |
comment | Human-readable explanation |
Definition at line 121 of file Searchable.cpp.
yarp::os::Value yarp::os::Searchable::check |
Gets a value corresponding to a given keyword.
key | The keyword to look for |
fallback | A default value to return if nothing found |
comment | Optional human-readable explanation |
Definition at line 198 of file Searchable.cpp.
bool yarp::os::Searchable::check |
Gets a value corresponding to a given keyword.
If a property does not exist, this returns false and does not modify the result pointer. If a property exists but does not have a value, this again returns false and does not modify the result pointer.
key | The keyword to look for |
result | A pointer to store the address of the result in |
comment | Optional human-readable explanation |
Definition at line 184 of file Searchable.cpp.
void Property::clear | ( | ) |
Remove all associations.
Guarantees that find(key).isNull()
will be true for all values of key
.
Definition at line 1040 of file Property.cpp.
|
overridevirtual |
Gets a value corresponding to a given keyword.
key | The keyword to look for |
Implements yarp::os::Searchable.
Definition at line 1034 of file Property.cpp.
|
overridevirtual |
Gets a list corresponding to a given keyword.
key | The keyword to look for |
Implements yarp::os::Searchable.
Definition at line 1125 of file Property.cpp.
virtual Bottle& yarp::os::Searchable::findGroup |
Gets a list corresponding to a given keyword.
key | The keyword to look for |
yarp::os::Bottle & yarp::os::Searchable::findGroup |
Gets a list corresponding to a given keyword.
key | The keyword to look for |
comment | Human-readable explanation |
Definition at line 157 of file Searchable.cpp.
void Property::fromArguments | ( | const char * | arguments, |
bool | wipe = true |
||
) |
Interprets a list of command arguments as a list of properties.
Keys are named by beginning with --
. For example, with argv = program_name --width 10 --height 15
, the Property object will be the mapping {width => 10, height => 15}
. Therefore:
If a key is duplicated, only the latest will be used. For example, with argv = program_name --foo bar --foo baz
, the Property object will be the mapping {foo => baz}.
arguments | the command arguments |
wipe | should Property be emptied first |
Definition at line 1071 of file Property.cpp.
void Property::fromCommand | ( | int | argc, |
char * | argv[], | ||
bool | skipFirst = true , |
||
bool | wipe = true |
||
) |
Interprets a list of command arguments as a list of properties.
Keys are named by beginning with --
. For example, with argv = program_name --width 10 --height 15
, the Property object will be the mapping {width => 10, height => 15}
. Therefore:
If a key is duplicated, only the latest will be used. For example, with argv = program_name --foo bar --foo baz
, the Property object will be the mapping {foo => baz}
.
argc | the number of arguments |
argv | the list of arguments |
skipFirst | set to true if the first argument should be skipped (which is the right thing to do for arguments passed to main()) |
wipe | should Property be emptied first |
Definition at line 1057 of file Property.cpp.
void Property::fromCommand | ( | int | argc, |
const char * | argv[], | ||
bool | skipFirst = true , |
||
bool | wipe = true |
||
) |
Interprets a list of command arguments as a list of properties.
Keys are named by beginning with --
. For example, with argv = program_name --width 10 --height 15
, the Property object will be the mapping {width => 10, height => 15}
. Therefore:
If a key is duplicated, only the latest will be used. For example, with argv = program_name --foo bar --foo baz
, the Property object will be the mapping {foo => baz}
.
argc | the number of arguments |
argv | the list of arguments |
skipFirst | set to true if the first argument should be skipped (which is the right thing to do for arguments passed to main()) |
wipe | should Property be emptied first |
Definition at line 1066 of file Property.cpp.
void Property::fromConfig | ( | const char * | txt, |
bool | wipe = true |
||
) |
Parses text in the configuration format described in fromConfigFile().
txt | the configuration text |
wipe | should Property be emptied first |
Definition at line 1093 of file Property.cpp.
void Property::fromConfig | ( | const char * | txt, |
Searchable & | env, | ||
bool | wipe = true |
||
) |
Variant of fromConfig(const char*, bool) that includes extra "environment variables".
These will be expanded, along with any other variables in the environment, if present in the configuration file in $variable
or
${variable}
form.
txt | the configuration text |
env | extra set of environment variables |
wipe | should Property be emptied first |
Definition at line 1099 of file Property.cpp.
bool Property::fromConfigDir | ( | const std::string & | dirname, |
const std::string & | section = std::string() , |
||
bool | wipe = true |
||
) |
Interprets all files in a directory as lists of properties as described in fromConfigFile().
dirname | the name of the file to read from |
section | if specified names the subsection for each file, otherwise use the same section for all files |
wipe | should Property be emptied first |
Definition at line 1076 of file Property.cpp.
bool Property::fromConfigFile | ( | const std::string & | fname, |
bool | wipe = true |
||
) |
Interprets a file as a list of properties.
For example, for a file containing:
the Property object will be the mapping {width => 10, height => 15}. In other words:
If a key is duplicated, only the latest will be used. For example, for a file containing:
the Property object will be the mapping {foo => baz}.
Lines of the form "[NAME]" will result in nested subproperties. For example, for a file containing:
the structure of the Property object will be "(SIZE (width 10) (height 15)) (APPEARANCE (color red))". In other words:
It is possible to nest configuration files. To include the configuration file "preamble.ini" inside another one, do:
This will insert the content of preamble.ini as if cut-and-pasted. If rather you would like the content included within a subsection called FOO, do instead:
fname | the name of the file to read from |
wipe | should Property be emptied first |
Definition at line 1081 of file Property.cpp.
bool Property::fromConfigFile | ( | const std::string & | fname, |
Searchable & | env, | ||
bool | wipe = true |
||
) |
Variant of fromConfigFile(fname, wipe) that includes extra "environment variables".
These will be expanded, along with any other variables in the environment, if present in the configuration file in $variable
or ${variable}
form.
fname | the name of the file to read from |
env | extra set of environment variables |
wipe | should Property be emptied first |
Definition at line 1088 of file Property.cpp.
void Property::fromQuery | ( | const char * | url, |
bool | wipe = true |
||
) |
Parses text in a url.
Anything of the form foo=bar
is added as a property.
url | the text of the url, of form ...prop1=val1&prop2=val2... |
wipe | should Property be emptied first |
Definition at line 1153 of file Property.cpp.
void Property::fromString | ( | const std::string & | txt, |
bool | wipe = true |
||
) |
Interprets a string as a list of properties.
Uses the the same format as a Bottle. The first element of every sub-list is interpreted as a key. For example, with text = (width 10) (height 15)
, the Property object will be the mapping {width => 10, height => 15}
. Therefore:
If a key is duplicated, only the latest will be used. For example, with text = (foo bar) (foo baz)
, the Property object will be the mapping {foo => baz}
.
Definition at line 1046 of file Property.cpp.
Copy assignment operator.
Definition at line 977 of file Property.cpp.
Move assignment operator.
Definition at line 988 of file Property.cpp.
void Property::put | ( | const std::string & | key, |
const std::string & | value | ||
) |
Associate the given key
with the given string.
After the association find(key).asString()
will return that string. If key
is already associated, the value will be replaced with the new one.
key | the key |
value | the string value |
Definition at line 998 of file Property.cpp.
void Property::put | ( | const std::string & | key, |
const Value & | value | ||
) |
Associate the given key
with the given value.
After the association find(key).asString()
will return that value. If key
is already associated, the value will be replaced with the new one.
key | the key |
value | the value |
Definition at line 1003 of file Property.cpp.
void Property::put | ( | const std::string & | key, |
double | value | ||
) |
Associate the given key
with the given floating point number.
After the association find(key).asDouble()
will return that number. If key
is already associated, the value will be replaced with the new one.
key | the key |
value | the floating point value |
Definition at line 1019 of file Property.cpp.
void Property::put | ( | const std::string & | key, |
int | value | ||
) |
Associate the given key
with the given integer.
After the association find(key).asInt32()
will return that integer. If key
is already associated, the value will be replaced with the new one.
key | the key |
value | the integer value |
Definition at line 1014 of file Property.cpp.
void Property::put | ( | const std::string & | key, |
Value * | value | ||
) |
Associate the given key
with the given value.
After the association find(key)
will return that value. The Property object is responsible for deallocating the value. If key
is already associated, the value will be replaced with the new one.
key | the key |
value | the value |
Definition at line 1009 of file Property.cpp.
|
overridevirtual |
Read this object from a network connection.
Override this for your particular class.
reader | an interface to the network connection for reading |
Implements yarp::os::Portable.
Definition at line 1105 of file Property.cpp.
|
overridevirtual |
Return a standard text representation of the content of the object.
The representation is readable by the Bottle and Property classes.
Implements yarp::os::Searchable.
Definition at line 1052 of file Property.cpp.
void Property::unput | ( | const std::string & | key | ) |
Remove the association from the given key
to a value, if present.
Guarantees that find(key).isNull()
will be true.
key | the key |
Definition at line 1029 of file Property.cpp.
|
overridevirtual |
Write this object to a network connection.
Override this for your particular class. Be aware that depending on the nature of the connections a port has, and what protocol they use, and how efficient the YARP implementation is, this method may be called once, twice, or many times, as the result of a single call to Port::write
writer | an interface to the network connection for writing |
Implements yarp::os::Portable.
Definition at line 1117 of file Property.cpp.