Q: pointers to capnp::Builder

26 views
Skip to first unread message

Stefan Kaps

unread,
Sep 4, 2019, 2:10:36 PM9/4/19
to Cap'n Proto
Hi,

i would like to use pointers to builders in my exporter, so I can pass nullptr in case of "please simply do a check, but do not build capnp-data".  Is this possible at all?

If not, are there any suggestions you have for me how to provide such functionality?

Thanks a lot in advance,
Stefan

Kenton Varda

unread,
Sep 4, 2019, 6:43:18 PM9/4/19
to Stefan Kaps, Cap'n Proto
Hi Stefan,

A Builder already acts like a pointer: You can copy it by value, and the copy manipulates the same underlying message.

If you just want to express the type "capnp::Builder or null", then I suggest using `kj::Maybe<capnp::Builder>`, which serves exactly this purpose. A `kj::Maybe<T>` can be assigned to any `T` or to `nullptr`. You can unpack a Maybe with `KJ_IF_MAYBE()`. Check out the comments in `kj/commen.h` and copious examples throughout the KJ and Cap'n Proto source code to learn more.

-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/c4c89dfb-6700-406e-99bf-e5c01c89fddf%40googlegroups.com.

Stefan Kaps

unread,
Sep 4, 2019, 6:51:25 PM9/4/19
to Cap'n Proto
Oh, very cool.  I had not yet stumbled upon it.  This is definitly better than my solution, as it most likely won't generate any code where it's not needed.

Just an hour ago I wrote my own little hacky thing: a simple asm function that returns its first parameter.

extern "C" {
    myT
*refPtr( myT & );
};

As the asm simply does
    mov rax, rcx
    ret

Works fine as we know under the hood references are pointers.  I would hope the CPU itself would optimize around the call-ret, seeing only one uOp being produced before reaching the "ret".


Am Donnerstag, 5. September 2019 00:43:18 UTC+2 schrieb Kenton Varda:
Hi Stefan,

A Builder already acts like a pointer: You can copy it by value, and the copy manipulates the same underlying message.

If you just want to express the type "capnp::Builder or null", then I suggest using `kj::Maybe<capnp::Builder>`, which serves exactly this purpose. A `kj::Maybe<T>` can be assigned to any `T` or to `nullptr`. You can unpack a Maybe with `KJ_IF_MAYBE()`. Check out the comments in `kj/commen.h` and copious examples throughout the KJ and Cap'n Proto source code to learn more.

-Kenton

On Wed, Sep 4, 2019 at 11:10 AM Stefan Kaps <st0f...@gmail.com> wrote:
Hi,

i would like to use pointers to builders in my exporter, so I can pass nullptr in case of "please simply do a check, but do not build capnp-data".  Is this possible at all?

If not, are there any suggestions you have for me how to provide such functionality?

Thanks a lot in advance,
Stefan

--
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.

Kenton Varda

unread,
Sep 4, 2019, 6:56:49 PM9/4/19
to Stefan Kaps, Cap'n Proto
Hmm, can't that function be equivalently written as:

myT* refPtr(myT& ref) { return &ref; }

?

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/97f32335-e03f-45ba-8125-12a121af6df8%40googlegroups.com.

Stefan Kaps

unread,
Sep 4, 2019, 7:04:58 PM9/4/19
to Cap'n Proto
Yes, indeed.  I was troubling my mind of the lifetimes of those references.  And without thinking about it, I just went to work, hehe.  But indeed, as I do recursion in that case, the Builder's lifetime does never end before the called function returned.

Thus my asm is garbage and I don't even need
kj::Maybe()
...
Reply all
Reply to author
Forward
0 new messages