Hi zosiasmail,
On Mon, Dec 11, 2017 at 8:43 AM, <[email protected]> wrote:
> std::vector<unsigned char> bytes(1024);
> kj::ArrayPtr<capnp::word> words(reinterpret_cast<const
> capnp::byte*>(bytes.data()), bytes.size() / sizeof(capnp::word));
>
This code will work if you change the reinterpret_cast to:
reinterpret_cast<capnp::word*>(bytes.data())
That is, you are casting to the wrong type (and wrong constness).
However, there's a deeper problem this doesn't solve, which is ensuring
that your buffer is aligned. There's no guarantee that the bytes in a
vector are aligned to a word boundary. Since Cap'n Proto doesn't have a
separate decoding step, it's necessary that the message be properly-aligned
for direct access of types up to 64 bits.
The trick is to allocate your backing buffer as words in the first place:
std::vector<capnp::word> words(128);
Now you can read into this vector like:
read(fd, words.begin(), words.size() * sizeof(capnp::word))
Better yet, don't use std::vector; use kj::Array all the way through:
auto words = kj::heapArray<capnp::word>(128);
ssize_t n = read(fd, words.begin(), words.size() * sizeof(capnp::word));
// TODO: check errors, etc.
auto messageWords = words.slice(0, n / sizeof(capnp::word));
If you are using an I/O library that insists on giving you strictly bytes
with no alignment guarantee, then you might have a problem. You may be
forced to make a copy in this case.
-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 [email protected].
Visit this group at https://groups.google.com/group/capnproto.