Skip to content

Commit 8a788f3

Browse files
committed
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.
1 parent 7710868 commit 8a788f3

File tree

12 files changed

+494
-13
lines changed

12 files changed

+494
-13
lines changed

.travis.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ matrix:
3838
include:
3939
- os: osx
4040
compiler: clang
41-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="1"
41+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="1"
4242
- os: osx
4343
compiler: clang
44-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3"
44+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3"
4545
- os: osx
4646
compiler: clang
47-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
47+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
4848
- os: osx
4949
compiler: clang
5050
env: ACTION="ci/build_cmake.sh" ARCH="64" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="2"
@@ -89,7 +89,7 @@ matrix:
8989
- libc6-dbg
9090
- os: linux
9191
compiler: clang
92-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3" X3_PARSE="ON"
92+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="3" X3_PARSE="ON"
9393
addons:
9494
apt:
9595
sources:
@@ -106,7 +106,7 @@ matrix:
106106
- libc6-dbg
107107
- os: linux
108108
compiler: clang
109-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="32" SHARED="OFF" CHAR_SIGN="unsigned" API_VERSION="2"
109+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="32" SHARED="OFF" CHAR_SIGN="unsigned" API_VERSION="2"
110110
addons:
111111
apt:
112112
sources:
@@ -128,7 +128,7 @@ matrix:
128128
- libc6-dbg
129129
- os: linux
130130
compiler: gcc
131-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
131+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2"
132132
addons:
133133
apt:
134134
sources:
@@ -142,7 +142,7 @@ matrix:
142142
- libc6-dbg
143143
- os: linux
144144
compiler: gcc
145-
env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="32" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="3" X3_PARSE="ON"
145+
env: ACTION="ci/build_cmake.sh" CXX17="ON" ARCH="32" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="3" X3_PARSE="ON"
146146
addons:
147147
apt:
148148
sources:

Files.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,11 @@ IF (MSGPACK_ENABLE_CXX)
182182
include/msgpack/adaptor/cpp11/unique_ptr.hpp
183183
include/msgpack/adaptor/cpp11/unordered_map.hpp
184184
include/msgpack/adaptor/cpp11/unordered_set.hpp
185+
include/msgpack/adaptor/cpp17/byte.hpp
186+
include/msgpack/adaptor/cpp17/carray_byte.hpp
185187
include/msgpack/adaptor/cpp17/optional.hpp
186188
include/msgpack/adaptor/cpp17/string_view.hpp
189+
include/msgpack/adaptor/cpp17/vector_byte.hpp
187190
include/msgpack/adaptor/define.hpp
188191
include/msgpack/adaptor/define_decl.hpp
189192
include/msgpack/adaptor/deque.hpp
@@ -539,8 +542,11 @@ IF (MSGPACK_ENABLE_CXX)
539542
include/msgpack/v1/adaptor/cpp11/unique_ptr.hpp
540543
include/msgpack/v1/adaptor/cpp11/unordered_map.hpp
541544
include/msgpack/v1/adaptor/cpp11/unordered_set.hpp
545+
include/msgpack/v1/adaptor/cpp17/byte.hpp
546+
include/msgpack/v1/adaptor/cpp17/carray_byte.hpp
542547
include/msgpack/v1/adaptor/cpp17/optional.hpp
543548
include/msgpack/v1/adaptor/cpp17/string_view.hpp
549+
include/msgpack/v1/adaptor/cpp17/vector_byte.hpp
544550
include/msgpack/v1/adaptor/define.hpp
545551
include/msgpack/v1/adaptor/define_decl.hpp
546552
include/msgpack/v1/adaptor/deque.hpp

ci/build_cmake.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ else
2323
export BIT32="OFF"
2424
fi
2525

26-
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} ..
26+
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} ..
2727

2828
ret=$?
2929
if [ $ret -ne 0 ]
@@ -39,7 +39,7 @@ then
3939
exit $ret
4040
fi
4141

42-
make test
42+
ctest -VV
4343

4444
ret=$?
4545
if [ $ret -ne 0 ]

ci/build_regression.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ then
2222
exit 1
2323
fi
2424

25-
cmake -DMSGPACK_FUZZ_REGRESSION="ON" -DMSGPACK_CXX11="ON" -DMSGPACK_BOOST=${BOOST} -DMSGPACK_SAN=${MSGPACK_SAN} -v ..
25+
cmake -DMSGPACK_FUZZ_REGRESSION="ON" -DMSGPACK_CXX17="ON" -DMSGPACK_BOOST=${BOOST} -DMSGPACK_SAN=${MSGPACK_SAN} -v ..
2626

2727
ret=$?
2828
if [ $ret -ne 0 ]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2018 KONDO Takatoshi
5+
//
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See accompanying file LICENSE_1_0.txt or copy at
8+
// http://www.boost.org/LICENSE_1_0.txt)
9+
//
10+
11+
#ifndef MSGPACK_TYPE_CPP17_BYTE_HPP
12+
#define MSGPACK_TYPE_CPP17_BYTE_HPP
13+
14+
#include "msgpack/v1/adaptor/cpp17/byte.hpp"
15+
16+
#endif // MSGPACK_TYPE_CPP17_BYTE_HPP
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2018 KONDO Takatoshi
5+
//
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See accompanying file LICENSE_1_0.txt or copy at
8+
// http://www.boost.org/LICENSE_1_0.txt)
9+
//
10+
11+
#ifndef MSGPACK_TYPE_CPP17_CARRAY_BYTE_HPP
12+
#define MSGPACK_TYPE_CPP17_CARRAY_BYTE_HPP
13+
14+
#include "msgpack/v1/adaptor/cpp17/carray_byte.hpp"
15+
16+
#endif // MSGPACK_TYPE_CPP17_CARRAY_BYTE_HPP
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2018 KONDO Takatoshi
5+
//
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See accompanying file LICENSE_1_0.txt or copy at
8+
// http://www.boost.org/LICENSE_1_0.txt)
9+
//
10+
11+
#ifndef MSGPACK_TYPE_CPP17_VECTOR_BYTE_HPP
12+
#define MSGPACK_TYPE_CPP17_VECTOR_BYTE_HPP
13+
14+
#include "msgpack/v1/adaptor/cpp17/vector_byte.hpp"
15+
16+
#endif // MSGPACK_TYPE_CPP17_VECTOR_BYTE_HPP

include/msgpack/type.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
#include "adaptor/cpp17/string_view.hpp"
5252
#endif // MSGPACK_HAS_INCLUDE(<string_view>)
5353

54+
#include "adaptor/cpp17/byte.hpp"
55+
#include "adaptor/cpp17/carray_byte.hpp"
56+
#include "adaptor/cpp17/vector_byte.hpp"
57+
5458
#endif // defined(MSGPACK_USE_CPP03)
5559

5660
#if defined(MSGPACK_USE_BOOST)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2018 KONDO Takatoshi
5+
//
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See accompanying file LICENSE_1_0.txt or copy at
8+
// http://www.boost.org/LICENSE_1_0.txt)
9+
//
10+
#ifndef MSGPACK_V1_TYPE_BYTE_HPP
11+
#define MSGPACK_V1_TYPE_BYTE_HPP
12+
13+
#if __cplusplus >= 201703
14+
15+
#include "msgpack/versioning.hpp"
16+
#include "msgpack/adaptor/adaptor_base.hpp"
17+
#include "msgpack/adaptor/adaptor_base.hpp"
18+
#include "msgpack/adaptor/int_decl.hpp"
19+
#include "msgpack/object.hpp"
20+
21+
#include <cstddef>
22+
23+
namespace msgpack {
24+
25+
/// @cond
26+
MSGPACK_API_VERSION_NAMESPACE(v1) {
27+
/// @endcond
28+
29+
namespace adaptor {
30+
31+
template <>
32+
struct convert<std::byte> {
33+
msgpack::object const& operator()(msgpack::object const& o, std::byte& v) const {
34+
v = static_cast<std::byte>(type::detail::convert_integer<unsigned char>(o));
35+
return o;
36+
}
37+
};
38+
39+
template <>
40+
struct pack<std::byte> {
41+
template <typename Stream>
42+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::byte v) const {
43+
o.pack_unsigned_char(static_cast<unsigned char>(v));
44+
return o;
45+
}
46+
};
47+
48+
template <>
49+
struct object<std::byte> {
50+
void operator()(msgpack::object& o, std::byte v) const {
51+
o.type = msgpack::type::POSITIVE_INTEGER;
52+
o.via.u64 = static_cast<unsigned char>(v);
53+
}
54+
};
55+
56+
template <>
57+
struct object_with_zone<std::byte> {
58+
void operator()(msgpack::object::with_zone& o, const std::byte& v) const {
59+
static_cast<msgpack::object&>(o) << v;
60+
}
61+
};
62+
63+
64+
} // namespace adaptor
65+
66+
/// @cond
67+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
68+
/// @endcond
69+
70+
} // namespace msgpack
71+
72+
#endif // __cplusplus >= 201703
73+
74+
#endif // MSGPACK_V1_TYPE_BYTE_HPP
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2018 KONDO Takatoshi
5+
//
6+
// Distributed under the Boost Software License, Version 1.0.
7+
// (See accompanying file LICENSE_1_0.txt or copy at
8+
// http://www.boost.org/LICENSE_1_0.txt)
9+
//
10+
#ifndef MSGPACK_V1_TYPE_CARRAY_BYTE_HPP
11+
#define MSGPACK_V1_TYPE_CARRAY_BYTE_HPP
12+
13+
#if __cplusplus >= 201703
14+
15+
#include "msgpack/versioning.hpp"
16+
#include "msgpack/adaptor/adaptor_base.hpp"
17+
#include "msgpack/adaptor/check_container_size.hpp"
18+
19+
#include <cstring>
20+
#include <cstddef>
21+
22+
namespace msgpack {
23+
24+
/// @cond
25+
MSGPACK_API_VERSION_NAMESPACE(v1) {
26+
/// @endcond
27+
28+
namespace adaptor {
29+
30+
template <std::size_t N>
31+
struct convert<std::byte[N]> {
32+
msgpack::object const& operator()(msgpack::object const& o, std::byte(&v)[N]) const {
33+
switch (o.type) {
34+
case msgpack::type::BIN:
35+
if (o.via.bin.size > N) { throw msgpack::type_error(); }
36+
std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
37+
break;
38+
case msgpack::type::STR:
39+
if (o.via.str.size > N) { throw msgpack::type_error(); }
40+
std::memcpy(v, o.via.str.ptr, o.via.str.size);
41+
if (o.via.str.size < N) v[o.via.str.size] = std::byte{'\0'};
42+
break;
43+
default:
44+
throw msgpack::type_error();
45+
break;
46+
}
47+
return o;
48+
}
49+
};
50+
51+
template <std::size_t N>
52+
struct pack<std::byte[N]> {
53+
template <typename Stream>
54+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const {
55+
std::byte const* p = v;
56+
uint32_t size = checked_get_container_size(N);
57+
o.pack_bin(size);
58+
o.pack_bin_body(reinterpret_cast<char const*>(p), size);
59+
return o;
60+
}
61+
};
62+
63+
template <std::size_t N>
64+
struct pack<const std::byte[N]> {
65+
template <typename Stream>
66+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const {
67+
std::byte const* p = v;
68+
uint32_t size = checked_get_container_size(N);
69+
o.pack_bin(size);
70+
o.pack_bin_body(reinterpret_cast<char const*>(p), size);
71+
return o;
72+
}
73+
};
74+
75+
template <std::size_t N>
76+
struct object_with_zone<std::byte[N]> {
77+
void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const {
78+
uint32_t size = checked_get_container_size(N);
79+
o.type = msgpack::type::BIN;
80+
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
81+
o.via.bin.ptr = ptr;
82+
o.via.bin.size = size;
83+
std::memcpy(ptr, v, size);
84+
}
85+
};
86+
87+
template <std::size_t N>
88+
struct object_with_zone<const std::byte[N]> {
89+
void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const {
90+
uint32_t size = checked_get_container_size(N);
91+
o.type = msgpack::type::BIN;
92+
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
93+
o.via.bin.ptr = ptr;
94+
o.via.bin.size = size;
95+
std::memcpy(ptr, v, size);
96+
}
97+
};
98+
99+
} // namespace adaptor
100+
101+
/// @cond
102+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
103+
/// @endcond
104+
105+
} // namespace msgpack
106+
107+
#endif // __cplusplus >= 201703
108+
109+
#endif // MSGPACK_V1_TYPE_CARRAY_BYTE_HPP

0 commit comments

Comments
 (0)