C++: How to "cast" a Capability::Client to AnyPointer

114 views
Skip to first unread message

Michael Berg

unread,
Aug 27, 2019, 8:09:41 AM8/27/19
to Cap'n Proto
Hello,

interface A(T) {
    value @0 () -> (val :T);
}

is there a way to get from a capnp::Capability::Client to a capnp::AnyPointer? 

I'm struggling with some generic schema where I'm implementing "A::Server" which holds an AnyPointer member field to store a value of some type T. An instance of A will later be returned to a client which can then get the value via calling the "value" method. The value to be stored in A can for instance either be a list of Capability::Client or a just a Capability::Client. So when I want to instantiate A I got (e.g. as a parameter to the method) a Capability::Client not an AnyPointer. 
So my problem is, that I can easily get a Capabilty::Client from an AnyPointer via AnyPointer::getAs<some Interface>(), but I find no way to get from a Capabilty::Client to an AnyPointer which I need in order to create an instance of A (in my C++ code). I have the feeling that I'm missing something very obvious.
Can somebody help?

Thank you and best regards

Michael



Kenton Varda

unread,
Aug 27, 2019, 11:50:18 AM8/27/19
to Michael Berg, Cap'n Proto
Hi Michael,

The thing about AnyPointer is that it's not just a pointer to a value -- it's a (native) pointer to a (capnp) pointer, the latter of which has to exist within some larger message.

The right way to initialize an AnyPointer, then, is more like how you'd initialize a nested struct -- you start with the parent object's Builder and call getVal() to get an AnyPointer::Builder, then you call setAs<whatever>(value) on that, like:

  context.getResults().getVal().setAs<SomeInterface>(capClient);

Does that help?

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/708e920e-09bd-4343-a4bd-3c82bf6238e9%40googlegroups.com.

Michael Berg

unread,
Aug 29, 2019, 7:44:27 AM8/29/19
to Cap'n Proto
Hello Kenton,

yes, this helped. Thanks a lot for the help and quick reply. 

Cheers, Michael


On Tuesday, August 27, 2019 at 5:50:18 PM UTC+2, Kenton Varda wrote:
Hi Michael,

The thing about AnyPointer is that it's not just a pointer to a value -- it's a (native) pointer to a (capnp) pointer, the latter of which has to exist within some larger message.

The right way to initialize an AnyPointer, then, is more like how you'd initialize a nested struct -- you start with the parent object's Builder and call getVal() to get an AnyPointer::Builder, then you call setAs<whatever>(value) on that, like:

  context.getResults().getVal().setAs<SomeInterface>(capClient);

Does that help?

-Kenton

On Tue, Aug 27, 2019 at 7:09 AM Michael Berg <suuri...@gmail.com> wrote:
Hello,

interface A(T) {
    value @0 () -> (val :T);
}

is there a way to get from a capnp::Capability::Client to a capnp::AnyPointer? 

I'm struggling with some generic schema where I'm implementing "A::Server" which holds an AnyPointer member field to store a value of some type T. An instance of A will later be returned to a client which can then get the value via calling the "value" method. The value to be stored in A can for instance either be a list of Capability::Client or a just a Capability::Client. So when I want to instantiate A I got (e.g. as a parameter to the method) a Capability::Client not an AnyPointer. 
So my problem is, that I can easily get a Capabilty::Client from an AnyPointer via AnyPointer::getAs<some Interface>(), but I find no way to get from a Capabilty::Client to an AnyPointer which I need in order to create an instance of A (in my C++ code). I have the feeling that I'm missing something very obvious.
Can somebody help?

Thank you and best regards

Michael



--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capn...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages