Added an example of the non default constructible class.

Refined an intrusive example. It includes base class serialization. We can switch map based and array based serialization.
This commit is contained in:
Takatoshi Kondo 2015-07-06 15:50:03 +09:00
parent 1a97e761fb
commit f1504d851a
2 changed files with 92 additions and 4 deletions

View File

@ -21,17 +21,42 @@
#include <sstream> #include <sstream>
#include <cassert> #include <cassert>
#if 0 // When you want to adapt map instead of array, you can enable these macro definition.
#define MSGPACK_USE_DEFINE_MAP
#define MSGPACK_USE_BOOST
#endif
#include <msgpack.hpp> #include <msgpack.hpp>
struct my_base1 {
int a;
MSGPACK_DEFINE(a);
};
inline bool operator==(my_base1 const& lhs, my_base1 const& rhs) {
return lhs.a == rhs.a;
}
class my_class { struct my_base2 {
std::string b;
std::string c;
MSGPACK_DEFINE(b, c);
};
inline bool operator==(my_base2 const& lhs, my_base2 const& rhs) {
return lhs.b == rhs.b && lhs.c == rhs.c;
}
class my_class : public my_base1, private my_base2 {
public: public:
my_class() {} // When you want to convert from msgpack::object to my_class, my_class() {} // When you want to convert from msgpack::object to my_class,
// my_class should be default constractible. // my_class should be default constractible.
my_class(std::string const& name, int age):name_(name), age_(age) {} my_class(std::string const& name, int age):name_(name), age_(age) {}
void set_b(std::string const& str) { b = str; }
void set_c(std::string const& str) { c = str; }
friend bool operator==(my_class const& lhs, my_class const& rhs) { friend bool operator==(my_class const& lhs, my_class const& rhs) {
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_; return
static_cast<my_base1 const&>(lhs) == static_cast<my_base1 const&>(rhs) &&
static_cast<my_base2 const&>(lhs) == static_cast<my_base2 const&>(rhs) &&
lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
} }
private: private:
@ -39,7 +64,7 @@ private:
int age_; int age_;
public: public:
MSGPACK_DEFINE(name_, age_); MSGPACK_DEFINE(name_, age_, MSGPACK_BASE(my_base1), MSGPACK_BASE(my_base2));
}; };
void print(std::string const& buf) { void print(std::string const& buf) {
@ -59,6 +84,9 @@ void print(std::string const& buf) {
int main() { int main() {
{ // pack, unpack { // pack, unpack
my_class my("John Smith", 42); my_class my("John Smith", 42);
my.a = 123;
my.set_b("ABC");
my.set_c("DEF");
std::stringstream ss; std::stringstream ss;
msgpack::pack(ss, my); msgpack::pack(ss, my);
@ -72,6 +100,8 @@ int main() {
} }
{ // create object with zone { // create object with zone
my_class my("John Smith", 42); my_class my("John Smith", 42);
my.set_b("ABC");
my.set_c("DEF");
msgpack::zone z; msgpack::zone z;
msgpack::object obj(my, z); msgpack::object obj(my, z);
std::cout << obj << std::endl; std::cout << obj << std::endl;

View File

@ -0,0 +1,58 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <cassert>
#include <memory>
#include <msgpack.hpp>
struct my {
my() = delete;
// target class should be either copyable or movable (or both).
my(my const&) = delete;
my(my&&) = default;
my(int a):a(a) {}
int a;
MSGPACK_DEFINE(a);
};
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template<>
struct as<my> {
my operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 1) throw msgpack::type_error();
return my(o.via.array.ptr[0].as<int>());
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack
int main() {
my m1(42);
msgpack::zone z;
msgpack::object obj(m1, z);
auto m2 = obj.as<my>();
assert(m1.a == m2.a);
}