Pyrogenesis  trunk
Span.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_PS_SPAN
19 #define INCLUDED_PS_SPAN
20 
21 #include <cstddef>
22 #include <iterator>
23 #include <type_traits>
24 
25 namespace PS
26 {
27 
28 /**
29  * Simplifed version of std::span (C++20) as we don't support the original one
30  * yet. The naming intentionally follows the STL version to make the future
31  * replacement easier with less blame changing.
32  * It supports only very basic subset of std::span functionality.
33  * TODO: remove as soon as std::span become available.
34  */
35 template<typename T>
36 class span
37 {
38 public:
39  using element_type = T;
40  using value_type = std::remove_cv_t<T>;
41  using size_type = size_t;
42  using pointer = T*;
43  using reference = T&;
44  using iterator = pointer;
45 
46  constexpr span()
47  : m_Pointer(nullptr), m_Extent(0) {}
48 
49  constexpr span(iterator first, size_type extent)
50  : m_Pointer(first), m_Extent(extent) {}
51 
52  constexpr span(iterator first, iterator last)
53  : m_Pointer(first), m_Extent(static_cast<size_type>(last - first)) {}
54 
55  template<typename OtherT, size_t N>
56  constexpr span(const std::array<OtherT, N>& arr)
57  : m_Pointer(arr.data()), m_Extent(arr.size()) {}
58 
59  constexpr span(const span& other) = default;
60 
61  constexpr span& operator=(const span& other) = default;
62 
63  ~span() = default;
64 
65  constexpr size_type size() const { return m_Extent; }
66  constexpr bool empty() const { return size() == 0; }
67  constexpr reference operator[](size_type index) const { return *(m_Pointer + index); }
68  constexpr pointer data() const { return m_Pointer; }
69 
70  constexpr iterator begin() const { return m_Pointer; }
71  constexpr iterator end() const { return m_Pointer + m_Extent; }
72 
73  constexpr span subspan(size_type offset) const { return {m_Pointer + offset, m_Extent - offset}; }
74 
75 private:
78 };
79 
80 template<typename T, size_t N>
81 span(const std::array<T, N>&) -> span<const T>;
82 
83 } // namespace PS
84 
85 #endif // INCLUDED_PS_SPAN
constexpr span(iterator first, size_type extent)
Definition: Span.h:49
~span()=default
pointer m_Pointer
Definition: Span.h:76
constexpr span(iterator first, iterator last)
Definition: Span.h:52
constexpr reference operator[](size_type index) const
Definition: Span.h:67
constexpr span & operator=(const span &other)=default
constexpr iterator begin() const
Definition: Span.h:70
constexpr bool empty() const
Definition: Span.h:66
Renderer::Backend::Vulkan::CTexture *const & reference
Definition: Span.h:43
std::remove_cv_t< Renderer::Backend::Vulkan::CTexture *const > value_type
Definition: Span.h:40
#define T(string_literal)
Definition: secure_crt.cpp:77
constexpr pointer data() const
Definition: Span.h:68
constexpr span()
Definition: Span.h:46
Definition: Span.h:25
constexpr span(const std::array< OtherT, N > &arr)
Definition: Span.h:56
Renderer::Backend::Vulkan::CTexture *const * pointer
Definition: Span.h:42
size_type m_Extent
Definition: Span.h:77
constexpr span subspan(size_type offset) const
Definition: Span.h:73
constexpr iterator end() const
Definition: Span.h:71
constexpr size_type size() const
Definition: Span.h:65
Renderer::Backend::Vulkan::CTexture *const element_type
Definition: Span.h:39
Simplifed version of std::span (C++20) as we don&#39;t support the original one yet.
Definition: Span.h:36