From 15aec0493dd466229b334d6ac04349bb0a100bb7 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Tue, 3 Feb 2015 20:40:44 +0900 Subject: [PATCH] Added a C++11 container example. --- example/cpp11/container.cpp | 108 ++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 example/cpp11/container.cpp diff --git a/example/cpp11/container.cpp b/example/cpp11/container.cpp new file mode 100644 index 00000000..d3006309 --- /dev/null +++ b/example/cpp11/container.cpp @@ -0,0 +1,108 @@ +// 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 +#include +#include +#include +#include +#include + +#include + +void array() { + std::array a { 1, 2, 3, 4, 5 }; + std::stringstream ss; + msgpack::pack(ss, a); + + msgpack::unpacked und = msgpack::unpack(ss.str().data(), ss.str().size()); + msgpack::object obj = und.get(); + std::array const& new_a = obj.as>(); + + assert(new_a == a); +} + +void tuple() { + std::tuple t(true, "ABC", 42); + std::stringstream ss; + msgpack::pack(ss, t); + + auto und = msgpack::unpack(ss.str().data(), ss.str().size()); + auto obj = und.get(); + assert(obj.as() == t); +} + +void unordered_map() { + std::unordered_map m { {"ABC", 1}, {"DEF", 3} }; + std::stringstream ss; + msgpack::pack(ss, m); + + auto und = msgpack::unpack(ss.str().data(), ss.str().size()); + assert(und.get().as() == m); +} + +void unordered_set() { + std::unordered_set s { "ABC", "DEF" }; + std::stringstream ss; + msgpack::pack(ss, s); + + assert(msgpack::unpack(ss.str().data(), ss.str().size()).get().as() == s); +} + +void forward_list() { + using type = std::forward_list; + type f { "ABC", "DEF" }; + std::stringstream ss; + msgpack::pack(ss, f); + + assert(msgpack::unpack(ss.str().data(), ss.str().size()).get().as() == f); +} + +void combi() { + std::array a { 1, 2, 3, 4, 5 }; + std::tuple t {true, "ABC", 42}; + std::unordered_map m { {"ABC", 1}, {"DEF", 3} }; + std::unordered_set s { "ABC", "DEF" }; + std::forward_list f { "ABC", "DEF" }; + + std::stringstream ss; + msgpack::pack(ss, a); + msgpack::pack(ss, t); + msgpack::pack(ss, m); + msgpack::pack(ss, s); + msgpack::pack(ss, f); + + std::size_t offset = 0; + assert(msgpack::unpack(ss.str().data(), ss.str().size(), offset).get().as() == a); + assert(msgpack::unpack(ss.str().data(), ss.str().size(), offset).get().as() == t); + assert(msgpack::unpack(ss.str().data(), ss.str().size(), offset).get().as() == m); + assert(msgpack::unpack(ss.str().data(), ss.str().size(), offset).get().as() == s); + assert(msgpack::unpack(ss.str().data(), ss.str().size(), offset).get().as() == f); +} + +int main() { + array(); + tuple(); + unordered_map(); + unordered_set(); + forward_list(); + combi(); +}