Skip to content

Commit

Permalink
Merge pull request #333 from qicosmos/support_span
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Nov 27, 2024
2 parents 1f99cb2 + c738195 commit 18bf96b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
16 changes: 16 additions & 0 deletions iguana/detail/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#include <variant>
#include <vector>

#if __cplusplus > 201703L
#if __has_include(<span>)
#include <span>
#endif
#endif

#include "iguana/define.h"

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

#if __cplusplus > 201703L
#if __has_include(<span>)
template <typename>
struct is_span : std::false_type {};

template <typename T, size_t Extent>
struct is_span<std::span<T, Extent>> : std::true_type {};
#endif
#endif

template <class T>
struct is_sequence_container
: std::integral_constant<
Expand Down
14 changes: 13 additions & 1 deletion iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,22 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
}
}

template <typename T>
constexpr size_t get_array_size(const T &t) {
#if __cplusplus > 201703L
#if __has_include(<span>)
if constexpr (is_span<T>::value)
return t.size();
else
#endif
#endif
return sizeof(T) / sizeof(decltype(std::declval<T>()[0]));
}

template <typename U, typename It, std::enable_if_t<fixed_array_v<U>, int> = 0>
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
using T = std::remove_reference_t<U>;
constexpr auto n = sizeof(T) / sizeof(decltype(std::declval<T>()[0]));
size_t n = get_array_size(value);
skip_ws(it, end);

if constexpr (std::is_same_v<char, std::remove_reference_t<
Expand Down
4 changes: 3 additions & 1 deletion iguana/pb_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ template <uint64_t v, typename Writer, size_t... I>
IGUANA_INLINE void append_varint_u32(Writer& writer,
std::index_sequence<I...>) {
uint8_t temp = 0;
((temp = static_cast<uint8_t>(v >> (7 * I)), writer.write(&temp, 1)), ...);
((temp = static_cast<uint8_t>(v >> (7 * I)),
writer.write((const char*)&temp, 1)),
...);
}

template <uint32_t v, typename Writer>
Expand Down
8 changes: 7 additions & 1 deletion iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ template <typename T>
constexpr inline bool array_v = is_array<std::remove_cvref_t<T>>::value;

template <typename Type>
constexpr inline bool fixed_array_v = c_array_v<Type> || array_v<Type>;
constexpr inline bool fixed_array_v = c_array_v<Type> ||
#if __cplusplus > 201703L
#if __has_include(<span>)
is_span<Type>::value ||
#endif
#endif
array_v<Type>;

template <typename T>
constexpr inline bool string_view_v =
Expand Down
17 changes: 17 additions & 0 deletions test/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ TEST_CASE("test parse item array_t") {
CHECK(test[0] == 1);
CHECK(test[1] == -222);
}
#if __cplusplus > 201703L
#if __has_include(<span>)
{
std::vector<int> v{1, 2};
std::span<int> span(v.data(), v.data() + 2);
std::string str;
iguana::to_json(span, str);

std::vector<int> v1;
v1.resize(2);
std::span<int> span1(v1.data(), v1.data() + 2);

iguana::from_json(span1, str);
assert(v == v1);
}
#endif
#endif
{
std::string str{"[1, -222,"};
std::array<int, 2> test;
Expand Down

0 comments on commit 18bf96b

Please sign in to comment.