YARP
Yet Another Robot Platform
bottle/main.cpp

Experiment with bottles and properties.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <yarp/os/Bottle.h>
using namespace yarp::os;
int main(int argc, char *argv[]) {
Bottle b;
b.addString("color");
b.addString("red");
printf("Bottle b is: %s\n", b.toString().c_str());
// should give: color red
Bottle b2;
b2.addString("height");
b2.addInt32(15);
printf("Bottle b2 is: %s\n", b2.toString().c_str());
// should give: height 15
Bottle b3;
b3.addList() = b;
b3.addList() = b2;
printf("Bottle b3 is: %s\n", b3.toString().c_str());
// should give: (color red) (height 15)
printf("color check: %s\n", b3.find("color").asString().c_str());
printf("height check: %d\n", b3.find("height").asInt32());
Bottle b4;
b4.addString("nested");
b4.addList() = b3;
printf("Bottle b4 is: %s\n", b4.toString().c_str());
// should give: nested ((color red) (height 15))
// alternative way to create a Bottle from textual representation
Bottle b5("(pos left top) (size 10)");
printf("Bottle b5 is: %s\n", b5.toString().c_str());
// should give: (pos left top) (size 10)
Bottle b6;
b6 = b5;
b6.addList() = b4;
printf("Bottle b6 is: %s\n", b6.toString().c_str());
// should give: (pos left top) (size 10) (nested ((color red) (height 5))
printf("size check: %d\n", b6.find("size").asInt32());
printf("pos check: %s\n", b6.find("pos").asString().c_str());
// find assumes key->value pairs; for lists, use findGroup
printf("pos group check: %s\n", b6.findGroup("pos").toString().c_str());
// see documentation for Bottle::findGroup
printf("nested check: %s\n", b6.find("nested").toString().c_str());
printf("nested height check: %d\n", b6.find("nested").find("height").asInt32());
printf("\n");
printf("Relationship of Bottle and Property\n");
Property subProp;
subProp.put("hello","there");
subProp.put("fortytwo",42);
printf("subProp: %s\n", subProp.toString().c_str());
if (lst==NULL) {
printf("Failed to allocate list\n");
return 1;
}
lst->asList()->fromString(subProp.toString());
printf("lst: %s\n", lst->toString().c_str());
Property prop;
prop.put("height",15);
prop.put("verbose",1);
prop.put("sub",lst);
printf("prop: %s\n", prop.toString().c_str());
return 0;
}
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:72
yarp::os::Value::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Value.cpp:330
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
yarp::os::Value::makeList
static Value * makeList()
Create a list Value.
Definition: Value.cpp:449
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:259
yarp::os::Bottle::fromString
void fromString(const std::string &text)
Initializes bottle from a string.
Definition: Bottle.cpp:207
yarp::os::Bottle::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Bottle.cpp:290
yarp::os::Bottle::findGroup
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:305
yarp::os::Bottle::addList
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:185
yarp::os::Property::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Property.cpp:1052
Property.h
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::Bottle::addInt32
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:143
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
yarp::os::Value::asInt32
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::Value::asList
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:46
Bottle.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:34