OGLplus  (0.59.0) a C++ wrapper for rendering APIs

wrapping_container.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_WRAPPING_CONTAINER_HPP
10 #define EAGINE_WRAPPING_CONTAINER_HPP
11 
12 #include "iterator.hpp"
13 #include "range_types.hpp"
14 #include "span.hpp"
15 #include "types.hpp"
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
20 template <
21  typename Container,
22  typename Wrapper,
23  typename T,
24  T initial = T{},
25  typename Transform = typename Wrapper::transform>
26 class basic_wrapping_container : private Transform {
27 public:
28  using wrapper_type = Wrapper;
29 
32  typename Container::const_iterator,
33  Wrapper,
34  T,
35  Transform>;
36 
38  constexpr explicit basic_wrapping_container() noexcept {
39  _initialize(_items);
40  }
41 
43  constexpr explicit basic_wrapping_container(
44  typename Container::size_type count) {
45  _items.resize(count);
46  _initialize(_items);
47  }
48 
51  : _items{temp._release_items()} {}
52 
55  default;
56 
58  auto operator=(basic_wrapping_container&& temp) noexcept
60  using std::swap;
61 
62  auto items{temp._release_items()};
63  swap(items, _items);
64  return *this;
65  }
66 
68  constexpr auto operator=(const basic_wrapping_container&)
69  -> basic_wrapping_container& = default;
70 
71  ~basic_wrapping_container() noexcept = default;
72 
74  constexpr operator basic_wrapping_container<
75  span<T>,
76  Wrapper,
77  T,
78  initial,
79  Transform>() noexcept {
80  return {cover(_items)};
81  }
82 
84  constexpr operator basic_wrapping_container<
85  span<const T>,
86  Wrapper,
87  T,
88  initial,
89  Transform>() noexcept {
90  return {view(_items)};
91  }
92 
95  constexpr auto empty() const noexcept -> bool {
96  return _items.empty();
97  }
98 
101  constexpr auto size() const noexcept {
102  return span_size(_items.size());
103  }
104 
107  constexpr auto operator[](span_size_t index) const noexcept {
108  return _transf()(_items[range_index<Container>(index)]);
109  }
110 
112  constexpr auto at(span_size_t index) const {
113  return _transf()(_items.at(range_index<Container>(index)));
114  }
115 
117  constexpr auto begin() const noexcept -> const_iterator {
118  return {_items.begin()};
119  }
120 
122  constexpr auto end() const noexcept -> const_iterator {
123  return {_items.end()};
124  }
125 
127  constexpr auto raw_items() noexcept {
128  if constexpr(std::is_const_v<typename Container::value_type>) {
129  return view(_items);
130  } else {
131  return cover(_items);
132  }
133  }
134 
136  constexpr auto raw_items() const noexcept {
137  return view(_items);
138  }
139 
140 private:
141  constexpr auto _transf() const noexcept -> const Transform& {
142  return *this;
143  }
144 
145  Container _items{};
146 
147  static constexpr void _initialize(Container& items) {
148  using std::begin;
149  using std::end;
150  using std::fill;
151  fill(begin(items), end(items), initial);
152  }
153 
154  auto _release_items() noexcept {
155  Container items{std::move(_items)};
156  _intialize(_items);
157  return items;
158  }
159 };
160 //------------------------------------------------------------------------------
161 } // namespace eagine
162 
163 #endif // EAGINE_WRAPPING_CONTAINER_HPP
constexpr auto at(span_size_t index) const
Access the element at the specified index.
Definition: wrapping_container.hpp:112
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
constexpr auto end() const noexcept -> const_iterator
Returns an iterator past the last element in this container.
Definition: wrapping_container.hpp:122
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
constexpr auto empty() const noexcept -> bool
Indicates if this container is empty.
Definition: wrapping_container.hpp:95
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
basic_span< T, T *, S > span
Default alias for basic memory spans with native pointer type.
Definition: span.hpp:415
constexpr auto begin() const noexcept -> const_iterator
Returns an iterator to the first element in this container.
Definition: wrapping_container.hpp:117
static auto fill(basic_span< T, P, S > spn, const V &v) -> basic_span< T, P, S >
Fills a span with copies of the specified value.
Definition: span_algo.hpp:536
constexpr basic_wrapping_container(typename Container::size_type count)
Resizing constructor.
Definition: wrapping_container.hpp:43
constexpr basic_wrapping_container() noexcept
Default constructor.
Definition: wrapping_container.hpp:38
static auto transform(basic_span< T, P, S > spn, Transform function) -> basic_span< T, P, S >
Transforms the elements of a span with a function.
Definition: span_algo.hpp:572
constexpr auto operator[](span_size_t index) const noexcept
Subscript access.
Definition: wrapping_container.hpp:107
constexpr auto raw_items() noexcept
Returns view of the elements as they are stored, without transfomation.
Definition: wrapping_container.hpp:127
constexpr auto size() const noexcept
Returns the number of elements in this container.
Definition: wrapping_container.hpp:101
auto operator=(basic_wrapping_container &&temp) noexcept -> basic_wrapping_container &
Move assignable.
Definition: wrapping_container.hpp:58
basic_wrapping_container(basic_wrapping_container &&temp) noexcept
Move constructible.
Definition: wrapping_container.hpp:50
Template used in implementation of containers with wrapped elements.
Definition: wrapping_container.hpp:26
constexpr auto raw_items() const noexcept
Returns view of the elements as they are stored, without transfomation.
Definition: wrapping_container.hpp:136
Default instantiation of basic_transforming_iterator.
Definition: iterator.hpp:270

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).