From 79e7a9810ca74ad737c0df04ee46bce16ca60030 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sat, 1 Nov 2014 18:13:52 +0900 Subject: [PATCH] Moved examples. Added intrusive and non-intrusive versions of packing and unpacking examples. --- example/{ => c}/lib_buffer_unpack.c | 2 +- example/{ => c}/simple.c | 0 example/{ => c}/speed_test_uint32_array.c | 0 example/{ => c}/speed_test_uint64_array.c | 0 example/{ => c}/user_buffer_unpack.c | 0 example/cpp03/class_intrusive.cpp | 53 ++++++++++ example/cpp03/class_non_intrusive.cpp | 96 +++++++++++++++++++ example/{custom.cc => cpp03/custom.cpp} | 0 example/{protocol.cc => cpp03/protocol.cpp} | 0 example/{simple.cc => cpp03/simple.cpp} | 0 .../{speed_test.cc => cpp03/speed_test.cpp} | 0 .../speed_test_nested_array.cpp} | 0 example/{stream.cc => cpp03/stream.cpp} | 0 13 files changed, 150 insertions(+), 1 deletion(-) rename example/{ => c}/lib_buffer_unpack.c (97%) rename example/{ => c}/simple.c (100%) rename example/{ => c}/speed_test_uint32_array.c (100%) rename example/{ => c}/speed_test_uint64_array.c (100%) rename example/{ => c}/user_buffer_unpack.c (100%) create mode 100644 example/cpp03/class_intrusive.cpp create mode 100644 example/cpp03/class_non_intrusive.cpp rename example/{custom.cc => cpp03/custom.cpp} (100%) rename example/{protocol.cc => cpp03/protocol.cpp} (100%) rename example/{simple.cc => cpp03/simple.cpp} (100%) rename example/{speed_test.cc => cpp03/speed_test.cpp} (100%) rename example/{speed_test_nested_array.cc => cpp03/speed_test_nested_array.cpp} (100%) rename example/{stream.cc => cpp03/stream.cpp} (100%) diff --git a/example/lib_buffer_unpack.c b/example/c/lib_buffer_unpack.c similarity index 97% rename from example/lib_buffer_unpack.c rename to example/c/lib_buffer_unpack.c index 6d26c544..dd78b92a 100644 --- a/example/lib_buffer_unpack.c +++ b/example/c/lib_buffer_unpack.c @@ -67,7 +67,7 @@ void unpack(receiver* r) { int recv_count = 0; while (recv_len > 0) { int i = 0; - printf("receive count: %d %d bytes received.:\n", recv_count++, recv_len); + printf("receive count: %d %zd bytes received.:\n", recv_count++, recv_len); ret = msgpack_unpacker_next(unp, &result); while (ret == MSGPACK_UNPACK_SUCCESS) { msgpack_object obj = result.data; diff --git a/example/simple.c b/example/c/simple.c similarity index 100% rename from example/simple.c rename to example/c/simple.c diff --git a/example/speed_test_uint32_array.c b/example/c/speed_test_uint32_array.c similarity index 100% rename from example/speed_test_uint32_array.c rename to example/c/speed_test_uint32_array.c diff --git a/example/speed_test_uint64_array.c b/example/c/speed_test_uint64_array.c similarity index 100% rename from example/speed_test_uint64_array.c rename to example/c/speed_test_uint64_array.c diff --git a/example/user_buffer_unpack.c b/example/c/user_buffer_unpack.c similarity index 100% rename from example/user_buffer_unpack.c rename to example/c/user_buffer_unpack.c diff --git a/example/cpp03/class_intrusive.cpp b/example/cpp03/class_intrusive.cpp new file mode 100644 index 00000000..c27dcd4d --- /dev/null +++ b/example/cpp03/class_intrusive.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +#include + + +class my_class { +public: + my_class() {} // When you want to convert from msgpack::object to my_class, + // my_class should be default constractible. + my_class(std::string const& name, int age):name_(name), age_(age) {} + + friend bool operator==(my_class const& lhs, my_class const& rhs) { + return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_; + } + +private: + std::string name_; + int age_; + +public: + MSGPACK_DEFINE(name_, age_); +}; + +void print(std::string const& buf) { + for (std::string::const_iterator it = buf.begin(), end = buf.end(); + it != end; + ++it) { + std::cout + << std::setw(2) + << std::hex + << std::setfill('0') + << (static_cast(*it) & 0xff) + << ' '; + } + std::cout << std::endl; +} + +int main() { + my_class my("John Smith", 42); + std::stringstream ss; + msgpack::pack(ss, my); + + print(ss.str()); + + msgpack::unpacked unp; + msgpack::unpack(unp, ss.str().data(), ss.str().size()); + msgpack::object obj = unp.get(); + assert(obj.as() == my); +} diff --git a/example/cpp03/class_non_intrusive.cpp b/example/cpp03/class_non_intrusive.cpp new file mode 100644 index 00000000..b3bbb8bd --- /dev/null +++ b/example/cpp03/class_non_intrusive.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +#include + + +// declarations +class my_class; + +namespace msgpack { + +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { + +object const& operator>> (object const& o, my_class& v); + +template +packer& operator<< (packer& o, my_class const& v); + +} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) +} // namespace msgpack + +#include + +class my_class { +public: + my_class() {} // When you want to convert from msgpack::object to my_class, + // my_class should be default constractible. + my_class(std::string const& name, int age):name_(name), age_(age) {} + + // When you want to define a non-intrusive adaptor functions, + // member variables should be public. + std::string name_; + int age_; +}; + +inline bool operator==(my_class const& lhs, my_class const& rhs) { + return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_; +} + +// definitions + +namespace msgpack { + +MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { + +inline object const& operator>> (object const& o, my_class& v) { + if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); + if (o.via.array.size != 2) throw msgpack::type_error(); + o.via.array.ptr[0].convert(v.name_); + o.via.array.ptr[1].convert(v.age_); + return o; +} + + +template +inline packer& operator<< (packer& o, my_class const& v) { + // packing member variables as an array. + o.pack_array(2); + o.pack(v.name_); + o.pack(v.age_); + return o; +} + +} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) +} // namespace msgpack + + +void print(std::string const& buf) { + for (std::string::const_iterator it = buf.begin(), end = buf.end(); + it != end; + ++it) { + std::cout + << std::setw(2) + << std::hex + << std::setfill('0') + << (static_cast(*it) & 0xff) + << ' '; + } + std::cout << std::endl; +} + +int main() { + my_class my("John Smith", 42); + std::stringstream ss; + msgpack::pack(ss, my); + + print(ss.str()); + + msgpack::unpacked unp; + msgpack::unpack(unp, ss.str().data(), ss.str().size()); + msgpack::object obj = unp.get(); + assert(obj.as() == my); +} diff --git a/example/custom.cc b/example/cpp03/custom.cpp similarity index 100% rename from example/custom.cc rename to example/cpp03/custom.cpp diff --git a/example/protocol.cc b/example/cpp03/protocol.cpp similarity index 100% rename from example/protocol.cc rename to example/cpp03/protocol.cpp diff --git a/example/simple.cc b/example/cpp03/simple.cpp similarity index 100% rename from example/simple.cc rename to example/cpp03/simple.cpp diff --git a/example/speed_test.cc b/example/cpp03/speed_test.cpp similarity index 100% rename from example/speed_test.cc rename to example/cpp03/speed_test.cpp diff --git a/example/speed_test_nested_array.cc b/example/cpp03/speed_test_nested_array.cpp similarity index 100% rename from example/speed_test_nested_array.cc rename to example/cpp03/speed_test_nested_array.cpp diff --git a/example/stream.cc b/example/cpp03/stream.cpp similarity index 100% rename from example/stream.cc rename to example/cpp03/stream.cpp