Hash :
0cfea380
Author :
Date :
2025-01-15T10:46:54
Rename sh::TSpan as general purpose angle::Span Span abstraction is useful for making buffer manipulation more consistent. The commit makes the Span available to all code until std::span can be used. Bug: angleproject:389951202 Change-Id: Id0c6b54bb6e75d3cc4e85af854d9e61b66906752 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6170997 Reviewed-by: Geoff Lang <geofflang@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
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 103 104 105 106 107 108 109 110 111 112
//
// Copyright 2025 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Span.h:
// Basic implementation of C++20's span.
//
#ifndef COMMON_SPAN_H_
#define COMMON_SPAN_H_
#include <type_traits>
#include "common/log_utils.h"
namespace angle
{
// Basic implementation of C++20's span.
// See the reference for std::span here: https://en.cppreference.com/w/cpp/container/span
template <typename T>
class Span
{
public:
typedef size_t size_type;
constexpr Span() = default;
constexpr Span(T *ptr, size_type size) : mData(ptr), mSize(size) {}
template <typename V,
typename = std::enable_if_t<std::remove_reference_t<V>::is_pool_allocated>>
constexpr Span(V &&vec) : mData(vec.data()), mSize(vec.size())
{}
template <typename V,
typename = std::enable_if_t<std::remove_reference_t<V>::is_pool_allocated>>
constexpr Span &operator=(V &&vec)
{
mData = vec.data();
mSize = vec.size();
return *this;
}
constexpr bool operator==(const Span &that) const
{
if (mSize != that.mSize)
{
return false;
}
if (mData == that.mData)
{
return true;
}
for (size_type index = 0; index < mSize; ++index)
{
if (mData[index] != that.mData[index])
{
return false;
}
}
return true;
}
constexpr bool operator!=(const Span &that) const { return !(*this == that); }
constexpr T *data() const { return mData; }
constexpr size_type size() const { return mSize; }
constexpr bool empty() const { return mSize == 0; }
constexpr T &operator[](size_type index) const { return mData[index]; }
constexpr T &front() const { return mData[0]; }
constexpr T &back() const { return mData[mSize - 1]; }
constexpr T *begin() const { return mData; }
constexpr T *end() const { return mData + mSize; }
constexpr std::reverse_iterator<T *> rbegin() const
{
return std::make_reverse_iterator(end());
}
constexpr std::reverse_iterator<T *> rend() const
{
return std::make_reverse_iterator(begin());
}
constexpr Span first(size_type count) const
{
ASSERT(count <= mSize);
return count == 0 ? Span() : Span(mData, count);
}
constexpr Span last(size_type count) const
{
ASSERT(count <= mSize);
return count == 0 ? Span() : Span(mData + mSize - count, count);
}
constexpr Span subspan(size_type offset, size_type count) const
{
ASSERT(offset + count <= mSize);
return count == 0 ? Span() : Span(mData + offset, count);
}
private:
T *mData = nullptr;
size_t mSize = 0;
};
} // namespace angle
#endif // COMMON_SPAN_