mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-16 18:56:54 +02:00
Added C++17 std::byte
support.
`std::byte` is mapped to https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family. `std::vector<std::byte>` and `std::byte[]` are mapped to https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family.
This commit is contained in:
14
.travis.yml
14
.travis.yml
@@ -38,13 +38,13 @@ matrix:
|
||||
include:
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="1"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="1"
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3"
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: ACTION="ci/build_cmake.sh" ARCH="64" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="2"
|
||||
@@ -89,7 +89,7 @@ matrix:
|
||||
- libc6-dbg
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3" X3_PARSE="ON"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3" X3_PARSE="ON"
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
@@ -106,7 +106,7 @@ matrix:
|
||||
- libc6-dbg
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="32" SHARED="OFF" CHAR_SIGN="unsigned" API_VERSION="2"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="32" SHARED="OFF" CHAR_SIGN="unsigned" API_VERSION="2"
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
@@ -128,7 +128,7 @@ matrix:
|
||||
- libc6-dbg
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
@@ -142,7 +142,7 @@ matrix:
|
||||
- libc6-dbg
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="32" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="3" X3_PARSE="ON"
|
||||
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="32" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="3" X3_PARSE="ON"
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
|
@@ -182,8 +182,11 @@ IF (MSGPACK_ENABLE_CXX)
|
||||
include/msgpack/adaptor/cpp11/unique_ptr.hpp
|
||||
include/msgpack/adaptor/cpp11/unordered_map.hpp
|
||||
include/msgpack/adaptor/cpp11/unordered_set.hpp
|
||||
include/msgpack/adaptor/cpp17/byte.hpp
|
||||
include/msgpack/adaptor/cpp17/carray_byte.hpp
|
||||
include/msgpack/adaptor/cpp17/optional.hpp
|
||||
include/msgpack/adaptor/cpp17/string_view.hpp
|
||||
include/msgpack/adaptor/cpp17/vector_byte.hpp
|
||||
include/msgpack/adaptor/define.hpp
|
||||
include/msgpack/adaptor/define_decl.hpp
|
||||
include/msgpack/adaptor/deque.hpp
|
||||
@@ -539,8 +542,11 @@ IF (MSGPACK_ENABLE_CXX)
|
||||
include/msgpack/v1/adaptor/cpp11/unique_ptr.hpp
|
||||
include/msgpack/v1/adaptor/cpp11/unordered_map.hpp
|
||||
include/msgpack/v1/adaptor/cpp11/unordered_set.hpp
|
||||
include/msgpack/v1/adaptor/cpp17/byte.hpp
|
||||
include/msgpack/v1/adaptor/cpp17/carray_byte.hpp
|
||||
include/msgpack/v1/adaptor/cpp17/optional.hpp
|
||||
include/msgpack/v1/adaptor/cpp17/string_view.hpp
|
||||
include/msgpack/v1/adaptor/cpp17/vector_byte.hpp
|
||||
include/msgpack/v1/adaptor/define.hpp
|
||||
include/msgpack/v1/adaptor/define_decl.hpp
|
||||
include/msgpack/v1/adaptor/deque.hpp
|
||||
|
@@ -23,7 +23,7 @@ else
|
||||
export BIT32="OFF"
|
||||
fi
|
||||
|
||||
cmake -DMSGPACK_CXX11=${CXX11} -DMSGPACK_32BIT=${BIT32} -DMSGPACK_BOOST=${BOOST} -DBUILD_SHARED_LIBS=${SHARED} -DMSGPACK_CHAR_SIGN=${CHAR_SIGN} -DMSGPACK_DEFAULT_API_VERSION=${API_VERSION} -DMSGPACK_USE_X3_PARSE=${X3_PARSE} ..
|
||||
cmake -DMSGPACK_CXX11=${CXX11} -DMSGPACK_CXX17=${CXX17} -DMSGPACK_32BIT=${BIT32} -DMSGPACK_BOOST=${BOOST} -DBUILD_SHARED_LIBS=${SHARED} -DMSGPACK_CHAR_SIGN=${CHAR_SIGN} -DMSGPACK_DEFAULT_API_VERSION=${API_VERSION} -DMSGPACK_USE_X3_PARSE=${X3_PARSE} ..
|
||||
|
||||
ret=$?
|
||||
if [ $ret -ne 0 ]
|
||||
@@ -39,7 +39,7 @@ then
|
||||
exit $ret
|
||||
fi
|
||||
|
||||
make test
|
||||
ctest -VV
|
||||
|
||||
ret=$?
|
||||
if [ $ret -ne 0 ]
|
||||
|
@@ -22,7 +22,7 @@ then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cmake -DMSGPACK_FUZZ_REGRESSION="ON" -DMSGPACK_CXX11="ON" -DMSGPACK_BOOST=${BOOST} -DMSGPACK_SAN=${MSGPACK_SAN} -v ..
|
||||
cmake -DMSGPACK_FUZZ_REGRESSION="ON" -DMSGPACK_CXX17="ON" -DMSGPACK_BOOST=${BOOST} -DMSGPACK_SAN=${MSGPACK_SAN} -v ..
|
||||
|
||||
ret=$?
|
||||
if [ $ret -ne 0 ]
|
||||
|
16
include/msgpack/adaptor/cpp17/byte.hpp
Normal file
16
include/msgpack/adaptor/cpp17/byte.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// 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_CPP17_BYTE_HPP
|
||||
#define MSGPACK_TYPE_CPP17_BYTE_HPP
|
||||
|
||||
#include "msgpack/v1/adaptor/cpp17/byte.hpp"
|
||||
|
||||
#endif // MSGPACK_TYPE_CPP17_BYTE_HPP
|
16
include/msgpack/adaptor/cpp17/carray_byte.hpp
Normal file
16
include/msgpack/adaptor/cpp17/carray_byte.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// 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_CPP17_CARRAY_BYTE_HPP
|
||||
#define MSGPACK_TYPE_CPP17_CARRAY_BYTE_HPP
|
||||
|
||||
#include "msgpack/v1/adaptor/cpp17/carray_byte.hpp"
|
||||
|
||||
#endif // MSGPACK_TYPE_CPP17_CARRAY_BYTE_HPP
|
16
include/msgpack/adaptor/cpp17/vector_byte.hpp
Normal file
16
include/msgpack/adaptor/cpp17/vector_byte.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// 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_CPP17_VECTOR_BYTE_HPP
|
||||
#define MSGPACK_TYPE_CPP17_VECTOR_BYTE_HPP
|
||||
|
||||
#include "msgpack/v1/adaptor/cpp17/vector_byte.hpp"
|
||||
|
||||
#endif // MSGPACK_TYPE_CPP17_VECTOR_BYTE_HPP
|
@@ -51,6 +51,10 @@
|
||||
#include "adaptor/cpp17/string_view.hpp"
|
||||
#endif // MSGPACK_HAS_INCLUDE(<string_view>)
|
||||
|
||||
#include "adaptor/cpp17/byte.hpp"
|
||||
#include "adaptor/cpp17/carray_byte.hpp"
|
||||
#include "adaptor/cpp17/vector_byte.hpp"
|
||||
|
||||
#endif // defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#if defined(MSGPACK_USE_BOOST)
|
||||
|
74
include/msgpack/v1/adaptor/cpp17/byte.hpp
Normal file
74
include/msgpack/v1/adaptor/cpp17/byte.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// 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_BYTE_HPP
|
||||
#define MSGPACK_V1_TYPE_BYTE_HPP
|
||||
|
||||
#if __cplusplus >= 201703
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/adaptor/adaptor_base.hpp"
|
||||
#include "msgpack/adaptor/adaptor_base.hpp"
|
||||
#include "msgpack/adaptor/int_decl.hpp"
|
||||
#include "msgpack/object.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
/// @cond
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
/// @endcond
|
||||
|
||||
namespace adaptor {
|
||||
|
||||
template <>
|
||||
struct convert<std::byte> {
|
||||
msgpack::object const& operator()(msgpack::object const& o, std::byte& v) const {
|
||||
v = static_cast<std::byte>(type::detail::convert_integer<unsigned char>(o));
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct pack<std::byte> {
|
||||
template <typename Stream>
|
||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::byte v) const {
|
||||
o.pack_unsigned_char(static_cast<unsigned char>(v));
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct object<std::byte> {
|
||||
void operator()(msgpack::object& o, std::byte v) const {
|
||||
o.type = msgpack::type::POSITIVE_INTEGER;
|
||||
o.via.u64 = static_cast<unsigned char>(v);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct object_with_zone<std::byte> {
|
||||
void operator()(msgpack::object::with_zone& o, const std::byte& v) const {
|
||||
static_cast<msgpack::object&>(o) << v;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace adaptor
|
||||
|
||||
/// @cond
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
/// @endcond
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // __cplusplus >= 201703
|
||||
|
||||
#endif // MSGPACK_V1_TYPE_BYTE_HPP
|
109
include/msgpack/v1/adaptor/cpp17/carray_byte.hpp
Normal file
109
include/msgpack/v1/adaptor/cpp17/carray_byte.hpp
Normal file
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// 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_CARRAY_BYTE_HPP
|
||||
#define MSGPACK_V1_TYPE_CARRAY_BYTE_HPP
|
||||
|
||||
#if __cplusplus >= 201703
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/adaptor/adaptor_base.hpp"
|
||||
#include "msgpack/adaptor/check_container_size.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstddef>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
/// @cond
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
/// @endcond
|
||||
|
||||
namespace adaptor {
|
||||
|
||||
template <std::size_t N>
|
||||
struct convert<std::byte[N]> {
|
||||
msgpack::object const& operator()(msgpack::object const& o, std::byte(&v)[N]) const {
|
||||
switch (o.type) {
|
||||
case msgpack::type::BIN:
|
||||
if (o.via.bin.size > N) { throw msgpack::type_error(); }
|
||||
std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
|
||||
break;
|
||||
case msgpack::type::STR:
|
||||
if (o.via.str.size > N) { throw msgpack::type_error(); }
|
||||
std::memcpy(v, o.via.str.ptr, o.via.str.size);
|
||||
if (o.via.str.size < N) v[o.via.str.size] = std::byte{'\0'};
|
||||
break;
|
||||
default:
|
||||
throw msgpack::type_error();
|
||||
break;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct pack<std::byte[N]> {
|
||||
template <typename Stream>
|
||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const {
|
||||
std::byte const* p = v;
|
||||
uint32_t size = checked_get_container_size(N);
|
||||
o.pack_bin(size);
|
||||
o.pack_bin_body(reinterpret_cast<char const*>(p), size);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct pack<const std::byte[N]> {
|
||||
template <typename Stream>
|
||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const {
|
||||
std::byte const* p = v;
|
||||
uint32_t size = checked_get_container_size(N);
|
||||
o.pack_bin(size);
|
||||
o.pack_bin_body(reinterpret_cast<char const*>(p), size);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct object_with_zone<std::byte[N]> {
|
||||
void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const {
|
||||
uint32_t size = checked_get_container_size(N);
|
||||
o.type = msgpack::type::BIN;
|
||||
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
|
||||
o.via.bin.ptr = ptr;
|
||||
o.via.bin.size = size;
|
||||
std::memcpy(ptr, v, size);
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct object_with_zone<const std::byte[N]> {
|
||||
void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const {
|
||||
uint32_t size = checked_get_container_size(N);
|
||||
o.type = msgpack::type::BIN;
|
||||
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
|
||||
o.via.bin.ptr = ptr;
|
||||
o.via.bin.size = size;
|
||||
std::memcpy(ptr, v, size);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace adaptor
|
||||
|
||||
/// @cond
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
/// @endcond
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // __cplusplus >= 201703
|
||||
|
||||
#endif // MSGPACK_V1_TYPE_CARRAY_BYTE_HPP
|
119
include/msgpack/v1/adaptor/cpp17/vector_byte.hpp
Normal file
119
include/msgpack/v1/adaptor/cpp17/vector_byte.hpp
Normal file
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// 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_VECTOR_BYTE_HPP
|
||||
#define MSGPACK_V1_TYPE_VECTOR_BYTE_HPP
|
||||
|
||||
#if __cplusplus >= 201703
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/adaptor/adaptor_base.hpp"
|
||||
#include "msgpack/adaptor/check_container_size.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cstddef>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
/// @cond
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
/// @endcond
|
||||
|
||||
namespace adaptor {
|
||||
|
||||
template <typename Alloc>
|
||||
struct convert<std::vector<std::byte, Alloc> > {
|
||||
msgpack::object const& operator()(msgpack::object const& o, std::vector<std::byte, Alloc>& v) const {
|
||||
switch (o.type) {
|
||||
case msgpack::type::BIN:
|
||||
v.resize(o.via.bin.size);
|
||||
if (o.via.bin.size != 0) {
|
||||
#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__)
|
||||
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
|
||||
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
|
||||
}
|
||||
break;
|
||||
case msgpack::type::STR:
|
||||
v.resize(o.via.str.size);
|
||||
if (o.via.str.size != 0) {
|
||||
#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__)
|
||||
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
|
||||
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw msgpack::type_error();
|
||||
break;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Alloc>
|
||||
struct pack<std::vector<std::byte, Alloc> > {
|
||||
template <typename Stream>
|
||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<std::byte, Alloc>& v) const {
|
||||
uint32_t size = checked_get_container_size(v.size());
|
||||
o.pack_bin(size);
|
||||
if (size != 0) {
|
||||
o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Alloc>
|
||||
struct object<std::vector<std::byte, Alloc> > {
|
||||
void operator()(msgpack::object& o, const std::vector<std::byte, Alloc>& v) const {
|
||||
uint32_t size = checked_get_container_size(v.size());
|
||||
o.type = msgpack::type::BIN;
|
||||
if (size != 0) {
|
||||
o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
|
||||
}
|
||||
o.via.bin.size = size;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Alloc>
|
||||
struct object_with_zone<std::vector<std::byte, Alloc> > {
|
||||
void operator()(msgpack::object::with_zone& o, const std::vector<std::byte, Alloc>& v) const {
|
||||
uint32_t size = checked_get_container_size(v.size());
|
||||
o.type = msgpack::type::BIN;
|
||||
o.via.bin.size = size;
|
||||
if (size != 0) {
|
||||
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
|
||||
o.via.bin.ptr = ptr;
|
||||
std::memcpy(ptr, &v.front(), size);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace adaptor
|
||||
|
||||
/// @cond
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
/// @endcond
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // __cplusplus >= 201703
|
||||
|
||||
#endif // MSGPACK_V1_TYPE_VECTOR_BYTE_HPP
|
@@ -10,6 +10,8 @@
|
||||
|
||||
// C++17
|
||||
|
||||
#if MSGPACK_HAS_INCLUDE(<optional>)
|
||||
|
||||
TEST(MSGPACK_CPP17, optional_pack_convert_nil)
|
||||
{
|
||||
std::stringstream ss;
|
||||
@@ -171,7 +173,11 @@ TEST(MSGPACK_CPP17, optional_pack_convert_no_def_con)
|
||||
EXPECT_TRUE(val1 == val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, string_view_pack_convert_string_view)
|
||||
#endif // MSGPACK_HAS_INCLUDE(<optional>)
|
||||
|
||||
#if MSGPACK_HAS_INCLUDE(<string_view>)
|
||||
|
||||
TEST(MSGPACK_CPP17, string_view_pack_convert)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string s = "ABC";
|
||||
@@ -185,7 +191,7 @@ TEST(MSGPACK_CPP17, string_view_pack_convert_string_view)
|
||||
EXPECT_TRUE(val1 == val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, string_view_object_strinf_view)
|
||||
TEST(MSGPACK_CPP17, string_view_object)
|
||||
{
|
||||
std::string s = "ABC";
|
||||
std::string_view val1(s);
|
||||
@@ -194,7 +200,7 @@ TEST(MSGPACK_CPP17, string_view_object_strinf_view)
|
||||
EXPECT_TRUE(val1 == val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, string_view_object_with_zone_string_view)
|
||||
TEST(MSGPACK_CPP17, string_view_object_with_zone)
|
||||
{
|
||||
msgpack::zone z;
|
||||
std::string s = "ABC";
|
||||
@@ -204,4 +210,119 @@ TEST(MSGPACK_CPP17, string_view_object_with_zone_string_view)
|
||||
EXPECT_TRUE(val1 == val2);
|
||||
}
|
||||
|
||||
#endif // MSGPACK_HAS_INCLUDE(<string_view>)
|
||||
|
||||
TEST(MSGPACK_CPP17, byte_pack_convert)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::byte val1{0xff};
|
||||
|
||||
msgpack::pack(ss, val1);
|
||||
|
||||
msgpack::object_handle oh;
|
||||
msgpack::unpack(oh, ss.str().data(), ss.str().size());
|
||||
std::byte val2 = oh.get().as<std::byte>();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, byte_object)
|
||||
{
|
||||
std::byte val1{0x00};
|
||||
msgpack::object obj(val1);
|
||||
std::byte val2 = obj.as<std::byte>();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, byte_object_with_zone)
|
||||
{
|
||||
msgpack::zone z;
|
||||
std::byte val1{80};
|
||||
msgpack::object obj(val1, z);
|
||||
std::byte val2 = obj.as<std::byte>();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, vector_byte_pack_convert)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::vector<std::byte> val1{
|
||||
std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff}
|
||||
};
|
||||
|
||||
msgpack::pack(ss, val1);
|
||||
|
||||
char packed[] = { char(0xc4), char(0x05), char(0x01), char(0x02), char(0x7f), char(0x80), char(0xff) };
|
||||
for (size_t i = 0; i != sizeof(packed); ++i) {
|
||||
EXPECT_EQ(ss.str()[i], packed[i]);
|
||||
}
|
||||
|
||||
msgpack::object_handle oh;
|
||||
msgpack::unpack(oh, ss.str().data(), ss.str().size());
|
||||
std::vector<std::byte> val2 = oh.get().as<std::vector<std::byte>>();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, vector_byte_object)
|
||||
{
|
||||
std::vector<std::byte> val1{
|
||||
std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff}
|
||||
};
|
||||
|
||||
// Caller need to manage val1's lifetime. The Data is not copied.
|
||||
msgpack::object obj(val1);
|
||||
|
||||
std::vector<std::byte> val2 = obj.as<std::vector<std::byte>>();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, vector_byte_object_with_zone)
|
||||
{
|
||||
msgpack::zone z;
|
||||
std::vector<std::byte> val1{
|
||||
std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff}
|
||||
};
|
||||
msgpack::object obj(val1, z);
|
||||
|
||||
std::vector<std::byte> val2 = obj.as<std::vector<std::byte>>();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, carray_byte_pack_convert)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::byte val1[] = {
|
||||
std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff}
|
||||
};
|
||||
|
||||
msgpack::pack(ss, val1);
|
||||
|
||||
char packed[] = { char(0xc4), char(0x05), char(0x01), char(0x02), char(0x7f), char(0x80), char(0xff) };
|
||||
for (size_t i = 0; i != sizeof(packed); ++i) {
|
||||
EXPECT_EQ(ss.str()[i], packed[i]);
|
||||
}
|
||||
|
||||
msgpack::object_handle oh;
|
||||
msgpack::unpack(oh, ss.str().data(), ss.str().size());
|
||||
std::byte val2[sizeof(val1)];
|
||||
oh.get().convert(val2);
|
||||
for (size_t i = 0; i != sizeof(val1); ++i) {
|
||||
EXPECT_EQ(val1[i], val2[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_CPP17, carray_byte_object_with_zone)
|
||||
{
|
||||
msgpack::zone z;
|
||||
std::byte val1[] = {
|
||||
std::byte{0x01}, std::byte{0x02}, std::byte{0x7f}, std::byte{0x80}, std::byte{0xff}
|
||||
};
|
||||
msgpack::object obj(val1, z);
|
||||
|
||||
std::byte val2[sizeof(val1)];
|
||||
obj.convert(val2);
|
||||
for (size_t i = 0; i != sizeof(val1); ++i) {
|
||||
EXPECT_EQ(val1[i], val2[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !defined(MSGPACK_USE_CPP03) && __cplusplus >= 201703
|
||||
|
Reference in New Issue
Block a user