How to serialize and deserialize in the same application?

415 views
Skip to first unread message

jacek.m...@gmail.com

unread,
Feb 2, 2017, 9:51:45 AM2/2/17
to Cap'n Proto
Hi
I write simple app. And I want to test efficiency random access Capnpro structures. This is because you claim it is very fast. But I don't know how to create ::capnp::PackedMessageReader mMessageReader;  with  ::capnp::MallocMessageBuilder as input. Can you give me some advices how to do that?

best regards
Jacek

Kenton Varda

unread,
Feb 2, 2017, 9:32:23 PM2/2/17
to jacek.m...@gmail.com, Cap'n Proto
Hi Jacek,

If you wanted to create a MesasgeReader that reads from a message created by some MessageBuilder, you could use SegmentArrayMessageReader, like:

    capnp::SegmentArrayMessageReader reader(builder.getSegmentsForOutput());
    MyType::Reader root = reader.getRoot<MyType>();

This is a little bit of a funny thing to do. The performance will be almost identical to if you had done:

    MyType::Reader root = builder.getRoot<MyType>().asReader();

If you want to test packing/unpacking, then you will need to write the packed data to a kj::OutputStream and then read it back from a kj::InputStream. You could use kj::ArrayOutputStream and kj::ArrayInputStream with a suitably-sized backing array to do this in-memory.

-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+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.

jacek.m...@gmail.com

unread,
Feb 3, 2017, 9:01:32 AM2/3/17
to Cap'n Proto, jacek.m...@gmail.com
Hi Kenton,

Thank you for your reply! But I have further difficulties. I successfully create and load builder:
In my class:

protected: ::capnp::MallocMessageBuilder mBuilder;

Then I Try to get random access to the fields (I need it for my custom QAbstractItemModel).
In order that I use following code:
QModelIndex hCapnpModel::index(int row, int column, const QModelIndex & parent) const { if((column < 0) || (column >= 30)) return QModelIndex(); capnp::SegmentArrayMessageReader lReader(mBuilder.getSegmentsForOutput()); //capnp::SegmentArrayMessageReader lReader(((::capnp::MessageBuilder&)mBuilder).getSegmentsForOutput()); DokmsList::Reader lRoot = lReader.getRoot<DokmsList>(); ::capnp::List< ::Dokms::Dokm>::Reader lList = lRoot.getDokmsList(); if((row < 0) || (row >= lList.size())) return QModelIndex(); return createIndex(row, column); } In this function I need just number of rows, but in different function (data()) I access
to the rows. So I really need some solution of the problem.
Problem occurs in line:
capnp::SegmentArrayMessageReader lReader(mBuilder.getSegmentsForOutput());

I got following error from VC++:

..\hqpanelCapnp\hcapnpmodel.cpp(141): error C2662: 'kj::ArrayPtr<const kj::ArrayPtr<const capnp::word>> capnp::MessageBuilder::getSegmentsForOutput(void)': cannot convert 'this' pointer from 'const capnp::MallocMessageBuilder' to 'capnp::MessageBuilder &'

..\hqpanelCapnp\hcapnpmodel.cpp(141): note: Conversion loses qualifiers

..\hqpanelCapnp\hcapnpmodel.cpp(141): error C2512: 'capnp::SegmentArrayMessageReader::SegmentArrayMessageReader': no appropriate default constructor available


I think that (for unknown reason) VC++ thinks that mBuilder is const object so it is 
different from
capnp::MessageBuilder.

Now I am wonder how to fix it?


best regards

Jacek

Kenton Varda

unread,
Feb 3, 2017, 5:39:29 PM2/3/17
to jacek.m...@gmail.com, Cap'n Proto
Hi Jacek,

It looks like `mBuilder` is a member of `hCapnpModel`, and the `index()` method is declared `const`, therefore all its members are const within the method body. You'll either need to declare `mBuilder` as `mutable` or remove `const` from `index()`.

-Kenton

--
Reply all
Reply to author
Forward
0 new messages