From 70fbae3c5f3f9a3d8539934be055e4e06c2bb280 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sat, 2 May 2015 10:51:34 +0900 Subject: [PATCH] Added Boost.StringRef support. --- CMakeLists.txt | 1 + include/msgpack/adaptor/boost/optional.hpp | 2 +- include/msgpack/adaptor/boost/string_ref.hpp | 95 ++++++++++++++++++++ include/msgpack/type.hpp | 1 + src/Makefile.am | 1 + test/CMakeLists.txt | 1 + test/Makefile.am | 5 +- test/boost_string_ref.cpp | 47 ++++++++++ 8 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 include/msgpack/adaptor/boost/string_ref.hpp create mode 100644 test/boost_string_ref.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e081b63f..8ac75258 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,7 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/adaptor/adaptor_base.hpp include/msgpack/adaptor/bool.hpp include/msgpack/adaptor/boost/optional.hpp + include/msgpack/adaptor/boost/string_ref.hpp include/msgpack/adaptor/char_ptr.hpp include/msgpack/adaptor/check_container_size.hpp include/msgpack/adaptor/cpp11/array.hpp diff --git a/include/msgpack/adaptor/boost/optional.hpp b/include/msgpack/adaptor/boost/optional.hpp index 2a3b4899..9a3dbb83 100644 --- a/include/msgpack/adaptor/boost/optional.hpp +++ b/include/msgpack/adaptor/boost/optional.hpp @@ -1,7 +1,7 @@ // // MessagePack for C++ static resolution routine // -// Copyright (C) 2008-2009 FURUHASHI Sadayuki +// 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. diff --git a/include/msgpack/adaptor/boost/string_ref.hpp b/include/msgpack/adaptor/boost/string_ref.hpp new file mode 100644 index 00000000..68182182 --- /dev/null +++ b/include/msgpack/adaptor/boost/string_ref.hpp @@ -0,0 +1,95 @@ +// +// MessagePack for C++ static resolution routine +// +// 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. +// +#ifndef MSGPACK_TYPE_BOOST_STRING_REF_HPP +#define MSGPACK_TYPE_BOOST_STRING_REF_HPP + +#include +#if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + +#include "msgpack/versioning.hpp" +#include "msgpack/adaptor/adaptor_base.hpp" +#include "msgpack/adaptor/check_container_size.hpp" + +#include + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v1) { +/// @endcond + +namespace adaptor { + +template <> +struct convert { + msgpack::object const& operator()(msgpack::object const& o, boost::string_ref& v) const { + switch (o.type) { + case msgpack::type::BIN: + v = boost::string_ref(o.via.bin.ptr, o.via.bin.size); + break; + case msgpack::type::STR: + v = boost::string_ref(o.via.str.ptr, o.via.str.size); + break; + default: + throw msgpack::type_error(); + break; + } + return o; + } +}; + +template <> +struct pack { + template + msgpack::packer& operator()(msgpack::packer& o, const boost::string_ref& v) const { + uint32_t size = checked_get_container_size(v.size()); + o.pack_str(size); + o.pack_str_body(v.data(), size); + return o; + } +}; + +template <> +struct object { + void operator()(msgpack::object& o, const boost::string_ref& v) const { + uint32_t size = checked_get_container_size(v.size()); + o.type = msgpack::type::STR; + o.via.str.ptr = v.data(); + o.via.str.size = size; + } +}; + +template <> +struct object_with_zone { + void operator()(msgpack::object::with_zone& o, const boost::string_ref& v) const { + static_cast(o) << v; + } +}; + + +} // namespace adaptor + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v1) +/// @endcond + +} // namespace msgpack + +#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + +#endif // MSGPACK_TYPE_BOOST_STRING_REF_HPP diff --git a/include/msgpack/type.hpp b/include/msgpack/type.hpp index ffd09bbd..2300a0f4 100644 --- a/include/msgpack/type.hpp +++ b/include/msgpack/type.hpp @@ -37,5 +37,6 @@ #if defined(MSGPACK_USE_BOOST) #include "adaptor/boost/optional.hpp" +#include "adaptor/boost/string_ref.hpp" #endif // defined(MSGPACK_USE_BOOST) diff --git a/src/Makefile.am b/src/Makefile.am index 81a64b22..b5dafe60 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -57,6 +57,7 @@ nobase_include_HEADERS += \ ../include/msgpack/adaptor/adaptor_base.hpp \ ../include/msgpack/adaptor/bool.hpp \ ../include/msgpack/adaptor/boost/optional.hpp \ + ../include/msgpack/adaptor/boost/string_ref.hpp \ ../include/msgpack/adaptor/char_ptr.hpp \ ../include/msgpack/adaptor/check_container_size.hpp \ ../include/msgpack/adaptor/cpp11/array.hpp \ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 04e673d8..8b2f90cf 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -35,6 +35,7 @@ LIST (APPEND check_PROGRAMS IF (MSGPACK_BOOST) LIST (APPEND check_PROGRAMS boost_optional.cpp + boost_string_ref.cpp ) ENDIF () diff --git a/test/Makefile.am b/test/Makefile.am index 5272e754..5a8a2872 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -28,7 +28,8 @@ check_PROGRAMS = \ limit \ json \ iterator_cpp11 \ - boost_optional + boost_optional \ + boost_string_ref TESTS = $(check_PROGRAMS) @@ -85,4 +86,6 @@ iterator_cpp11_SOURCES = iterator_cpp11.cpp boost_optional_SOURCES = boost_optional.cpp +boost_string_ref_SOURCES = boost_string_ref.cpp + EXTRA_DIST = cases.mpac cases_compact.mpac diff --git a/test/boost_string_ref.cpp b/test/boost_string_ref.cpp new file mode 100644 index 00000000..a1ed87ff --- /dev/null +++ b/test/boost_string_ref.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#if defined(MSGPACK_USE_BOOST) +#if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 + +TEST(MSGPACK_BOOST, pack_convert_string_ref) +{ + std::stringstream ss; + std::string s = "ABC"; + boost::string_ref val1(s); + + msgpack::pack(ss, val1); + + msgpack::unpacked ret; + msgpack::unpack(ret, ss.str().data(), ss.str().size()); + boost::string_ref val2 = ret.get().as(); + EXPECT_TRUE(val1 == val2); +} + +TEST(MSGPACK_BOOST, object_strinf_ref) +{ + std::string s = "ABC"; + boost::string_ref val1(s); + msgpack::object obj(val1); + boost::string_ref val2 = obj.as(); + EXPECT_TRUE(val1 == val2); +} + +TEST(MSGPACK_BOOST, object_with_zone_string_ref) +{ + msgpack::zone z; + std::string s = "ABC"; + boost::string_ref val1(s); + msgpack::object obj(val1, z); + boost::string_ref val2 = obj.as(); + EXPECT_TRUE(val1 == val2); +} + +#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 +#endif // defined(MSGPACK_USE_BOOST)