YARP  2.3.70
Yet Another Robot Platform
os/browse_bottle.cpp

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

/*
* Copyright: (C) 2010 RobotCub Consortium
* Author: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#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.asInt());
break;
printf("float %g\n", element.asDouble());
break;
printf("string \"%s\"\n", element.asString().c_str());
break;
printf("binary blob of length %d\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;
}