Skip to content

Commit a2c8154

Browse files
committed
Updated examples to support new packing/converting mechanism.
1 parent e8d3c8d commit a2c8154

File tree

4 files changed

+42
-57
lines changed

4 files changed

+42
-57
lines changed

example/cpp03/class_intrusive.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include <sstream>
2222
#include <cassert>
2323

24-
// msgpack.hpp is also ok
25-
#include <msgpack_fwd.hpp>
24+
#include <msgpack.hpp>
2625

2726

2827
class my_class {
@@ -57,8 +56,6 @@ void print(std::string const& buf) {
5756
std::cout << std::dec << std::endl;
5857
}
5958

60-
#include <msgpack.hpp>
61-
6259
int main() {
6360
{ // pack, unpack
6461
my_class my("John Smith", 42);

example/cpp03/class_non_intrusive.cpp

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,6 @@
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

4726
class my_class {
@@ -63,40 +42,48 @@ class my_class {
6342
int age_;
6443
};
6544

66-
67-
// definitions
68-
45+
// User defined class template specialization
6946
namespace msgpack {
70-
7147
MSGPACK_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

example/cpp03/enum.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
// limitations under the License.
1616
//
1717

18-
#include <msgpack_fwd.hpp>
1918
#include <sstream>
2019
#include <iostream>
2120
#include <cassert>
2221

22+
#include <msgpack.hpp>
23+
2324
enum my_enum {
2425
elem1,
2526
elem2,
@@ -28,8 +29,6 @@ enum my_enum {
2829

2930
MSGPACK_ADD_ENUM(my_enum);
3031

31-
#include <msgpack.hpp>
32-
3332
int main(void)
3433
{
3534
{ // pack, unpack

example/cpp03/protocol.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <iostream>
2121
#include <sstream>
2222

23+
// This example uses obsolete APIs
24+
// See protocol_new.cpp
2325
namespace myprotocol {
2426
using namespace msgpack::type;
2527
using msgpack::define;

0 commit comments

Comments
 (0)