Q: C++ serialization - how to reorder the contents of a list

27 views
Skip to first unread message

Stefan Kaps

unread,
Sep 25, 2019, 8:09:49 AM9/25/19
to Cap'n Proto
Hi @all,

during serialization I need to reorder the contents of a list after creation.  How would I do that?  As far as I understood, it's all pointers.  So are there capnp::List<> - operators to swap 2 items?

All the best,
Stefan

Kenton Varda

unread,
Sep 25, 2019, 2:51:42 PM9/25/19
to Stefan Kaps, Cap'n Proto
Hi Stefan,

So, a List of structs is actually not encoded as a list of pointers. Instead, the struct bodies appear contiguously. So, to swap two elements in a list of structs, you would need to swap each field value within the two structs. For pointer fields, you could use target.adoptField(source.disownField()) to avoid performing a deep copy. You could write some code based on AnyStruct (capnp/any.h) that does such a swap in a schema-agnostic way, but there's no built-in code for this at present.

List(Text), List(Data), and List(List(T)) are actually encoded as lists of pointers. In those cases the List builders have adopt() and disown() methods that operate on whole elements which you could use to implement swapping pretty easily.

-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/8fbf6664-fbfc-4aa2-b054-894a8f01137e%40googlegroups.com.

Stefan Kaps

unread,
Sep 25, 2019, 3:24:07 PM9/25/19
to Cap'n Proto
Hi Kenton,

thank you for the explanation.  Due to the complex nature of my structs I guess it'd be a better approach to sort the entries before actual creation.

All the best, Stefan


Am Mittwoch, 25. September 2019 20:51:42 UTC+2 schrieb Kenton Varda:
Hi Stefan,

So, a List of structs is actually not encoded as a list of pointers. Instead, the struct bodies appear contiguously. So, to swap two elements in a list of structs, you would need to swap each field value within the two structs. For pointer fields, you could use target.adoptField(source.disownField()) to avoid performing a deep copy. You could write some code based on AnyStruct (capnp/any.h) that does such a swap in a schema-agnostic way, but there's no built-in code for this at present.

List(Text), List(Data), and List(List(T)) are actually encoded as lists of pointers. In those cases the List builders have adopt() and disown() methods that operate on whole elements which you could use to implement swapping pretty easily.

-Kenton

On Wed, Sep 25, 2019 at 5:09 AM Stefan Kaps <st0f...@gmail.com> wrote:
Hi @all,

during serialization I need to reorder the contents of a list after creation.  How would I do that?  As far as I understood, it's all pointers.  So are there capnp::List<> - operators to swap 2 items?

All the best,
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.
Reply all
Reply to author
Forward
0 new messages