|
28 | 28 | #ifndef SRC_DAWN_COMMON_ITYP_SPAN_H_
|
29 | 29 | #define SRC_DAWN_COMMON_ITYP_SPAN_H_
|
30 | 30 |
|
| 31 | +#include <limits> |
| 32 | +#include <span> |
| 33 | + |
31 | 34 | #include "dawn/common/TypedInteger.h"
|
32 | 35 | #include "dawn/common/UnderlyingType.h"
|
33 | 36 |
|
34 | 37 | namespace dawn::ityp {
|
35 | 38 |
|
36 |
| -// ityp::span is a helper class that wraps an unowned packed array of type |Value|. |
37 |
| -// It stores the size and pointer to first element. It has the restriction that |
38 |
| -// indices must be a particular type |Index|. This provides a type-safe way to index |
39 |
| -// raw pointers. |
| 39 | +// ityp::span is a helper class that wraps std::span<T, std::dynamic_extent> |
| 40 | +// with the restriction that indices must be a particular type |Index|. |
40 | 41 | template <typename Index, typename Value>
|
41 |
| -class span { |
| 42 | +class span : private ::std::span<Value> { |
42 | 43 | using I = UnderlyingType<Index>;
|
| 44 | + using Base = ::std::span<Value>; |
43 | 45 |
|
44 | 46 | public:
|
45 |
| - constexpr span() : mData(nullptr), mSize(0) {} |
46 |
| - constexpr span(Value* data, Index size) : mData(data), mSize(size) {} |
47 |
| - |
48 |
| - constexpr Value& operator[](Index i) const { |
49 |
| - DAWN_CHECK(i < mSize); |
50 |
| - return mData[static_cast<I>(i)]; |
51 |
| - } |
52 |
| - |
53 |
| - Value* data() noexcept { return mData; } |
54 |
| - |
55 |
| - const Value* data() const noexcept { return mData; } |
56 |
| - |
57 |
| - Value* begin() noexcept { return mData; } |
58 |
| - |
59 |
| - const Value* begin() const noexcept { return mData; } |
| 47 | + constexpr span() = default; |
| 48 | + constexpr span(Value* data, Index size) : Base{data, static_cast<I>(size)} {} |
60 | 49 |
|
61 |
| - Value* end() noexcept { return mData + static_cast<I>(mSize); } |
| 50 | + constexpr Value& operator[](Index i) const { return Base::operator[](static_cast<I>(i)); } |
62 | 51 |
|
63 |
| - const Value* end() const noexcept { return mData + static_cast<I>(mSize); } |
64 |
| - |
65 |
| - Value& front() { |
66 |
| - DAWN_ASSERT(mData != nullptr); |
67 |
| - DAWN_ASSERT(static_cast<I>(mSize) >= 0); |
68 |
| - return *mData; |
69 |
| - } |
70 |
| - |
71 |
| - const Value& front() const { |
72 |
| - DAWN_ASSERT(mData != nullptr); |
73 |
| - DAWN_ASSERT(static_cast<I>(mSize) >= 0); |
74 |
| - return *mData; |
| 52 | + constexpr Index size() const { |
| 53 | + DAWN_ASSERT(std::numeric_limits<I>::max() >= Base::size()); |
| 54 | + return Index(static_cast<I>(Base::size())); |
75 | 55 | }
|
76 | 56 |
|
77 |
| - Value& back() { |
78 |
| - DAWN_ASSERT(mData != nullptr); |
79 |
| - DAWN_ASSERT(static_cast<I>(mSize) >= 0); |
80 |
| - return *(mData + static_cast<I>(mSize) - 1); |
81 |
| - } |
82 |
| - |
83 |
| - const Value& back() const { |
84 |
| - DAWN_ASSERT(mData != nullptr); |
85 |
| - DAWN_ASSERT(static_cast<I>(mSize) >= 0); |
86 |
| - return *(mData + static_cast<I>(mSize) - 1); |
87 |
| - } |
| 57 | + using Base::data; |
88 | 58 |
|
89 |
| - Index size() const { return mSize; } |
| 59 | + using Base::begin; |
| 60 | + using Base::end; |
90 | 61 |
|
91 |
| - private: |
92 |
| - Value* mData; |
93 |
| - Index mSize; |
| 62 | + using Base::back; |
| 63 | + using Base::front; |
94 | 64 | };
|
95 | 65 |
|
96 | 66 | // ityp::SpanFromUntyped<Index>(myValues, myValueCount) creates a span<Index, Value> from a C-style
|
|
0 commit comments