Skip to content

Commit 7024a1e

Browse files
authored
Merge pull request #607 from redboltz/fix_597
Solved #597.
2 parents ca4a425 + a502097 commit 7024a1e

File tree

8 files changed

+437
-1
lines changed

8 files changed

+437
-1
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ IF (MSGPACK_USE_X3_PARSE)
5050
ENDIF ()
5151
ENDIF ()
5252
ELSE ()
53-
IF (MSGPACK_CXX11)
53+
IF (MSGPACK_CXX17)
54+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
55+
SET (CMAKE_CXX_FLAGS "-std=c++17 ${CMAKE_CXX_FLAGS}")
56+
ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
57+
SET (CMAKE_CXX_FLAGS "-std=c++17 ${CMAKE_CXX_FLAGS}")
58+
ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
59+
MESSAGE ( FATAL_ERROR "MSVC doesn't support C++17.")
60+
ENDIF ()
61+
ELSEIF (MSGPACK_CXX11)
5462
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
5563
SET (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
5664
ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
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) 2017 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_OPTIONAL_HPP
12+
#define MSGPACK_TYPE_CPP17_OPTIONAL_HPP
13+
14+
#include "msgpack/v1/adaptor/cpp17/optional.hpp"
15+
16+
#endif // MSGPACK_TYPE_CPP17_OPTIONAL_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) 2017 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_STRING_VIEW_HPP
12+
#define MSGPACK_TYPE_CPP17_STRING_VIEW_HPP
13+
14+
#include "msgpack/v1/adaptor/cpp17/string_view.hpp"
15+
16+
#endif // MSGPACK_TYPE_CPP17_STRING_VIEW_HPP

include/msgpack/type.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
#include "adaptor/cpp11/unordered_map.hpp"
4343
#include "adaptor/cpp11/unordered_set.hpp"
4444

45+
#if __cplusplus >= 201703
46+
47+
#include "adaptor/cpp17/optional.hpp"
48+
#include "adaptor/cpp17/string_view.hpp"
49+
50+
#endif // __cplusplus >= 201703
51+
4552
#endif // defined(MSGPACK_USE_CPP03)
4653

4754
#if defined(MSGPACK_USE_BOOST)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2017 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_OPTIONAL_HPP
11+
#define MSGPACK_V1_TYPE_OPTIONAL_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 <optional>
20+
21+
namespace msgpack {
22+
23+
/// @cond
24+
MSGPACK_API_VERSION_NAMESPACE(v1) {
25+
/// @endcond
26+
27+
namespace adaptor {
28+
29+
#if !defined (MSGPACK_USE_CPP03)
30+
31+
template <typename T>
32+
struct as<std::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
33+
std::optional<T> operator()(msgpack::object const& o) const {
34+
if(o.is_nil()) return std::nullopt;
35+
return o.as<T>();
36+
}
37+
};
38+
39+
#endif // !defined (MSGPACK_USE_CPP03)
40+
41+
template <typename T>
42+
struct convert<std::optional<T> > {
43+
msgpack::object const& operator()(msgpack::object const& o, std::optional<T>& v) const {
44+
if(o.is_nil()) v = std::nullopt;
45+
else {
46+
T t;
47+
msgpack::adaptor::convert<T>()(o, t);
48+
v = t;
49+
}
50+
return o;
51+
}
52+
};
53+
54+
template <typename T>
55+
struct pack<std::optional<T> > {
56+
template <typename Stream>
57+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::optional<T>& v) const {
58+
if (v) o.pack(*v);
59+
else o.pack_nil();
60+
return o;
61+
}
62+
};
63+
64+
template <typename T>
65+
struct object<std::optional<T> > {
66+
void operator()(msgpack::object& o, const std::optional<T>& v) const {
67+
if (v) msgpack::adaptor::object<T>()(o, *v);
68+
else o.type = msgpack::type::NIL;
69+
}
70+
};
71+
72+
template <typename T>
73+
struct object_with_zone<std::optional<T> > {
74+
void operator()(msgpack::object::with_zone& o, const std::optional<T>& v) const {
75+
if (v) msgpack::adaptor::object_with_zone<T>()(o, *v);
76+
else o.type = msgpack::type::NIL;
77+
}
78+
};
79+
80+
} // namespace adaptor
81+
82+
/// @cond
83+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
84+
/// @endcond
85+
86+
} // namespace msgpack
87+
88+
#endif // __cplusplus >= 201703
89+
90+
#endif // MSGPACK_V1_TYPE_OPTIONAL_HPP
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2017 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_STRING_VIEW_HPP
11+
#define MSGPACK_V1_TYPE_STRING_VIEW_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 <string_view>
20+
21+
namespace msgpack {
22+
23+
/// @cond
24+
MSGPACK_API_VERSION_NAMESPACE(v1) {
25+
/// @endcond
26+
27+
namespace adaptor {
28+
29+
template <>
30+
struct convert<std::string_view> {
31+
msgpack::object const& operator()(msgpack::object const& o, std::string_view& v) const {
32+
switch (o.type) {
33+
case msgpack::type::BIN:
34+
v = std::string_view(o.via.bin.ptr, o.via.bin.size);
35+
break;
36+
case msgpack::type::STR:
37+
v = std::string_view(o.via.str.ptr, o.via.str.size);
38+
break;
39+
default:
40+
throw msgpack::type_error();
41+
break;
42+
}
43+
return o;
44+
}
45+
};
46+
47+
template <>
48+
struct pack<std::string_view> {
49+
template <typename Stream>
50+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::string_view& v) const {
51+
uint32_t size = checked_get_container_size(v.size());
52+
o.pack_str(size);
53+
o.pack_str_body(v.data(), size);
54+
return o;
55+
}
56+
};
57+
58+
template <>
59+
struct object<std::string_view> {
60+
void operator()(msgpack::object& o, const std::string_view& v) const {
61+
uint32_t size = checked_get_container_size(v.size());
62+
o.type = msgpack::type::STR;
63+
o.via.str.ptr = v.data();
64+
o.via.str.size = size;
65+
}
66+
};
67+
68+
template <>
69+
struct object_with_zone<std::string_view> {
70+
void operator()(msgpack::object::with_zone& o, const std::string_view& v) const {
71+
static_cast<msgpack::object&>(o) << v;
72+
}
73+
};
74+
75+
76+
} // namespace adaptor
77+
78+
/// @cond
79+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
80+
/// @endcond
81+
82+
} // namespace msgpack
83+
84+
#endif // __cplusplus >= 201703
85+
86+
#endif // MSGPACK_V1_TYPE_STRING_VIEW_HPP

test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ IF (MSGPACK_CXX11)
6666
)
6767
ENDIF ()
6868

69+
IF (MSGPACK_CXX17)
70+
LIST (APPEND check_PROGRAMS
71+
msgpack_cpp17.cpp
72+
)
73+
ENDIF ()
74+
6975
FOREACH (source_file ${check_PROGRAMS})
7076
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
7177
ADD_EXECUTABLE (

0 commit comments

Comments
 (0)