Skip to content

Commit

Permalink
support span
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Nov 27, 2024
1 parent 1f99cb2 commit f58f206
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 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
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);
std::string str;
iguana::to_json(span, str);

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

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

0 comments on commit f58f206

Please sign in to comment.