SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_spanRef.h
1#ifndef SDL3PP_SPAN_REF_H_
2#define SDL3PP_SPAN_REF_H_
3
4#include <concepts>
5#include <ranges>
6#include <span>
7
8namespace SDL {
9
10template<class T, class BASE>
11concept DerivedWrapper =
12 std::derived_from<T, BASE> && sizeof(T) == sizeof(BASE);
13
22template<class T>
24{
25 std::span<T> value;
26
27public:
28 constexpr SpanRef() = default;
29
31 template<DerivedWrapper<T> U, size_t N>
32 constexpr SpanRef(U (&other)[N])
33 : value(static_cast<T*>(other), N)
34 {
35 }
36
38 template<DerivedWrapper<T> U>
39 constexpr SpanRef(const std::span<U>& other)
40 : value(other.data(), other.size())
41 {
42 }
43
45 template<std::contiguous_iterator It>
46 requires DerivedWrapper<std::iter_value_t<It>, T>
47 constexpr SpanRef(It first, size_t count)
48 : value((T*)(&*first), count)
49 {
50 }
51
53 template<std::contiguous_iterator It, std::sized_sentinel_for<It> End>
54 requires DerivedWrapper<std::iter_value_t<It>, T>
55 constexpr SpanRef(It first, End last)
56 : value((T*)(&*first), size_t(last - first))
57 {
58 }
59
61 template<std::ranges::contiguous_range R>
62 requires DerivedWrapper<std::iter_value_t<std::ranges::iterator_t<R>>, T>
63 constexpr SpanRef(R&& range)
64 : SpanRef(std::begin(range), std::end(range))
65 {
66 }
67
69 constexpr size_t size() const { return value.size(); }
70
72 constexpr size_t size_bytes() const { return value.size_bytes(); }
73
75 constexpr T* data() const { return value.data(); }
76};
77
78} // namespace SDL
79
80#endif /* SDL3PP_SPAN_REF_H_ */
span-like for empty-derived structs
Definition: SDL3pp_spanRef.h:24
constexpr SpanRef(It first, End last)
Constructor.
Definition: SDL3pp_spanRef.h:55
constexpr SpanRef(U(&other)[N])
Constructor.
Definition: SDL3pp_spanRef.h:32
constexpr T * data() const
Retrieves contained data.
Definition: SDL3pp_spanRef.h:75
constexpr SpanRef(It first, size_t count)
Constructor.
Definition: SDL3pp_spanRef.h:47
constexpr SpanRef(const std::span< U > &other)
Constructor.
Definition: SDL3pp_spanRef.h:39
constexpr SpanRef(R &&range)
Constructor.
Definition: SDL3pp_spanRef.h:63
constexpr size_t size() const
Retrieves contained size.
Definition: SDL3pp_spanRef.h:69
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_spanRef.h:72
Main include header for the SDL3pp library.