1

鉴于此架构

struct TestObject
{
    value1 @0 : Int32 = -5;
    value2 @1 : Float32 = 9.4;
}

struct TestContainer
{
    object @0: TestObject;
}

是否可以从 C++ 代码中的 TestObject::Builder 获取 AnyPointer::Builder?

这就是我想要做的:

::capnp::MallocMessageBuilder message;
auto container = message.initRoot<TestContainer>();

TestObject::Builder objBuilder = container.initObject();

//Get an AnyPointer
capnp::AnyPointer::Builder anyBuilder = capnp::toAny( objBuilder )(); //No this does not work.

MyTestObject test( 41, 643.7f );
test.serialise( anyBuilder );

我想做的是有一个带有单个参数类型的抽象接口,例如。

class ISerialisable
{
    virtual void serialise(capnp::AnyPointer::Builder& any) = 0;
}

class MyTestObject: public ISerialisable
{
   void serialise(capnp::AnyPointer::Builder& any) override
   {
        auto testObjBuilder = any.getAs<TestObject>(); or should initAs be used?
        testObject.setValue1( whatever1);
        testObject.setValue2( whatever2);
   }
}

有可能沿着这条路线走吗?

4

0 回答 0