Made std::span and std::string_view "RangeAdapterType"s so they can be passed by copy. Also added functions to pipe different iterable functions into each other (WIP).

This commit is contained in:
Patrick 2023-07-12 23:08:20 +02:00
parent ef5d78e1b1
commit adff35a26a

View File

@ -4,6 +4,8 @@
#define MIJIN_UTIL_ITERATORS_HPP_INCLUDED 1
#include <cstddef>
#include <span>
#include <string_view>
#include <tuple>
#include <variant>
@ -26,8 +28,25 @@ struct RangeRef
return std::end(range);
}
};
template<typename T>
concept RangeAdapterType = std::is_base_of_v<RangeAdapter, T>;
struct is_range_adapter_type : std::false_type {};
template<typename T> requires std::is_base_of_v<RangeAdapter, T>
struct is_range_adapter_type<T> : std::true_type {};
// allow passing in spans and string_views by value
template<typename T, std::size_t Extent>
struct is_range_adapter_type<std::span<T, Extent>> : std::true_type {};
template<typename CharT, typename Traits>
struct is_range_adapter_type<std::basic_string_view<CharT, Traits>> : std::true_type {};
template<typename T>
concept RangeAdapterType = is_range_adapter_type<T>::value;
static_assert(!is_range_adapter_type<std::tuple<int, int>>::value);
static_assert(is_range_adapter_type<RangeAdapter>::value);
template<RangeAdapterType T>
struct RangeRef<T>
@ -346,6 +365,41 @@ auto chain(TFirstRange&& firstRange, TSecondRange&& secondRange, TMoreRanges&&..
{
return chain(std::forward<TFirstRange>(firstRange), chain(std::forward<TSecondRange>(secondRange), std::forward<TMoreRanges>(moreRanges)...));
}
template<typename TAs, typename TIterable>
TAs collect(TIterable&& iterable)
{
return TAs(iterable.begin(), iterable.end());
}
namespace pipe
{
template<typename T>
struct Replace
{
T what;
T with;
Replace(T what_, T with_) : what(std::move(what_)), with(std::move(with_)) {}
};
template<typename T>
Replace(T, T) -> Replace<T>;
template<typename TIterable>
auto operator|(TIterable&& iterable, Replace<typename std::iterator_traits<decltype(std::begin(std::declval<TIterable>()))>::value_type> repl)
{
return replace(std::forward<TIterable>(iterable), std::move(repl.what), std::move(repl.with));
}
template<typename T>
struct Collect {};
template<typename TIterable, typename T>
auto operator|(TIterable&& iterable, Collect<T>)
{
return collect<T>(std::forward<TIterable>(iterable));
}
}
} // namespace mijin
#endif // MIJIN_UTIL_ITERATORS_HPP_INCLUDED