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

/*
* 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 <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::Vocab::decode(element.asVocab()).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;
}
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
BOTTLE_TAG_STRING
#define BOTTLE_TAG_STRING
Definition: Bottle.h:28
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
BOTTLE_TAG_INT32
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
yarp::os::Vocab::decode
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
BOTTLE_TAG_VOCAB
#define BOTTLE_TAG_VOCAB
Definition: Bottle.h:25
BOTTLE_TAG_BLOB
#define BOTTLE_TAG_BLOB
Definition: Bottle.h:29
BOTTLE_TAG_FLOAT64
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
Vocab.h
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
Bottle.h
Value.h