Skip to content

Commit 5df201e

Browse files
authored
[struct_pb][bug]fix compile error (#824)
1 parent e949cf7 commit 5df201e

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

include/ylt/standalone/iguana/detail/traits.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
#include <variant>
1515
#include <vector>
1616

17+
#if __cplusplus > 201703L
18+
#if __has_include(<span>)
19+
#include <span>
20+
#endif
21+
#endif
22+
1723
#include "iguana/define.h"
1824

1925
namespace iguana {
@@ -40,6 +46,16 @@ struct is_stdstring : is_template_instant_of<std::basic_string, T> {};
4046
template <typename T>
4147
struct is_tuple : is_template_instant_of<std::tuple, T> {};
4248

49+
#if __cplusplus > 201703L
50+
#if __has_include(<span>)
51+
template <typename>
52+
struct is_span : std::false_type {};
53+
54+
template <typename T, size_t Extent>
55+
struct is_span<std::span<T, Extent>> : std::true_type {};
56+
#endif
57+
#endif
58+
4359
template <class T>
4460
struct is_sequence_container
4561
: std::integral_constant<

include/ylt/standalone/iguana/json_reader.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end);
2525
template <typename U, typename It, std::enable_if_t<smart_ptr_v<U>, int> = 0>
2626
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end);
2727

28+
template <typename U, typename It, std::enable_if_t<variant_v<U>, int> = 0>
29+
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end);
30+
2831
template <typename U, typename It,
2932
std::enable_if_t<ylt_refletable_v<U>, int> = 0>
3033
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
@@ -262,10 +265,22 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
262265
}
263266
}
264267

268+
template <typename T>
269+
constexpr size_t get_array_size(const T &t) {
270+
#if __cplusplus > 201703L
271+
#if __has_include(<span>)
272+
if constexpr (is_span<T>::value)
273+
return t.size();
274+
else
275+
#endif
276+
#endif
277+
return sizeof(T) / sizeof(decltype(std::declval<T>()[0]));
278+
}
279+
265280
template <typename U, typename It, std::enable_if_t<fixed_array_v<U>, int> = 0>
266281
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
267282
using T = std::remove_reference_t<U>;
268-
constexpr auto n = sizeof(T) / sizeof(decltype(std::declval<T>()[0]));
283+
size_t n = get_array_size(value);
269284
skip_ws(it, end);
270285

271286
if constexpr (std::is_same_v<char, std::remove_reference_t<
@@ -546,7 +561,7 @@ IGUANA_INLINE void from_json_variant(U &value, It &it, It &end,
546561
end = temp_end;
547562
}
548563

549-
template <typename U, typename It, std::enable_if_t<variant_v<U>, int> = 0>
564+
template <typename U, typename It, std::enable_if_t<variant_v<U>, int>>
550565
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
551566
from_json_variant(value, it, end,
552567
std::make_index_sequence<

include/ylt/standalone/iguana/pb_util.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ template <uint64_t v, typename Writer, size_t... I>
197197
IGUANA_INLINE void append_varint_u32(Writer& writer,
198198
std::index_sequence<I...>) {
199199
uint8_t temp = 0;
200-
((temp = static_cast<uint8_t>(v >> (7 * I)), writer.write(&temp, 1)), ...);
200+
((temp = static_cast<uint8_t>(v >> (7 * I)),
201+
writer.write((const char*)&temp, 1)),
202+
...);
201203
}
202204

203205
template <uint32_t v, typename Writer>

include/ylt/standalone/iguana/util.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ template <typename T>
8989
constexpr inline bool array_v = is_array<std::remove_cvref_t<T>>::value;
9090

9191
template <typename Type>
92-
constexpr inline bool fixed_array_v = c_array_v<Type> || array_v<Type>;
92+
constexpr inline bool fixed_array_v = c_array_v<Type> ||
93+
#if __cplusplus > 201703L
94+
#if __has_include(<span>)
95+
is_span<Type>::value ||
96+
#endif
97+
#endif
98+
array_v<Type>;
9399

94100
template <typename T>
95101
constexpr inline bool string_view_v =

src/struct_json/examples/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ void test_inner_object() {
4141
iguana::from_json(obj1, str);
4242
assert(obj1.get_id() == 20);
4343
assert(obj1.get_name() == "tom");
44+
45+
#if __cplusplus > 201703L
46+
#if __has_include(<span>)
47+
{
48+
std::vector<int> v{1, 2};
49+
std::span<int> span(v.data(), v.data() + 2);
50+
std::string str;
51+
iguana::to_json(span, str);
52+
53+
std::vector<int> v1;
54+
v1.resize(2);
55+
std::span<int> span1(v1.data(), v1.data() + 2);
56+
57+
iguana::from_json(span1, str);
58+
assert(v == v1);
59+
}
60+
#endif
61+
#endif
4462
}
4563

4664
struct person1 {
@@ -63,13 +81,15 @@ void use_smart_pointer() {
6381
}
6482

6583
void test_escape_serialize() {
84+
#ifdef __linux__
6685
person p{"\t", 20};
6786
std::string ss;
6887
struct_json::to_json(p, ss);
6988
std::cout << ss << std::endl;
7089
person p1;
7190
struct_json::from_json(p1, ss);
7291
assert(p1.name == p.name);
92+
#endif
7393
}
7494

7595
int main() {

0 commit comments

Comments
 (0)