Pyrogenesis trunk
Span.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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
25namespace 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 */
35template<typename T>
36class span
37{
38public:
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&;
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 template<typename ContinuousRange>
60 constexpr span(ContinuousRange& range)
61 : m_Pointer(range.data()), m_Extent(range.size()) {}
62
63 constexpr span(const span& other) = default;
64
65 constexpr span& operator=(const span& other) = default;
66
67 ~span() = default;
68
69 constexpr size_type size() const { return m_Extent; }
70 constexpr bool empty() const { return size() == 0; }
71 constexpr reference operator[](size_type index) const { return *(m_Pointer + index); }
72 constexpr pointer data() const { return m_Pointer; }
73
74 constexpr iterator begin() const { return m_Pointer; }
75 constexpr iterator end() const { return m_Pointer + m_Extent; }
76
77 constexpr span subspan(size_type offset) const { return {m_Pointer + offset, m_Extent - offset}; }
78
79private:
82};
83
84template<typename T, size_t N>
85span(const std::array<T, N>&) -> span<const T>;
86
87} // namespace PS
88
89#endif // INCLUDED_PS_SPAN
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
constexpr span(ContinuousRange &range)
Definition: Span.h:60
constexpr bool empty() const
Definition: Span.h:70
constexpr reference operator[](size_type index) const
Definition: Span.h:71
size_t size_type
Definition: Span.h:41
constexpr span(const std::array< OtherT, N > &arr)
Definition: Span.h:56
constexpr span(const span &other)=default
pointer iterator
Definition: Span.h:44
constexpr span subspan(size_type offset) const
Definition: Span.h:77
constexpr span(iterator first, iterator last)
Definition: Span.h:52
T * pointer
Definition: Span.h:42
constexpr span()
Definition: Span.h:46
T & reference
Definition: Span.h:43
constexpr iterator end() const
Definition: Span.h:75
size_type m_Extent
Definition: Span.h:81
constexpr iterator begin() const
Definition: Span.h:74
constexpr span(iterator first, size_type extent)
Definition: Span.h:49
constexpr pointer data() const
Definition: Span.h:72
std::remove_cv_t< T > value_type
Definition: Span.h:40
pointer m_Pointer
Definition: Span.h:80
constexpr size_type size() const
Definition: Span.h:69
~span()=default
constexpr span & operator=(const span &other)=default
T element_type
Definition: Span.h:39
Definition: NetEnet.cpp:26
span(const std::array< T, N > &) -> span< const T >
#define T(string_literal)
Definition: secure_crt.cpp:77