YARP
Yet Another Robot Platform
os/browse_bottle.cpp

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

/*
* Copyright (C) 2006-2018 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 <yarp/os/Bottle.h>
#include <yarp/os/Vocab.h>
using namespace yarp::os;
void showBottle(Bottle& anUnknownBottle, int indentation = 0) {
for (int i=0; i<anUnknownBottle.size(); i++) {
for (int j=0; j<indentation; j++) { printf(" "); }
printf("[%d]: ", 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", Vocab::decode(element.asVocab()).c_str());
break;
default:
if (element.isList()) {
Bottle *lst = element.asList();
printf("list of %d elements\n", lst->size());
showBottle(*lst,indentation+2);
} else {
printf("unrecognized type\n");
}
break;
}
}
}
int main() {
Bottle anUnknownBottle("equals 7 (add (add 2 3) 2)");
showBottle(anUnknownBottle);
return 0;
}