Skip to content

v2_0_cpp_variant

Takatoshi Kondo edited this page Aug 25, 2017 · 5 revisions

variant type

What is this?

msgpack-c doesn't have boost::variant adaptor. But msgpack::type::variant can contain any msgpack types. It is implemented using boost::variant. There are two variant types. One is value based msgpack::type::variant, the other is reference based msgpack::type::variant_ref. When you use msgpack::type::variant, you need to define MSGPACK_USE_BOOST macro.

Type mapping

In common types msgpack::type::variant and msgpack::type::variant_ref

msgpack::type variant type
NIL msgpack::type::nil_t
BOOLEAN bool
POSITIVE_INTEGER uint64_t
NEGATIVE_INTEGER int64_t
FLOAT double

msgpack::type::variant

msgpack::type variant type
STR std::string
BIN std::vector
EXT msgpack::type::ext
ARRAY std::vector<msgpack::type::variant>
MAP std::multimap<msgpack::type::variant, msgpack::type::variant>

msgpack::type::variant_ref

msgpack::type variant type
STR boost::string_ref
BIN msgpack::type::raw_ref
EXT msgpack::type::ext_ref
ARRAY std::vector<msgpack::type::variant_ref>
MAP std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>

Usecase

msgpack-c has a variant type msgpack::object. However, it is difficult to modify. For example, you can't insert/delete ARRAY, MAP items. You can't change STR, BIN, EXT fields. You can do that using msgpack::type::variant. So, you can use it when you want to treat unpredictable msgpack data structures and need to modify them.

Example