I'm just starting out experimenting with cap'n proto and trying to improve my understanding.
Right now I'm trying to work out how best to use AnyPointer and whilst experimenting I noticed that I didn't seem to need to call initAs for my object and the values I set would be read in correctly.
This is my schema for a root container that has any kind of struct object
struct TestObject
{
value1 @0 : Int32 = -5;
value2 @1 : Float32 = 9.4;
}
struct TestContainer
{
object @0: AnyPointer;
}
When I come to serialise some objects it does not seem to matter whether I use initAs or getAs.
::capnp::MallocMessageBuilder message;
auto container= message.initRoot<TestContainer>();
auto anything = container.initObject();
auto objectBuilder = anything.getAs<TestObject>(); //I was expecting this to break since nothing obviously initialises it.
objectBuilder.setValue1( -2099 );
objectBuilder.setValue2( -3.994f );
//using initAs works as well
auto anything = container.initObject();
auto objectBuilder = anything.initAs<TestObject>();
objectBuilder.setValue1( 270001 );
objectBuilder.setValue2( -65.2f );
When I deserialise again I get the correct values back from either of the above methods. Is using getAs here correct or would a more complicated TestObject break things?