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

span.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SPAN_HPP
10 #define EAGINE_SPAN_HPP
11 
12 #include "memory/span.hpp"
13 #include "valid_if/decl.hpp"
14 #include <iosfwd>
15 
16 namespace eagine {
17 //------------------------------------------------------------------------------
18 using memory::span;
19 //------------------------------------------------------------------------------
20 using memory::cover;
21 using memory::cover_one;
22 using memory::view;
23 using memory::view_one;
24 //------------------------------------------------------------------------------
29 template <typename T, typename P, typename S, typename Output>
30 static inline auto list_to_stream(Output& out, memory::basic_span<T, P, S> s)
31  -> Output& {
32  out << '[';
33  bool first = true;
34  for(const auto& e : s) {
35  if(first) {
36  first = false;
37  } else {
38  out << ',';
39  }
40  out << e;
41  }
42  return out << ']';
43 }
44 //------------------------------------------------------------------------------
49 template <typename Input, typename T, typename P, typename S>
50 static inline auto read_from_stream(Input& in, memory::basic_span<T, P, S> s)
51  -> auto& {
52  return in.read(
53  reinterpret_cast<char*>(s.data()), limit_cast<std::streamsize>(s.size()));
54 }
55 //------------------------------------------------------------------------------
61 template <typename Output, typename T, typename P, typename S>
62 static inline auto write_to_stream(Output& out, memory::basic_span<T, P, S> s)
63  -> auto& {
64  return out.write(
65  reinterpret_cast<const char*>(s.data()),
66  limit_cast<std::streamsize>(s.size()));
67 }
68 //------------------------------------------------------------------------------
73 template <typename T, typename P, typename S>
74 static inline auto operator<<(std::ostream& out, memory::basic_span<T, P, S> s)
75  -> std::
76  enable_if_t<!std::is_same_v<std::remove_const_t<T>, char>, std::ostream&> {
77  return list_to_stream(out, s);
78 }
79 //------------------------------------------------------------------------------
84 template <typename T, typename P, typename S>
85 static inline auto operator<<(std::ostream& out, memory::basic_span<T, P, S> s)
86  -> std::
87  enable_if_t<std::is_same_v<std::remove_const_t<T>, char>, std::ostream&> {
88  return write_to_stream(out, absolute(s));
89 }
90 //------------------------------------------------------------------------------
98 template <typename T, typename P, typename S>
99 static inline auto
101  return [&i, spn]() -> optionally_valid<std::remove_const_t<T>> {
102  if(i < spn.size()) {
103  return {spn[i++], true};
104  }
105  return {};
106  };
107 }
108 //------------------------------------------------------------------------------
109 template <typename Src>
110 static inline auto make_span_getter(span_size_t& i, const Src& src) {
111  return make_span_getter(i, view(src));
112 }
113 //------------------------------------------------------------------------------
122 template <typename T, typename P, typename S, typename Transform>
123 static inline auto make_span_getter(
124  span_size_t& i,
126  Transform transform) {
127  return [&i, spn, transform]() -> decltype(transform(std::declval<T>())) {
128  if(i < spn.size()) {
129  return transform(spn[i++]);
130  }
131  return {};
132  };
133 }
134 //------------------------------------------------------------------------------
135 template <typename Src, typename Transform>
136 static inline auto
137 make_span_getter(span_size_t& i, const Src& src, Transform transform) {
138  return make_span_getter(i, view(src), std::move(transform));
139 }
140 //------------------------------------------------------------------------------
147 template <typename T, typename P, typename S>
148 static inline auto
150  return [&i, spn](auto value) mutable -> bool {
151  if(i < spn.size()) {
152  spn[i++] = T(std::move(value));
153  return true;
154  }
155  return false;
156  };
157 }
158 //------------------------------------------------------------------------------
159 template <typename Dst>
160 static inline auto make_span_putter(span_size_t& o, Dst& dst) {
161  return make_span_putter(o, cover(dst));
162 }
163 //------------------------------------------------------------------------------
171 template <typename T, typename P, typename S, typename Transform>
172 static inline auto make_span_putter(
173  span_size_t& i,
175  Transform transform) {
176  return [&i, spn, transform](auto value) mutable -> bool {
177  if(i < spn.size()) {
178  if(auto transformed = transform(value)) {
179  spn[i++] = T(std::move(extract(transformed)));
180  return true;
181  }
182  }
183  return false;
184  };
185 }
186 //------------------------------------------------------------------------------
187 template <typename Dst, typename Transform>
188 static inline auto
189 make_span_putter(span_size_t& o, Dst& dst, Transform transform) {
190  return make_span_putter(o, cover(dst), std::move(transform));
191 }
192 //------------------------------------------------------------------------------
193 } // namespace eagine
194 
195 #endif // EAGINE_SPAN_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Common code is placed in this namespace.
Definition: eagine.hpp:21
static auto make_span_putter(span_size_t &i, memory::basic_span< T, P, S > spn, Transform transform)
Makes a callable setting consecutive elements of a span starting at i.
Definition: span.hpp:172
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
static auto make_span_getter(span_size_t &i, memory::basic_span< T, P, S > spn)
Makes a callable that returns consecutive span elements starting at i.
Definition: span.hpp:100
static auto make_span_getter(span_size_t &i, memory::basic_span< T, P, S > spn, Transform transform)
Makes a callable getting consecutive, transformed span elements starting at i.
Definition: span.hpp:123
Primary template for conditionally valid values.
Definition: decl.hpp:49
static auto write_to_stream(Output &out, memory::basic_span< T, P, S > s) -> auto &
Helper function for raw-reading spans into output streams.
Definition: span.hpp:62
basic_span< T, T *, S > span
Default alias for basic memory spans with native pointer type.
Definition: span.hpp:415
static constexpr auto cover_one(T *pointer) noexcept -> span_if_mutable< T >
Creates a single-element mutable span from the specified pointer.
Definition: span.hpp:451
static constexpr auto view_one(const T *pointer) noexcept -> const_span< T >
Creates a single-const-element view from the specified pointer.
Definition: span.hpp:444
static constexpr auto view(const C &container) noexcept
Creates a const view over a compatible contiguous container.
Definition: span.hpp:513
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
static auto operator<<(std::ostream &out, memory::basic_span< T, P, S > s) -> std::enable_if_t<!std::is_same_v< std::remove_const_t< T >, char >, std::ostream & >
Operator for printing generic element spans into output streams.
Definition: span.hpp:74
static auto read_from_stream(Input &in, memory::basic_span< T, P, S > s) -> auto &
Helper function for raw-reading spans from output streams.
Definition: span.hpp:50
static auto operator<<(std::ostream &out, memory::basic_span< T, P, S > s) -> std::enable_if_t< std::is_same_v< std::remove_const_t< T >, char >, std::ostream & >
Operator for printing spans of string characters into output streams.
Definition: span.hpp:85
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the span.
Definition: span.hpp:246
static constexpr auto cover(C &container) noexcept
Creates a mutable span covering a compatible contiguous container.
Definition: span.hpp:523
static auto list_to_stream(Output &out, memory::basic_span< T, P, S > s) -> Output &
Helper function for pretty-printing spans as lists into output streams.
Definition: span.hpp:30
static auto make_span_putter(span_size_t &i, memory::basic_span< T, P, S > spn)
Makes a callable setting consecutive elements of a span starting at i.
Definition: span.hpp:149
static constexpr auto absolute(basic_span< T, P, S > spn) noexcept -> basic_span< T, T *, S >
Converts argument to span using a native pointer type.
Definition: span.hpp:407

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