c++capnproto

How to deserialize a std::string to a concrete message in Cap'n Proto C++?


I need to decode a message from a std::string object.

I can see how to do this in Java by using a ByteBuffer, but I failed to find a simple C++ equivalent.

More precisely, how to implement this?

const std::string serialized_data = ...; // Source
SomeDataType data = ???; // How to convert from serialized_data to data?

Solution

  • You can write code like this:

    std::string serialized_data = ...;
    kj::ArrayPtr<const capnp::word> words(
        reinterpret_cast<const capnp::word*>(serialized_data.begin()),
        serialized_data.size() / sizeof(capnp::word));
    capnp::FlatArrayMessageReader reader(words);
    

    Note that this assumes that the backing buffer of an std::string is always word-aligned. I think this is true, but I am not sure. If it is not aligned, you will get exceptions saying so. In this case it may be necessary to copy the data into an aligned buffer:

    auto words = kj::heapArray<capnp::word>(
        serialized_data.size() / sizeof(capnp::word));
    memcpy(words.begin(), serialized_data.begin(), words.asBytes().size());
    capnp::FlatAraryMessageReader reader(words);
    

    Note that while std::string is technically able to store arbitrary bytes, it is not a particularly good data structure for this purpose, since it is optimized for text. For example, it may re-allocate and copy your buffer in order to add a NUL terminator to support .c_str(). I would recommend using a more data-oriented type for byte arrays.