1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#pragma once
#include "asl/meta.hpp"
#include "asl/annotations.hpp"
#include "asl/layout.hpp"
#include "asl/assert.hpp"
namespace asl
{
static constexpr int64_t dynamic_size = -1;
template<is_object T, int64_t kSize = dynamic_size>
class span
{
static constexpr bool kIsDynamic = kSize < 0;
using SizeType = select_t<kIsDynamic, int64_t, empty>;
T* m_data{};
ASL_NO_UNIQUE_ADDRESS SizeType m_size{};
public:
constexpr span() = default;
constexpr span(T* data, int64_t size)
requires kIsDynamic
: m_data{data}
, m_size{size}
{}
constexpr explicit span(T* data, int64_t size)
requires (!kIsDynamic)
: m_data{data}
{
ASL_ASSERT(size == kSize);
}
template<int64_t N>
constexpr span(T (&array)[N]) // NOLINT(*-explicit-conversions)
requires (kIsDynamic)
: m_data{array}
, m_size{N}
{}
template<int64_t N>
constexpr span(T (&array)[N]) // NOLINT(*-explicit-conversions)
requires (!kIsDynamic) && (N == kSize)
: m_data{array}
{}
template<is_object U, int64_t kOtherSize>
constexpr explicit(!kIsDynamic)
span(const span<U, kOtherSize>& other) // NOLINT(*-explicit-conversions)
requires (
(
kIsDynamic ||
span<U, kOtherSize>::kIsDynamic ||
kOtherSize == kSize
) && convertible_from<T(&)[], U(&)[]>
)
: span{static_cast<U*>(other.data()), other.size()}
{
}
constexpr span(const span&) = default;
constexpr span(span&&) = default;
constexpr span& operator=(const span&) = default;
constexpr span& operator=(span&&) = default;
~span() = default;
constexpr int64_t size() const
{
if constexpr (kIsDynamic) { return m_size; }
else { return kSize; }
}
constexpr int64_t size_bytes() const { return size() * size_of<T>; }
constexpr bool is_empty() const { return size() == 0; }
constexpr T* data() const { return m_data; }
constexpr T* begin() const { return m_data; }
constexpr T* end() const { return m_data + size(); }
constexpr T& operator[](int64_t i) const
{
ASL_ASSERT(i >= 0 && i < size());
return m_data[i]; // NOLINT(*-pointer-arithmetic)
}
// @Todo subspan, first, last
// @Todo as_(mutable_)bytes
template<is_object U, int64_t kOtherSize>
friend class span;
};
} // namespace asl
|