-
Notifications
You must be signed in to change notification settings - Fork 883
v1_0_cpp_adaptor
When you want to pack to msgpack::object from a various type object, you need an adaptor. Converting to msgpack::object from a various type object and vice versa, it requires an adaptor too.
See conversion.
msgpack-c provides predefined adaptors for C++ primitive types and standard libraries.
C++ type | msgpack::object type |
---|---|
bool | bool |
char* | str |
std::deque | array |
char | positive/negative integer |
signed ints *1 | positive/negative integer |
unsigned ints *2 | positive integer |
std::list | array |
std::map | array |
std::pair | array |
std::set | array |
std::string | str |
std::vector | array |
std::vector<char> | bin |
C++11 type | msgpack::object type |
---|---|
std::array | array |
std::array<char> | bin |
std::forward_list | array |
std::tuple | array |
std::array | array |
std::unordered_map | array |
std::unordered_set | array |
*1 signed ints signed char, signed short, signed int, signed long, signed long long
*2 unsigned ints unsigned char, unsigned short, unsigned int, signed long, signed long long
msgpack::object type is defined as follows: https://github.com/msgpack/msgpack/blob/master/spec.md
These adaptors are defined in the following directory: https://github.com/msgpack/msgpack-c/tree/cpp-1.0.1/include/msgpack/adaptor
When you want to adapt your class to msgpack, use MSGPACK_DEFINE macro.
#include <msgpack_fwd.hpp>
struct your_class {
int a;
std::string b;
MSGPACK_DEFINE(a, b);
};
#include <msgpack.hpp>
// ...
The macro MSGPACK_DEFINE provides packing, converting to msgpack object with zone, and converting to your_class from msgpack::object functionalities. your_class is packed/converted as msgpack array.
https://github.com/msgpack/msgpack-c/blob/cpp-1.0.1/example/cpp03/class_intrusive.cpp
https://github.com/msgpack/msgpack-c/blob/cpp-1.0.1/example/cpp03/class_non_intrusive.cpp
When you want to adapt enum or enum class to msgpack, use MSGPACK_ADD_ENUM macro.
#include <msgpack_fwd.hpp>
enum your_enum {
elem1,
elem2
};
MSGPACK_ADD_ENUM(your_enum);
enum class your_enum_class {
elem1,
elem2
};
MSGPACK_ADD_ENUM(your_enum_class);
#include <msgpack.hpp>
// ...
You need to use MSGPACK_ADD_ENUM in the global namespace. And you need to use MSGPACK_ADD_ENUM before include msgpack.hpp. You might need to re-struct your source code.
The macro MSGPACK_DEFINE provides packing, converting to msgpack object with or without zone, and converting to your_enum from msgpack::object functionalities. your_enum is packed/converted as msgpack positive/negative integer.
See an example.
-
Home
- Q&A
- v2.0.x or later
- v1.1.x - v1.4.x
- v1.0.x