Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Boost.StringRef support. #278

Merged
merged 1 commit into from May 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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.
Expand Down
95 changes: 95 additions & 0 deletions 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 <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
1 change: 1 addition & 0 deletions include/msgpack/type.hpp
Expand Up @@ -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)
1 change: 1 addition & 0 deletions src/Makefile.am
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Expand Up @@ -35,6 +35,7 @@ LIST (APPEND check_PROGRAMS
IF (MSGPACK_BOOST)
LIST (APPEND check_PROGRAMS
boost_optional.cpp
boost_string_ref.cpp
)
ENDIF ()

Expand Down
5 changes: 4 additions & 1 deletion test/Makefile.am
Expand Up @@ -28,7 +28,8 @@ check_PROGRAMS = \
limit \
json \
iterator_cpp11 \
boost_optional
boost_optional \
boost_string_ref

TESTS = $(check_PROGRAMS)

Expand Down Expand Up @@ -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 changes: 47 additions & 0 deletions test/boost_string_ref.cpp
@@ -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)