TermOx
transform_view.hpp
1 #ifndef TERMOX_COMMON_TRANFORM_VIEW_HPP
2 #define TERMOX_COMMON_TRANFORM_VIEW_HPP
3 #include <cstddef>
4 #include <type_traits>
5 #include <utility>
6 
7 #include <termox/common/transform_iterator.hpp>
8 
9 namespace ox {
10 
12 
15 template <typename Container, typename Map_fn>
17  public:
18  using Reference =
19  std::invoke_result_t<Map_fn, typename Container::reference>;
20  using Reference_const =
21  std::invoke_result_t<Map_fn, typename Container::const_reference>;
22 
23  public:
24  Transform_view(Container& c, Map_fn map_fn)
25  : container_{c}, map_fn_{std::move(map_fn)}
26  {}
27 
28  public:
29  [[nodiscard]] auto size() const -> std::size_t { return container_.size(); }
30 
31  [[nodiscard]] auto is_empty() const -> bool { return container_.empty(); }
32 
33  [[nodiscard]] auto operator[](std::size_t i) const -> Reference_const
34  {
35  return map_fn_(container_[i]);
36  }
37 
38  [[nodiscard]] auto operator[](std::size_t i) -> Reference
39  {
40  return map_fn_(container_[i]);
41  }
42 
43  [[nodiscard]] auto front() const -> Reference_const
44  {
45  return map_fn_(container_.front());
46  }
47 
48  [[nodiscard]] auto front() -> Reference
49  {
50  return map_fn_(container_.front());
51  }
52 
53  [[nodiscard]] auto back() const -> Reference_const
54  {
55  return map_fn_(container_.back());
56  }
57 
58  [[nodiscard]] auto back() -> Reference
59  {
60  return map_fn_(container_.back());
61  }
62 
63  [[nodiscard]] auto begin() const
64  {
65  return Transform_iterator{std::cbegin(container_), map_fn_};
66  }
67 
68  [[nodiscard]] auto begin()
69  {
70  return Transform_iterator{std::begin(container_), map_fn_};
71  }
72 
73  [[nodiscard]] auto end() const
74  {
75  return Transform_iterator{std::cend(container_), map_fn_};
76  }
77 
78  [[nodiscard]] auto end()
79  {
80  return Transform_iterator{std::end(container_), map_fn_};
81  }
82 
83  private:
84  Container& container_;
85  Map_fn map_fn_;
86 };
87 
88 } // namespace ox
89 #endif // TERMOX_COMMON_TRANFORM_VIEW_HPP
operator*() will apply map to the result of the underlying deref result.
Definition: transform_iterator.hpp:15
Read only Container view that applies transformation to elements at access.
Definition: transform_view.hpp:16