Question

I have seen references to 'zone' in the MsgPack C headers, but can find no documentation on what it is or what it's for. What is it? Furthermore, where's the function-by-function documentation for the C API?

Was it helpful?

Solution

msgpack_zone is an internal structure used for memory management & lifecycle at unpacking time. I would say you will never have to interact with it if you use the standard, high-level interface for unpacking or the alternative streaming version.

To my knowledge, there is no detailed documentation: instead you should refer to the test suite that provides convenient code samples to achieve the common tasks, e.g. see pack_unpack_c.cc and streaming_c.cc.

OTHER TIPS

From what I could gather, it is a move-only type that stores the actual data of a msgpack::object. It very well might intended to be an implementation detail, but it actually leaks into users' code sometimes. For example, any time you want to capture a msgpack::object in a lambda, you have to capture the msgpack::zone object as well. Sometimes you can't use move capture (e.g. asio handlers in some cases will only take copyable handlers, or your compiler doesn't support the feature). To work around this, you can:

msgpack::unpacked r;
while (pac_.next(&r)) {
   auto msg = result.get();
   io_->post([this, msg, z = std::shared_ptr<msgpack::zone>(r.zone().release())]() {
                // msg is valid here            
             }));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top