Merge pull request #737 from redboltz/fix_735

Added wstring adaptor.
This commit is contained in:
Takatoshi Kondo 2018-09-15 18:03:23 +09:00 committed by GitHub
commit db54c78ed4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 234 additions and 11 deletions

View File

@ -231,6 +231,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/vector_bool.hpp
include/msgpack/adaptor/vector_char.hpp
include/msgpack/adaptor/vector_unsigned_char.hpp
include/msgpack/adaptor/wstring.hpp
include/msgpack/cpp_config.hpp
include/msgpack/cpp_config_decl.hpp
include/msgpack/create_object_visitor.hpp
@ -605,6 +606,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/v1/adaptor/vector_bool.hpp
include/msgpack/v1/adaptor/vector_char.hpp
include/msgpack/v1/adaptor/vector_unsigned_char.hpp
include/msgpack/v1/adaptor/wstring.hpp
include/msgpack/v1/cpp_config.hpp
include/msgpack/v1/cpp_config_decl.hpp
include/msgpack/v1/detail/cpp03_zone.hpp

View File

@ -0,0 +1,15 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2018 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MSGPACK_TYPE_WSTRING_HPP
#define MSGPACK_TYPE_WSTRING_HPP
#include "msgpack/v1/adaptor/wstring.hpp"
#endif // MSGPACK_TYPE_WSTRING_HPP

View File

@ -21,6 +21,7 @@
#include "adaptor/vector_bool.hpp"
#include "adaptor/vector_char.hpp"
#include "adaptor/vector_unsigned_char.hpp"
#include "adaptor/wstring.hpp"
#include "adaptor/msgpack_tuple.hpp"
#include "adaptor/define.hpp"

View File

@ -64,10 +64,9 @@ inline T convert_integer(msgpack::object const& o)
}
template <>
struct object_char_sign<true> {
struct object_sign<true> {
template <typename T>
static typename msgpack::enable_if<msgpack::is_same<T, char>::value>::type
make(msgpack::object& o, T v) {
static void make(msgpack::object& o, T v) {
if (v < 0) {
o.type = msgpack::type::NEGATIVE_INTEGER;
o.via.i64 = v;
@ -80,15 +79,17 @@ struct object_char_sign<true> {
};
template <>
struct object_char_sign<false> {
static void make(msgpack::object& o, char v) {
struct object_sign<false> {
template <typename T>
static void make(msgpack::object& o, T v) {
o.type = msgpack::type::POSITIVE_INTEGER;
o.via.u64 = v;
}
};
inline void object_char(msgpack::object& o, char v) {
return object_char_sign<is_signed<char>::value>::make(o, v);
template <typename T>
inline void object_char(msgpack::object& o, T v) {
return object_sign<is_signed<T>::value>::make(o, v);
}
} // namespace detail
@ -102,6 +103,12 @@ struct convert<char> {
{ v = type::detail::convert_integer<char>(o); return o; }
};
template <>
struct convert<wchar_t> {
msgpack::object const& operator()(msgpack::object const& o, wchar_t& v) const
{ v = type::detail::convert_integer<wchar_t>(o); return o; }
};
template <>
struct convert<signed char> {
msgpack::object const& operator()(msgpack::object const& o, signed char& v) const
@ -171,6 +178,13 @@ struct pack<char> {
{ o.pack_char(v); return o; }
};
template <>
struct pack<wchar_t> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, wchar_t v) const
{ o.pack_wchar(v); return o; }
};
template <>
struct pack<signed char> {
template <typename Stream>
@ -249,6 +263,12 @@ struct object<char> {
{ type::detail::object_char(o, v); }
};
template <>
struct object<wchar_t> {
void operator()(msgpack::object& o, wchar_t v) const
{ type::detail::object_char(o, v); }
};
template <>
struct object<signed char> {
void operator()(msgpack::object& o, signed char v) const {
@ -367,6 +387,13 @@ struct object_with_zone<char> {
}
};
template <>
struct object_with_zone<wchar_t> {
void operator()(msgpack::object::with_zone& o, wchar_t v) const {
static_cast<msgpack::object&>(o) << v;
}
};
template <>
struct object_with_zone<signed char> {
void operator()(msgpack::object::with_zone& o, signed char v) const {

View File

@ -33,9 +33,10 @@ template <typename T>
T convert_integer(msgpack::object const& o);
template <bool Signed>
struct object_char_sign;
struct object_sign;
void object_char(msgpack::object& o, char v);
template <typename T>
void object_char(msgpack::object& o, T v);
} // namespace detail
} // namespace type

View File

@ -0,0 +1,121 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2018 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MSGPACK_V1_TYPE_WSTRING_HPP
#define MSGPACK_V1_TYPE_WSTRING_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include <vector>
namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace adaptor {
#if !defined(MSGPACK_USE_CPP03)
template <>
struct as<std::wstring> {
std::wstring operator()(const msgpack::object& o) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
std::wstring v;
v.reserve(o.via.array.size);
if (o.via.array.size > 0) {
msgpack::object* p = o.via.array.ptr;
msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
do {
v.push_back(p->as<wchar_t>());
++p;
} while (p < pend);
}
return v;
}
};
#endif // !defined(MSGPACK_USE_CPP03)
template <>
struct convert<std::wstring> {
msgpack::object const& operator()(msgpack::object const& o, std::wstring& v) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
v.resize(o.via.array.size);
if (o.via.array.size > 0) {
msgpack::object* p = o.via.array.ptr;
msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
std::wstring::iterator it = v.begin();
do {
p->convert(*it);
++p;
++it;
} while(p < pend);
}
return o;
}
};
template <>
struct pack<std::wstring> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::wstring& v) const {
uint32_t size = checked_get_container_size(v.size());
o.pack_array(size);
for (std::wstring::const_iterator it(v.begin()), it_end(v.end());
it != it_end; ++it) {
o.pack(*it);
}
return o;
}
};
template <>
struct object_with_zone<std::wstring> {
void operator()(msgpack::object::with_zone& o, const std::wstring& v) const {
o.type = msgpack::type::ARRAY;
if (v.empty()) {
o.via.array.ptr = MSGPACK_NULLPTR;
o.via.array.size = 0;
}
else {
uint32_t size = checked_get_container_size(v.size());
msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
msgpack::object* const pend = p + size;
o.via.array.ptr = p;
o.via.array.size = size;
std::wstring::const_iterator it(v.begin());
do {
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
*p = msgpack::object(*it, o.zone);
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
++p;
++it;
} while(p < pend);
}
}
};
} // namespace adaptor
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // MSGPACK_V1_TYPE_WSTRING_HPP

View File

@ -271,6 +271,20 @@ public:
*/
packer<Stream>& pack_char(char d);
/// Packing wchar_t
/**
* The byte size of the packed data depends on `d`.
* If `d` is zero or positive, the packed type is positive fixnum, or uint*,
* else the packed type is negative fixnum, or int*
* The minimum byte size expression is used.
* See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
*
* @param d a packing object.
*
* @return The reference of `*this`.
*/
packer<Stream>& pack_wchar(wchar_t d);
/// Packing signed char
/**
* The byte size of the packed data depends on `d`.
@ -820,6 +834,18 @@ inline packer<Stream>& packer<Stream>::pack_char(char d)
return *this;
}
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_wchar(wchar_t d)
{
if (d < 0) {
pack_imp_int64(static_cast<int64_t>(d));
}
else {
pack_imp_uint64(static_cast<uint64_t>(d));
}
return *this;
}
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_signed_char(signed char d)
{

View File

@ -30,7 +30,7 @@ struct is_signed;
template <bool Signed>
struct object_char_sign;
struct object_sign;
//using v1::type::detail::convert_integer_sign;

View File

@ -30,7 +30,7 @@ struct is_signed;
template <bool Signed>
struct object_char_sign;
struct object_sign;
//using v2::type::detail::convert_integer_sign;

View File

@ -614,3 +614,22 @@ TEST(MSGPACK_STL, simple_buffer_non_const_cstring)
EXPECT_EQ(val1, val2);
}
}
TEST(MSGPACK_STL, simple_buffer_wstring)
{
for (unsigned int k = 0; k < kLoop; k++) {
wstring val1;
for (unsigned int i = 0; i < kElements; i++)
val1 += L'a' + rand() % 26;
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
EXPECT_EQ(oh.get().type, msgpack::type::ARRAY);
wstring val2 = oh.get().as<wstring>();
EXPECT_EQ(val1, val2);
wstring val3;
oh.get().convert(val3);
EXPECT_EQ(val1, val3);
}
}

View File

@ -332,6 +332,17 @@ TEST(object_without_zone, string)
#endif // MSGPACK_DEFAULT_API_VERSION == 1
// wstring
TEST(object_with_zone, wstring)
{
wstring v = L"abc";
msgpack::zone z;
msgpack::object obj(v, z);
EXPECT_EQ(obj.as<wstring>(), v);
v[0] = 'd';
EXPECT_EQ(obj.as<wstring>()[0], L'a');
}
// char*
TEST(object_with_zone, char_ptr)
{