Demonstrates one way to access bottle objects. See also os/bottle_add/bottle_add.cpp
#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;
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[])
{
Bottle anUnknownBottle("equals 7 (add (add 2 3) 2)");
showBottle(anUnknownBottle);
return 0;
}