libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
ranges_extras.h
Go to the documentation of this file.
1#pragma once
2
3#include <ranges>
4
5namespace Quotient {
6
7//! \brief An indexOf() alternative for any range
8//!
9//! Unlike QList::indexOf(), returns `range.size()` if \p value is not found
10template <typename RangeT, typename ValT, typename ProjT = std::identity>
13inline auto findIndex(const RangeT& range, const ValT& value, ProjT proj = {})
14{
15 using namespace std::ranges;
17}
18
19//! \brief A replacement of std::ranges::to() while toolchains catch up
20//!
21//! Returns a container of type \p TargetT created from \p sourceRange. Unlike std::ranges::to(),
22//! you have to pass the range to it (e.g. `rangeTo<TargetT>(someRange)`); using it in a pipeline
23//! (`someRange | rangeTo<TargetT>()`) won't compile. Internally calls std::ranges::to() if it's
24//! available; otherwise, returns the result of calling
25//! `TargetT(ranges::begin(sourceRange), ranges::end(sourceRange))`.
26template <class TargetT, typename SourceT>
27[[nodiscard]] constexpr auto rangeTo(SourceT&& sourceRange)
28{
29#if defined(__cpp_lib_ranges_to_container)
30 return std::ranges::to<TargetT>(std::forward<SourceT>(sourceRange));
31#else
32 using std::begin, std::end;
33 return TargetT(begin(sourceRange), end(sourceRange));
34#endif
35}
36
37//! An overload that accepts unspecialised container template
38template <template <typename> class TargetT, typename SourceT>
39[[nodiscard]] constexpr auto rangeTo(SourceT&& sourceRange)
40{
41 // Avoid template argument deduction because Xcode still can't do it when TargetT is an alias
42#if defined(__cpp_lib_ranges_to_container)
43 return std::ranges::to<TargetT<std::ranges::range_value_t<SourceT>>>(
44 std::forward<SourceT>(sourceRange));
45#else
46 using std::begin, std::end;
47 return TargetT<std::ranges::range_value_t<SourceT>>(begin(sourceRange), end(sourceRange));
48#endif
49}
50
51}
constexpr auto rangeTo(SourceT &&sourceRange)
A replacement of std::ranges::to() while toolchains catch up.