A simple collection of objects that can be described and transmitted in a portable way. More...
#include <yarp/os/Bottle.h>
Public Types | |
using | size_type = size_t |
Public Member Functions | |
Bottle () | |
Constructor. More... | |
Bottle (const std::string &text) | |
Creates a bottle from a string. More... | |
Bottle (const Bottle &rhs) | |
Copy constructor. More... | |
Bottle (Bottle &&rhs) noexcept | |
Move constructor. More... | |
Bottle (std::initializer_list< yarp::os::Value > values) | |
Initializer list constructor. More... | |
Bottle & | operator= (const Bottle &rhs) |
Copy assignment operator. More... | |
Bottle & | operator= (Bottle &&rhs) noexcept |
Move assignment operator. More... | |
virtual | ~Bottle () |
Destructor. More... | |
void | clear () |
Empties the bottle of any objects it contains. More... | |
void | addInt (int x) |
Places an integer in the bottle, at the end of the list. More... | |
void | addInt8 (std::int8_t x) |
Places a 8-bit integer in the bottle, at the end of the list. More... | |
void | addInt16 (std::int16_t x) |
Places a 16-bit integer in the bottle, at the end of the list. More... | |
void | addInt32 (std::int32_t x) |
Places a 32-bit integer in the bottle, at the end of the list. More... | |
void | addInt64 (std::int64_t x) |
Places a 64-bit integer in the bottle, at the end of the list. More... | |
void | addVocab (int x) |
Places a vocabulary item in the bottle, at the end of the list. More... | |
void | addDouble (double x) |
Places a floating point number in the bottle, at the end of the list. More... | |
void | addFloat32 (yarp::conf::float32_t x) |
Places a 32-bit floating point number in the bottle, at the end of the list. More... | |
void | addFloat64 (yarp::conf::float64_t x) |
Places a 64-bit floating point number in the bottle, at the end of the list. More... | |
void | addString (const char *str) |
Places a string in the bottle, at the end of the list. More... | |
void | addString (const std::string &str) |
Places a string in the bottle, at the end of the list. More... | |
void | add (const Value &value) |
Add a Value to the bottle, at the end of the list. More... | |
void | add (Value *value) |
Add a Value to the bottle, at the end of the list. More... | |
Bottle & | addList () |
Places an empty nested list in the bottle, at the end of the list. More... | |
Property & | addDict () |
Places an empty key/value object in the bottle, at the end of the list. More... | |
Value | pop () |
Removes a Value v from the end of the list and returns this value. More... | |
Value & | get (size_type index) const |
Reads a Value v from a certain part of the list. More... | |
size_type | size () const |
Gets the number of elements in the bottle. More... | |
void | fromString (const std::string &text) |
Initializes bottle from a string. More... | |
void | fromBinary (const char *buf, size_t len) |
Initializes bottle from a binary representation. More... | |
const char * | toBinary (size_t *size=nullptr) |
Returns binary representation of bottle. More... | |
std::string | toString () const override |
Gives a human-readable textual representation of the bottle. More... | |
bool | write (ConnectionWriter &writer) const override |
Output a representation of the bottle to a network connection. More... | |
bool | read (ConnectionReader &reader) override |
Set the bottle's value based on input from a network connection. More... | |
bool | write (PortReader &reader, bool textMode=false) |
Copy the bottle's value to a object that can read a serialization. More... | |
bool | read (const PortWriter &writer, bool textMode=false) |
Set the bottle's value based on input from a serializable object. More... | |
void | onCommencement () const override |
This is called when the port is about to begin writing operations. More... | |
bool | check (const std::string &key) const override |
Check if there exists a property of the given name. 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... | |
bool | isNull () const override |
Checks if the object is invalid. More... | |
void | copy (const Bottle &alt, size_type first=0, size_type len=npos) |
Copy all or part of another Bottle. More... | |
bool | operator== (const Bottle &alt) const |
Equality test. More... | |
bool | operator!= (const Bottle &alt) const |
Inequality test. More... | |
void | append (const Bottle &alt) |
Append the content of the given bottle to the current list. More... | |
Bottle | tail () const |
Get all but the first element of a bottle. More... | |
void | hasChanged () |
Declare that the content of the Bottle has been changed. More... | |
int | getSpecialization () |
Get numeric bottle code for this bottle. 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::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 yarp::os::Type | getWriteType () const |
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... | |
Static Public Member Functions | |
static Bottle & | getNullBottle () |
A special Bottle with no content. More... | |
static std::string | toString (int x) |
static std::string | describeBottleCode (int code) |
Convert a numeric bottle code to a string. More... | |
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... | |
Static Public Attributes | |
static const size_type | npos = static_cast<Bottle::size_type>(-1) |
Protected Member Functions | |
void | setReadOnly (bool readOnly) |
Friends | |
class | yarp::os::NetworkBase |
A simple collection of objects that can be described and transmitted in a portable way.
Objects are stored in a list, which you can add to and access. It is handy to use until you feel the need to make your own more efficient formats for transmission.
Here's an example of using a Bottle:
This class has a well-defined, documented representation in both binary and text form. The name of this class comes from the idea of throwing a "message in a bottle" into the network and hoping it will eventually wash ashore somewhere else. In the very early days of YARP, that is what communication felt like.
using yarp::os::Bottle::size_type = size_t |
Bottle::Bottle | ( | ) |
|
explicit |
Creates a bottle from a string.
The string which should contain a textual form of the bottle, e.g. text = "10 -5.3 1.0 \"hello there"" would give a bottle with 4 elements: an integer, two floating point numbers, and a string.
text | the textual form of the bottle to be interpreted. |
Definition at line 57 of file Bottle.cpp.
Bottle::Bottle | ( | const Bottle & | rhs | ) |
|
noexcept |
yarp::os::Bottle::Bottle | ( | std::initializer_list< yarp::os::Value > | values | ) |
|
virtual |
Destructor.
Definition at line 119 of file Bottle.cpp.
void Bottle::add | ( | const Value & | value | ) |
Add a Value to the bottle, at the end of the list.
value | the Value to add. |
Definition at line 339 of file Bottle.cpp.
void Bottle::add | ( | Value * | value | ) |
Add a Value to the bottle, at the end of the list.
The object passed will be placed directly into the list, without copying. The Bottle will be responsible for deallocating it when appropriate.
value | the Value to add. |
Definition at line 333 of file Bottle.cpp.
Property & Bottle::addDict | ( | ) |
Places an empty key/value object in the bottle, at the end of the list.
Definition at line 191 of file Bottle.cpp.
|
inline |
void Bottle::addFloat32 | ( | yarp::conf::float32_t | x | ) |
Places a 32-bit floating point number in the bottle, at the end of the list.
x | the number to add. |
Definition at line 155 of file Bottle.cpp.
void Bottle::addFloat64 | ( | yarp::conf::float64_t | x | ) |
Places a 64-bit floating point number in the bottle, at the end of the list.
x | the number to add. |
Definition at line 161 of file Bottle.cpp.
|
inline |
void Bottle::addInt16 | ( | std::int16_t | x | ) |
Places a 16-bit integer in the bottle, at the end of the list.
x | the 16-bit integer to add. |
Definition at line 137 of file Bottle.cpp.
void Bottle::addInt32 | ( | std::int32_t | x | ) |
Places a 32-bit integer in the bottle, at the end of the list.
x | the 32-bit integer to add. |
Definition at line 143 of file Bottle.cpp.
void Bottle::addInt64 | ( | std::int64_t | x | ) |
Places a 64-bit integer in the bottle, at the end of the list.
x | the 64-bit integer to add. |
Definition at line 149 of file Bottle.cpp.
void Bottle::addInt8 | ( | std::int8_t | x | ) |
Places a 8-bit integer in the bottle, at the end of the list.
x | the 8-bit integer to add. |
Definition at line 131 of file Bottle.cpp.
Bottle & Bottle::addList | ( | ) |
Places an empty nested list in the bottle, at the end of the list.
The list itself is represented as a bottle.
Definition at line 185 of file Bottle.cpp.
void Bottle::addString | ( | const char * | str | ) |
Places a string in the bottle, at the end of the list.
str | the string to add. |
Definition at line 173 of file Bottle.cpp.
void Bottle::addString | ( | const std::string & | str | ) |
Places a string in the bottle, at the end of the list.
str | the string to add. |
Definition at line 179 of file Bottle.cpp.
void Bottle::addVocab | ( | int | x | ) |
Places a vocabulary item in the bottle, at the end of the list.
x | the item to add. |
Definition at line 167 of file Bottle.cpp.
void Bottle::append | ( | const Bottle & | alt | ) |
Append the content of the given bottle to the current list.
alt | the content to add. |
Definition at line 383 of file Bottle.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 280 of file Bottle.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 Bottle::clear | ( | ) |
Empties the bottle of any objects it contains.
Definition at line 124 of file Bottle.cpp.
Copy all or part of another Bottle.
alt | The object to copy. |
first | The index of the first element to copy. |
len | The number of elements to copy (-1 for all). |
Definition at line 269 of file Bottle.cpp.
|
static |
Convert a numeric bottle code to a string.
code | the code to convert |
Definition at line 406 of file Bottle.cpp.
|
overridevirtual |
Gets a value corresponding to a given keyword.
key | The keyword to look for |
Implements yarp::os::Searchable.
Definition at line 290 of file Bottle.cpp.
|
overridevirtual |
Gets a list corresponding to a given keyword.
key | The keyword to look for |
Implements yarp::os::Searchable.
Definition at line 305 of file Bottle.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 Bottle::fromBinary | ( | const char * | buf, |
size_t | len | ||
) |
Initializes bottle from a binary representation.
buf | the binary form of the bottle to be interpreted. |
len | the length of the binary form. |
Definition at line 219 of file Bottle.cpp.
void Bottle::fromString | ( | const std::string & | text | ) |
Initializes bottle from a string.
The string should contain a textual form of the bottle, e.g. text = "10 -5.3 1.0 \"hello there"" would give a bottle with 4 elements: an integer, two floating point numbers, and a string.
text | the textual form of the bottle to be interpreted. |
Definition at line 207 of file Bottle.cpp.
Reads a Value v from a certain part of the list.
Methods like v.isInt32() or v.isString() can be used to check the type of the result. Methods like v.asInt32() or v.asString() can be used to access the result as a particular type.
index | the part of the list to read from. |
Definition at line 249 of file Bottle.cpp.
|
static |
A special Bottle with no content.
Definition at line 345 of file Bottle.cpp.
int Bottle::getSpecialization | ( | ) |
Get numeric bottle code for this bottle.
Definition at line 264 of file Bottle.cpp.
void Bottle::hasChanged | ( | ) |
Declare that the content of the Bottle has been changed.
It is important to call this if you modify an individual element of the Bottle through assignment, so that serialization happens correctly.
Definition at line 259 of file Bottle.cpp.
|
overridevirtual |
Checks if the object is invalid.
Reimplemented from yarp::os::Searchable.
Definition at line 373 of file Bottle.cpp.
|
overridevirtual |
This is called when the port is about to begin writing operations.
After this point, the write method may be called zero, once, or many times by YARP depending on the mix of formats and protocols in use.
Reimplemented from yarp::os::PortWriter.
Definition at line 238 of file Bottle.cpp.
bool Bottle::operator!= | ( | const Bottle & | alt | ) | const |
Inequality test.
alt | the value to compare against. |
Definition at line 378 of file Bottle.cpp.
Move assignment operator.
rhs | The object to move. |
Definition at line 111 of file Bottle.cpp.
Copy assignment operator.
rhs | The object to copy. |
Definition at line 102 of file Bottle.cpp.
bool Bottle::operator== | ( | const Bottle & | alt | ) | const |
Equality test.
alt | the value to compare against. |
Definition at line 351 of file Bottle.cpp.
Value Bottle::pop | ( | ) |
Removes a Value v from the end of the list and returns this value.
Definition at line 197 of file Bottle.cpp.
|
overridevirtual |
Set the bottle's value based on input from a network connection.
reader | the interface to the network connection for reading |
Implements yarp::os::Portable.
Definition at line 243 of file Bottle.cpp.
bool Bottle::read | ( | const PortWriter & | writer, |
bool | textMode = false |
||
) |
Set the bottle's value based on input from a serializable object.
Must be serialized in standard Bottle-compatible format.
writer | the serializable object. |
textMode | true if text serialization should be used. |
Definition at line 364 of file Bottle.cpp.
|
protected |
Definition at line 442 of file Bottle.cpp.
size_t Bottle::size | ( | ) | const |
Gets the number of elements in the bottle.
Definition at line 254 of file Bottle.cpp.
Bottle Bottle::tail | ( | ) | const |
Get all but the first element of a bottle.
Definition at line 391 of file Bottle.cpp.
const char * Bottle::toBinary | ( | size_t * | size = nullptr | ) |
Returns binary representation of bottle.
Lifetime is until next modification of bottle.
size | if non-null, the referenced variable is set to the buffer size. |
Definition at line 225 of file Bottle.cpp.
|
overridevirtual |
Gives a human-readable textual representation of the bottle.
This representation is suitable for passing to Bottle::fromString (see that method for examples).
Implements yarp::os::Searchable.
Definition at line 214 of file Bottle.cpp.
|
static |
Definition at line 401 of file Bottle.cpp.
|
overridevirtual |
Output a representation of the bottle to a network connection.
writer | the interface to the network connection for writing |
Implements yarp::os::Portable.
Definition at line 233 of file Bottle.cpp.
bool Bottle::write | ( | PortReader & | reader, |
bool | textMode = false |
||
) |
Copy the bottle's value to a object that can read a serialization.
Must be serialized in standard Bottle-compatible format.
reader | the serializable object. |
textMode | true if text serialization should be used. |
Definition at line 356 of file Bottle.cpp.
|
friend |
|
static |