Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: utils::range_value_t #29

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@

namespace json
{
namespace utils
{
template <typename T>
using iterator_t = decltype(std::declval<T&>().begin());
zzyyyl marked this conversation as resolved.
Show resolved Hide resolved
template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
template <typename T>
using iter_value_t = typename std::iterator_traits<remove_cvref_t<T>>::value_type;
template <typename R>
using range_value_t = iter_value_t<iterator_t<R>>;
}
template <typename string_t>
class basic_value;
template <typename string_t>
Expand Down Expand Up @@ -273,8 +284,7 @@ class basic_array
explicit basic_array(basic_value<string_t>&& val);

template <typename collection_t,
typename _ = std::enable_if_t<std::is_constructible_v<
value_type, typename std::iterator_traits<typename collection_t::iterator>::value_type>>>
typename = std::enable_if_t<std::is_constructible_v<value_type, utils::range_value_t<collection_t>>>>
basic_array(collection_t arr)
: _array_data(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()))
{}
Expand Down Expand Up @@ -384,8 +394,8 @@ class basic_object
basic_object(std::initializer_list<value_type> init_list);
explicit basic_object(const basic_value<string_t>& val);
explicit basic_object(basic_value<string_t>&& val);
template <typename map_t, typename _ = std::enable_if_t<std::is_constructible_v<
value_type, typename std::iterator_traits<typename map_t::iterator>::value_type>>>
template <typename map_t,
typename = std::enable_if_t<std::is_constructible_v<value_type, utils::range_value_t<map_t>>>>
basic_object(map_t map) : _object_data(std::make_move_iterator(map.begin()), std::make_move_iterator(map.end()))
{}

Expand Down Expand Up @@ -2534,20 +2544,16 @@ namespace _serialization_helper
static constexpr bool value = decltype(test<T>(0))::value;
};

template <typename...>
using void_t = void;

template <typename T, typename = void>
constexpr bool is_container = false;
template <typename T>
constexpr bool is_container<T, void_t<typename T::value_type, typename T::iterator,
typename std::iterator_traits<typename T::iterator>::value_type>> =
std::is_same_v<typename T::value_type, typename std::iterator_traits<typename T::iterator>::value_type>;
constexpr bool is_container<T, std::void_t<typename T::value_type, utils::range_value_t<T>>> =
std::is_same_v<typename T::value_type, utils::range_value_t<T>>;

template <typename T, typename = void>
constexpr bool is_map = false;
template <typename T>
constexpr bool is_map<T, void_t<typename T::key_type, typename T::mapped_type>> = is_container<T>;
constexpr bool is_map<T, std::void_t<typename T::key_type, typename T::mapped_type>> = is_container<T>;

template <typename T, typename = void>
constexpr bool is_collection = false;
Expand Down