Horizon
Loading...
Searching...
No Matches
range_util.hpp
1#pragma once
2#include "common/common.hpp"
3#include "canvas/selectables.hpp"
4#include <range/v3/view.hpp>
6
7namespace horizon {
8inline auto sel_filter_type(ObjectType type)
9{
10 return ranges::views::filter([type](const SelectableRef &x) { return x.type == type; });
11}
12
13template <typename Tk, typename Tv> auto map_ptr_from_sel(std::map<Tk, Tv> &m)
14{
15 return [&m](const SelectableRef &x) { return &m.at(x.uuid); };
16}
17
18
19template <typename Tk, typename Tv> auto map_ref_from_sel(std::map<Tk, Tv> &m)
20{
21 return ranges::views::transform([&m](const SelectableRef &x) -> Tv & { return m.at(x.uuid); });
22}
23
24template <typename Tr, typename Tp> auto find_if_ptr(Tr &&range, Tp pred)
25{
26 auto x = ranges::find_if(range, pred);
27 if (x == range.end())
28 return static_cast<decltype(&*x)>(nullptr);
29 else
30 return &*x;
31}
32} // namespace horizon