YARP
Yet Another Robot Platform
os/browse_bottle/browse_bottle.cpp

Demonstrates one way to access bottle objects. See also os/bottle_add/bottle_add.cpp

/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <yarp/os/Bottle.h>
#include <yarp/os/Value.h>
#include <yarp/os/Vocab.h>
#include <cstdio>
void showBottle(Bottle& anUnknownBottle, int indentation = 0)
{
for (size_t i = 0; i < anUnknownBottle.size(); i++) {
for (int j = 0; j < indentation; j++) {
printf(" ");
}
printf("[%zu]: ", i);
Value& element = anUnknownBottle.get(i);
switch (element.getCode()) {
printf("int %d\n", element.asInt32());
break;
printf("float %g\n", element.asFloat64());
break;
printf("string \"%s\"\n", element.asString().c_str());
break;
printf("binary blob of length %zd\n", element.asBlobLength());
break;
printf("vocab [%s]\n", yarp::os::Vocab32::decode(element.asVocab32()).c_str());
break;
default:
if (element.isList()) {
Bottle* lst = element.asList();
printf("list of %zu elements\n", lst->size());
showBottle(*lst, indentation + 2);
} else {
printf("unrecognized type\n");
}
break;
}
}
}
int main(int argc, char* argv[])
{
YARP_UNUSED(argc);
YARP_UNUSED(argv);
Bottle anUnknownBottle("equals 7 (add (add 2 3) 2)");
showBottle(anUnknownBottle);
return 0;
}
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:25
#define BOTTLE_TAG_INT32
Definition: Bottle.h:21
#define BOTTLE_TAG_STRING
Definition: Bottle.h:26
#define BOTTLE_TAG_BLOB
Definition: Bottle.h:27
#define BOTTLE_TAG_VOCAB32
Definition: Bottle.h:23
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
A single value (typically within a Bottle).
Definition: Value.h:45
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:33
#define YARP_UNUSED(var)
Definition: api.h:162
int main(int argc, char *argv[])
Definition: yarpros.cpp:264