0
我剛剛開始嘗試cap'n proto,並試圖提高我的理解。爲什麼我不需要在capnproto中使用AnyPointer :: initAs?
現在我正在努力研究如何最好地使用AnyPointer,同時嘗試我注意到我似乎不需要爲我的對象調用initAs,並且我設置的值將被正確讀入。
這是我的根容器有任何形式的結構對象
struct TestObject
{
value1 @0 : Int32 = -5;
value2 @1 : Float32 = 9.4;
}
struct TestContainer
{
object @0: AnyPointer;
}
的當我來到連載一些對象它似乎並沒有不管我用initAs或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);
當我再次反序列化時,我從上述任一方法中得到正確的值。在這裏使用getAs是正確的還是會更復雜的TestObject打破東西?