Skip to content

v1_1_cpp_object

Peter Tissen edited this page Aug 18, 2016 · 3 revisions

msgpack::object

What is msgpack::object

msgpack::object is a kind of variant type. The internal type of the msgpack::object is corresponding to the msgpack format.

If the type of msgpack::object is array or map, the object has children. So msgpack::object is a composite structure.

Object

When the type of msgpack::object is str, bin, or ext, the msgpack::object might have a reference of external memory.

ObjectMemory

When you use msgpack::unpacker, msgpack::unpacked manages all allocated memories. Treat msgpack::unpacked as a smart pointer.

When you use msgpack::unpack() function without unpack_referenc_func, msgpack::unpacked manages all allocated memories. If you give a custom unpack_referenc_func that returns true, you need to keep the lifetime of the data that you passed to msgpack::unpack() while the unpacked msgpack::unpacked exists.

Conversion

Conversion

When you unpack msgpack format data, you get msgpack::object from msgpack::unpacked. Then you get various types values from msgpack::object. If you want to convert to custom types from msgpack::object, see adaptor.

You can create a msgpack::object from a various type value using the following constructor. If you want to create a msgpack::object from a custom type, see https://github.com/redboltz/msgpack-c/wiki/v1_1_cpp_adaptor#object and https://github.com/redboltz/msgpack-c/wiki/v1_1_cpp_adaptor#object_with_zone

    template <typename T>
    object(const T& v, zone& z);

You need pass a zone. When the object contains array and/or map, child objects are allocated in the zone. A value that has the type corresponding to str and bin, the contents of the value is copied to the zone with one exception. The exception is msgpack::type::raw_ref. When you call msgpack::object constructor with raw_ref as follows:

msgpack::object obj(msgpack::type::raw_ref(data, size), zone);

The data is NOT copied to the zone.