diff --git a/example/cpp03/class_intrusive.cpp b/example/cpp03/class_intrusive.cpp index a5bb0819..64a3956e 100644 --- a/example/cpp03/class_intrusive.cpp +++ b/example/cpp03/class_intrusive.cpp @@ -54,20 +54,30 @@ void print(std::string const& buf) { << (static_cast(*it) & 0xff) << ' '; } - std::cout << std::endl; + std::cout << std::dec << std::endl; } #include int main() { - my_class my("John Smith", 42); - std::stringstream ss; - msgpack::pack(ss, my); + { // pack, unpack + my_class my("John Smith", 42); + std::stringstream ss; + msgpack::pack(ss, my); - print(ss.str()); + print(ss.str()); - msgpack::unpacked unp; - msgpack::unpack(unp, ss.str().data(), ss.str().size()); - msgpack::object obj = unp.get(); - assert(obj.as() == my); + msgpack::unpacked unp; + msgpack::unpack(unp, ss.str().data(), ss.str().size()); + msgpack::object obj = unp.get(); + std::cout << obj << std::endl; + assert(obj.as() == my); + } + { // create object with zone + my_class my("John Smith", 42); + msgpack::zone z; + msgpack::object obj(my, z); + std::cout << obj << std::endl; + assert(obj.as() == my); + } } diff --git a/example/cpp03/class_non_intrusive.cpp b/example/cpp03/class_non_intrusive.cpp index 57f10890..1168b484 100644 --- a/example/cpp03/class_non_intrusive.cpp +++ b/example/cpp03/class_non_intrusive.cpp @@ -37,6 +37,8 @@ object const& operator>> (msgpack::object const& o, my_class& v); template packer& operator<< (msgpack::packer& o, my_class const& v); +void operator<< (msgpack::object::with_zone& o, my_class const& v); + } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) } // namespace msgpack @@ -87,6 +89,14 @@ inline packer& operator<< (msgpack::packer& o, my_class const& v return o; } +inline void operator<< (msgpack::object::with_zone& o, my_class const& v) { + o.type = type::ARRAY; + o.via.array.size = 2; + o.via.array.ptr = static_cast(o.zone.allocate_align(sizeof(object) * o.via.array.size)); + o.via.array.ptr[0] = msgpack::object(v.get_name(), o.zone); + o.via.array.ptr[1] = msgpack::object(v.get_age(), o.zone); +} + } // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) } // namespace msgpack @@ -102,18 +112,28 @@ void print(std::string const& buf) { << (static_cast(*it) & 0xff) << ' '; } - std::cout << std::endl; + std::cout << std::dec << std::endl; } int main() { - my_class my("John Smith", 42); - std::stringstream ss; - msgpack::pack(ss, my); + { // pack, unpack + my_class my("John Smith", 42); + std::stringstream ss; + msgpack::pack(ss, my); - print(ss.str()); + print(ss.str()); - msgpack::unpacked unp; - msgpack::unpack(unp, ss.str().data(), ss.str().size()); - msgpack::object obj = unp.get(); - assert(obj.as() == my); + msgpack::unpacked unp; + msgpack::unpack(unp, ss.str().data(), ss.str().size()); + msgpack::object obj = unp.get(); + std::cout << obj << std::endl; + assert(obj.as() == my); + } + { // create object with zone + my_class my("John Smith", 42); + msgpack::zone z; + msgpack::object obj(my, z); + std::cout << obj << std::endl; + assert(obj.as() == my); + } } diff --git a/example/cpp03/enum.cpp b/example/cpp03/enum.cpp new file mode 100644 index 00000000..c87dd516 --- /dev/null +++ b/example/cpp03/enum.cpp @@ -0,0 +1,68 @@ +// 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 +#include +#include +#include + +enum my_enum { + elem1, + elem2, + elem3 +}; + +MSGPACK_ADD_ENUM(my_enum); + +#include + +int main(void) +{ + { // pack, unpack + std::stringstream sbuf; + msgpack::pack(sbuf, elem1); + msgpack::pack(sbuf, elem2); + my_enum e3 = elem3; + msgpack::pack(sbuf, e3); + + msgpack::unpacked result; + std::size_t off = 0; + + msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off); + std::cout << result.get().as() << std::endl; + assert(result.get().as() == elem1); + + msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off); + std::cout << result.get().as() << std::endl; + assert(result.get().as() == elem2); + + msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off); + std::cout << result.get().as() << std::endl; + assert(result.get().as() == elem3); + } + { // create object without zone + msgpack::object obj(elem2); + std::cout << obj.as() << std::endl; + assert(obj.as() == elem2); + } + { // create object with zone + msgpack::zone z; + msgpack::object objz(elem3, z); + std::cout << objz.as() << std::endl; + assert(objz.as() == elem3); + } +}