2121#include < sstream>
2222#include < cassert>
2323
24- // msgpack.hpp should be included at after user declarations
25- #include < msgpack_fwd.hpp>
26-
27-
28- // declarations
29- class my_class ;
30-
31- namespace msgpack {
32-
33- MSGPACK_API_VERSION_NAMESPACE (MSGPACK_DEFAULT_API_NS) {
34-
35- object const & operator >> (msgpack::object const & o, my_class& v);
36-
37- template <typename Stream>
38- packer<Stream>& operator << (msgpack::packer<Stream>& o, my_class const & v);
39-
40- void operator << (msgpack::object::with_zone& o, my_class const & v);
41-
42- } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
43- } // namespace msgpack
44-
4524#include < msgpack.hpp>
4625
4726class my_class {
@@ -63,40 +42,48 @@ class my_class {
6342 int age_;
6443};
6544
66-
67- // definitions
68-
45+ // User defined class template specialization
6946namespace msgpack {
70-
7147MSGPACK_API_VERSION_NAMESPACE (MSGPACK_DEFAULT_API_NS) {
48+ namespace adaptor {
49+
50+ template <>
51+ struct convert <my_class> {
52+ msgpack::object const & operator ()(msgpack::object const & o, my_class& v) const {
53+ if (o.type != msgpack::type::ARRAY) throw msgpack::type_error ();
54+ if (o.via .array .size != 2 ) throw msgpack::type_error ();
55+ v = my_class (
56+ o.via .array .ptr [0 ].as <std::string>(),
57+ o.via .array .ptr [1 ].as <int >());
58+ return o;
59+ }
60+ };
7261
73- inline object const & operator >> (msgpack::object const & o, my_class& v) {
74- if (o.type != msgpack::type::ARRAY) throw msgpack::type_error ();
75- if (o.via .array .size != 2 ) throw msgpack::type_error ();
76- v = my_class (
77- o.via .array .ptr [0 ].as <std::string>(),
78- o.via .array .ptr [1 ].as <int >());
79- return o;
80- }
81-
82-
83- template <typename Stream>
84- inline packer<Stream>& operator << (msgpack::packer<Stream>& o, my_class const & v) {
85- // packing member variables as an array.
86- o.pack_array (2 );
87- o.pack (v.get_name ());
88- o.pack (v.get_age ());
89- return o;
90- }
62+ template <>
63+ struct pack <my_class> {
64+ template <typename Stream>
65+ packer<Stream>& operator ()(msgpack::packer<Stream>& o, my_class const & v) const {
66+ // packing member variables as an array.
67+ o.pack_array (2 );
68+ o.pack (v.get_name ());
69+ o.pack (v.get_age ());
70+ return o;
71+ }
72+ };
9173
92- inline void operator << (msgpack::object::with_zone& o, my_class const & v) {
93- o.type = type::ARRAY;
94- o.via .array .size = 2 ;
95- o.via .array .ptr = static_cast <object*>(o.zone .allocate_align (sizeof (object) * o.via .array .size ));
96- o.via .array .ptr [0 ] = msgpack::object (v.get_name (), o.zone );
97- o.via .array .ptr [1 ] = msgpack::object (v.get_age (), o.zone );
98- }
74+ template <>
75+ struct object_with_zone <my_class> {
76+ void operator ()(msgpack::object::with_zone& o, my_class const & v) const {
77+ o.type = type::ARRAY;
78+ o.via .array .size = 2 ;
79+ o.via .array .ptr = static_cast <msgpack::object*>(
80+ o.zone .allocate_align (sizeof (msgpack::object) * o.via .array .size ));
81+ o.via .array .ptr [0 ] = msgpack::object (v.get_name (), o.zone );
82+ o.via .array .ptr [1 ] = msgpack::object (v.get_age (), o.zone );
83+ }
84+ };
9985
86+ } // namespace adaptor
10087} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
10188} // namespace msgpack
10289
0 commit comments