Variant type without boost - also, community?
Hello,
I'm hoping to send/receive variant types (int, double, string, bool). I've seen the boost variant code but I am hoping to do it without boost (I don't understand it all yet, but may try to base my solution .
I initially thought that sending a map of msgpack objects would work, but they appear unable to store strings without a zone (which makes having independent variant object tricky). I tried sending a vector of object_handles (since they include zones) but that did not appear to work either.
If I may ask, is there something obvious I am missing? Also, is there any sort of community site associated with msgpack-c (forum, mailing list, etc?).
Thanks, Charlie West
Hi @charlesrwest ,
If you can use the Boost, use msgpack::type::variant or msgpack::type::variant_ref. The former can contain variant types without external storage. The latter requires keeping the lifetime of the buffer that is referenced by variant_ref.
If you can't use the Boost, use msgpack::object. It requires msgpack::zone. So you need to manage the zone. You can create msgpack::object from adapted C++ types. See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_object#conversion.
If you want to manage the zone automatically, you can use the following constructor:
https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L53
See the following example:
http://melpon.org/wandbox/permlink/RGNZ139p0iACKuyr
msgpack::zone is automatically deleted via unique_ptr.
If you don't want to use Boost and msgpack::zone, there is no solution, so far.
If I may ask, is there something obvious I am missing? Also, is there any sort of community site associated with msgpack-c (forum, mailing list, etc?).
No, here is the appropriate place to ask :)
Thanks.
I've made a preliminary implementation of a simple (non-recursive) variant class and attempted to make adaptors for it. However, I am still getting message pack errors related to the adaptors (I think). If I may ask, would you be willing to tell me where I am going wrong?
Link: http://melpon.org/wandbox/permlink/w1SAP3r1CaqYE4Ux
Thanks again, Charlie
I fixed your code. http://melpon.org/wandbox/permlink/VsLLbyFOzQWHWMws
See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_adaptor "Example:"
msgpack::pack(inputStream, (int64_t) inputVariant);
should be
inputStream.pack( (int64_t) inputVariant);
By the way, the variable inputStream is a kind of output stream.