Skip to content

Commit e25ecc5

Browse files
committed
Merge pull request #310 from redboltz/add_examples
Added an example of the non default constructible class.
2 parents 1a97e76 + f1504d8 commit e25ecc5

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

example/cpp03/class_intrusive.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,50 @@
2121
#include <sstream>
2222
#include <cassert>
2323

24+
#if 0 // When you want to adapt map instead of array, you can enable these macro definition.
25+
#define MSGPACK_USE_DEFINE_MAP
26+
#define MSGPACK_USE_BOOST
27+
#endif
28+
2429
#include <msgpack.hpp>
2530

31+
struct my_base1 {
32+
int a;
33+
MSGPACK_DEFINE(a);
34+
};
35+
inline bool operator==(my_base1 const& lhs, my_base1 const& rhs) {
36+
return lhs.a == rhs.a;
37+
}
2638

27-
class my_class {
39+
struct my_base2 {
40+
std::string b;
41+
std::string c;
42+
MSGPACK_DEFINE(b, c);
43+
};
44+
inline bool operator==(my_base2 const& lhs, my_base2 const& rhs) {
45+
return lhs.b == rhs.b && lhs.c == rhs.c;
46+
}
47+
48+
class my_class : public my_base1, private my_base2 {
2849
public:
2950
my_class() {} // When you want to convert from msgpack::object to my_class,
3051
// my_class should be default constractible.
3152
my_class(std::string const& name, int age):name_(name), age_(age) {}
32-
53+
void set_b(std::string const& str) { b = str; }
54+
void set_c(std::string const& str) { c = str; }
3355
friend bool operator==(my_class const& lhs, my_class const& rhs) {
34-
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
56+
return
57+
static_cast<my_base1 const&>(lhs) == static_cast<my_base1 const&>(rhs) &&
58+
static_cast<my_base2 const&>(lhs) == static_cast<my_base2 const&>(rhs) &&
59+
lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
3560
}
3661

3762
private:
3863
std::string name_;
3964
int age_;
4065

4166
public:
42-
MSGPACK_DEFINE(name_, age_);
67+
MSGPACK_DEFINE(name_, age_, MSGPACK_BASE(my_base1), MSGPACK_BASE(my_base2));
4368
};
4469

4570
void print(std::string const& buf) {
@@ -59,6 +84,9 @@ void print(std::string const& buf) {
5984
int main() {
6085
{ // pack, unpack
6186
my_class my("John Smith", 42);
87+
my.a = 123;
88+
my.set_b("ABC");
89+
my.set_c("DEF");
6290
std::stringstream ss;
6391
msgpack::pack(ss, my);
6492

@@ -72,6 +100,8 @@ int main() {
72100
}
73101
{ // create object with zone
74102
my_class my("John Smith", 42);
103+
my.set_b("ABC");
104+
my.set_c("DEF");
75105
msgpack::zone z;
76106
msgpack::object obj(my, z);
77107
std::cout << obj << std::endl;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// MessagePack for C++ example
2+
//
3+
// Copyright (C) 2015 KONDO Takatoshi
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
18+
#include <cassert>
19+
#include <memory>
20+
21+
#include <msgpack.hpp>
22+
23+
struct my {
24+
my() = delete;
25+
26+
// target class should be either copyable or movable (or both).
27+
my(my const&) = delete;
28+
my(my&&) = default;
29+
30+
my(int a):a(a) {}
31+
int a;
32+
MSGPACK_DEFINE(a);
33+
};
34+
35+
namespace msgpack {
36+
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
37+
namespace adaptor {
38+
39+
template<>
40+
struct as<my> {
41+
my operator()(msgpack::object const& o) const {
42+
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
43+
if (o.via.array.size != 1) throw msgpack::type_error();
44+
return my(o.via.array.ptr[0].as<int>());
45+
}
46+
};
47+
48+
} // namespace adaptor
49+
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
50+
} // namespace msgpack
51+
52+
int main() {
53+
my m1(42);
54+
msgpack::zone z;
55+
msgpack::object obj(m1, z);
56+
auto m2 = obj.as<my>();
57+
assert(m1.a == m2.a);
58+
}

0 commit comments

Comments
 (0)