mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-14 23:07:58 +02:00
Added Boost.StringRef support.
This commit is contained in:
@@ -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
|
||||
|
@@ -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.
|
||||
|
95
include/msgpack/adaptor/boost/string_ref.hpp
Normal file
95
include/msgpack/adaptor/boost/string_ref.hpp
Normal file
@@ -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 <boost/version.hpp>
|
||||
#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 <boost/utility/string_ref.hpp>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
/// @cond
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
/// @endcond
|
||||
|
||||
namespace adaptor {
|
||||
|
||||
template <>
|
||||
struct convert<boost::string_ref> {
|
||||
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<boost::string_ref> {
|
||||
template <typename Stream>
|
||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& 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<boost::string_ref> {
|
||||
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<boost::string_ref> {
|
||||
void operator()(msgpack::object::with_zone& o, const boost::string_ref& v) const {
|
||||
static_cast<msgpack::object&>(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
|
@@ -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)
|
||||
|
@@ -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 \
|
||||
|
@@ -35,6 +35,7 @@ LIST (APPEND check_PROGRAMS
|
||||
IF (MSGPACK_BOOST)
|
||||
LIST (APPEND check_PROGRAMS
|
||||
boost_optional.cpp
|
||||
boost_string_ref.cpp
|
||||
)
|
||||
ENDIF ()
|
||||
|
||||
|
@@ -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
|
||||
|
47
test/boost_string_ref.cpp
Normal file
47
test/boost_string_ref.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <sstream>
|
||||
#include <iterator>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#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<boost::string_ref>();
|
||||
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<boost::string_ref>();
|
||||
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<boost::string_ref>();
|
||||
EXPECT_TRUE(val1 == val2);
|
||||
}
|
||||
|
||||
#endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53
|
||||
#endif // defined(MSGPACK_USE_BOOST)
|
Reference in New Issue
Block a user