Hi Dmitry,
In git master, Orphan<List<T>> has a method truncate(size) which does what
you want. So you'd do:
auto orphan = myStruct.disownMyList();
orphan.truncate(size);
myStruct.adoptMyList(kj::mv(orphan));
As an added bonus, if the list is the last object in its message segment,
the truncated tail will actually by removed from the message rather than
leaving empty space.
However, this feature has not made it into any release yet.
-Kenton
On Wed, May 11, 2016 at 8:11 AM, <[email protected]> wrote:
> Great work on capnp! I'm getting a really smooth ride writing code that
> uses it.
>
> A question - is there any way to truncate pre-allocated ::capnp::List? I
> don't mind, that it would leave empty space in the message at the end of
> the list.
> Along the lines:
>
> ...
> myList @0 : List(myType)
> ...
>
> // preallocate
> auto myList = builder.initMyList(max_count);
> // set count elements
> // ...
> //
> myList.truncateToSize(count)
>
>
>
>
>
>
> I understand that there is an approach of creating an intermediate list,
> along these lines:
>
> capnp::Orphanage orphanage;
> std::vector<capnp::Orphan<MyType>> orphans;
>
>
> auto my = orphanage.newOrphan<MyType>();
>
> // Set fields of my
> // ...
>
> orphans.push_back(kj::mv(o));
>
> myList.initOp(orphans.size());
> int i=0;for(auto it = orphans.begin(); it != ops.end(); ++it, ++i)
> sr.getOp().adoptWithCaveats(i, std::move(*it));
>
>
>
> But what I want, seems to be a lot simpler, doesn't need extra copy, etc.
> Just adjust the size field, to truncate the list!
>
>
> Dmitry
>
> --
> 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 [email protected].
> Visit this group at https://groups.google.com/group/capnproto.
>
--
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 [email protected].
Visit this group at https://groups.google.com/group/capnproto.