libQuotient
A Qt library for building matrix clients
ranges_extras.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ranges>
4 
5 namespace Quotient {
6 
7 //! Same as std::projected but Proj is checked against the reference under the iterator
8 template <std::indirectly_readable IterT,
11 
12 //! \brief Find a value in a container of (smart) pointers
13 //!
14 //! This is a replica of std::ranges::find that automatically applies dereferencing projection
15 //! before applying the provided projection. Quotient has a few containers with pointers or wrappers
16 //! for other types - think of timeline items or `event_ptr_tt<>`s. This is meant to streamline
17 //! searching for events that match a specific simple criterion; e.g., to find an event with a given
18 //! id in a container you can now write `findIndirect(events, eventId, &RoomEvent::id);` instead
19 //! of having to supply your own lambda to dereference the timeline item and check the event id.
20 template <std::input_iterator IterT, typename ValT, typename Proj = std::identity>
23 // Most of constraints here (including IndirectlyProjected) are based on the definition of
24 // std::ranges::find and things around it
25 inline constexpr auto findIndirect(IterT from, IterT to, const ValT& value, Proj proj = {})
26 {
27  return std::ranges::find(from, to, value, [p = std::move(proj)](auto& itemPtr) {
28  return std::invoke(p, *itemPtr);
29  });
30 }
31 
32 //! The overload of findIndirect for ranges
33 template <typename RangeT, typename ValT, typename Proj = std::identity>
36  const ValT*>
37 inline constexpr auto findIndirect(RangeT&& range, const ValT& value, Proj proj = {})
38 {
40 }
41 
42 }