Debian Codegraph @

include/private/base/SkSpan_impl.h

raw
1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkSpan_DEFINED
9 #define SkSpan_DEFINED
10
11 /*
12 * Define this to keep the bespoke SkSpan implementation, until clients are ready.
13 * When this is not defined, SkSpan becomes just an alias for std::span
14 */
15 #define SK_USE_LEGACY_SKSPAN
16
17 /*
18 * SkSpan handles single POD initialization differently from std::span
19 * This macro allows clients to initialize a span with a single POD that will work with
20 * either implementation of SkSpan.
21 *
22 * When we switch to std::span entirely, callsites can remove this macro, and juse type {{a}}
23 */
24 #ifdef SK_USE_LEGACY_SKSPAN
25 #define SKSPAN_INIT_ONE(elem) {elem}
26 #else
27 #define SKSPAN_INIT_ONE(elem) {{elem}}
28 #endif
29
30 #ifdef SK_USE_LEGACY_SKSPAN
31
32 #include "include/private/base/SkAssert.h"
33 #include "include/private/base/SkDebug.h"
34 #include "include/private/base/SkTo.h"
35
36 #include <cstddef>
37 #include <initializer_list>
38 #include <iterator>
39 #include <limits>
40 #include <utility>
41
42 // Having this be an export works around IWYU churn related to
43 // https://github.com/include-what-you-use/include-what-you-use/issues/1121
44 #include <type_traits> // IWYU pragma: export
45
46 // Add macro to check the lifetime of initializer_list arguments. initializer_list has a very
47 // short life span, and can only be used as a parameter, and not as a variable.
48 #if defined(__clang__) && defined(__has_cpp_attribute) && __has_cpp_attribute(clang::lifetimebound)
49 #define SK_CHECK_IL_LIFETIME [[clang::lifetimebound]]
50 #else
51 #define SK_CHECK_IL_LIFETIME
52 #endif
53
54 /**
55 * SkSpan holds a reference to contiguous data of type T along with a count. SkSpan does not own
56 * the data itself but is merely a reference, therefore you must take care with the lifetime of
57 * the underlying data.
58 *
59 * SkSpan is a count and a pointer into existing array or data type that stores its data in
60 * contiguous memory like std::vector. Any container that works with std::size() and std::data()
61 * can be used.
62 *
63 * SkSpan makes a convenient parameter for a routine to accept array like things. This allows you to
64 * write the routine without overloads for all different container types.
65 *
66 * Example:
67 * void routine(SkSpan<const int> a) { ... }
68 *
69 * std::vector v = {1, 2, 3, 4, 5};
70 *
71 * routine(a);
72 *
73 * A word of caution when working with initializer_list, initializer_lists have a lifetime that is
74 * limited to the current statement. The following is correct and safe:
75 *
76 * Example:
77 * routine({1,2,3,4,5});
78 *
79 * The following is undefined, and will result in erratic execution:
80 *
81 * Bad Example:
82 * initializer_list l = {1, 2, 3, 4, 5}; // The data behind l dies at the ;.
83 * routine(l);
84 */
85 template <typename T>
86 class SkSpan {
87 public:
88 constexpr SkSpan() : fPtr{nullptr}, fSize{0} {}
89
90 template <typename Integer, std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
91 constexpr SkSpan(T* ptr, Integer size) : fPtr{ptr}, fSize{SkToSizeT(size)} {
92 SkASSERT(ptr || fSize == 0); // disallow nullptr + a nonzero size
93 SkASSERT(fSize < (std::numeric_limits<size_t>::max() / sizeof(T)));
94 }
95 template <typename U, typename = std::enable_if_t<std::is_same_v<const U, T>>>
96 constexpr SkSpan(const SkSpan<U>& that) : fPtr(std::data(that)), fSize(std::size(that)) {}
97 constexpr SkSpan(const SkSpan& o) = default;
98 template<size_t N> constexpr SkSpan(T(&a)[N]) : SkSpan(a, N) { }
99 template<typename Container>
100 constexpr SkSpan(Container&& c) : SkSpan(std::data(c), std::size(c)) { }
101 SkSpan(std::initializer_list<T> il SK_CHECK_IL_LIFETIME)
102 : SkSpan(std::data(il), std::size(il)) {}
103
104 constexpr SkSpan& operator=(const SkSpan& that) = default;
105
106 constexpr T& operator [] (size_t i) const {
107 return fPtr[sk_collection_check_bounds(i, this->size())];
108 }
109 constexpr T& front() const { sk_collection_not_empty(this->empty()); return fPtr[0]; }
110 constexpr T& back() const { sk_collection_not_empty(this->empty()); return fPtr[fSize - 1]; }
111 constexpr T* begin() const { return fPtr; }
112 constexpr T* end() const { return fPtr + fSize; }
113 constexpr auto rbegin() const { return std::make_reverse_iterator(this->end()); }
114 constexpr auto rend() const { return std::make_reverse_iterator(this->begin()); }
115 constexpr T* data() const { return this->begin(); }
116 constexpr size_t size() const { return fSize; }
117 constexpr bool empty() const { return fSize == 0; }
118 constexpr size_t size_bytes() const { return fSize * sizeof(T); }
119 constexpr SkSpan<T> first(size_t prefixLen) const {
120 return SkSpan{fPtr, sk_collection_check_length(prefixLen, fSize)};
121 }
122 constexpr SkSpan<T> last(size_t postfixLen) const {
123 return SkSpan{fPtr + (this->size() - postfixLen),
124 sk_collection_check_length(postfixLen, fSize)};
125 }
126 constexpr SkSpan<T> subspan(size_t offset) const {
127 return this->subspan(offset, this->size() - offset);
128 }
129 constexpr SkSpan<T> subspan(size_t offset, size_t count) const {
130 const size_t safeOffset = sk_collection_check_length(offset, fSize);
131
132 // Should read offset + count > size(), but that could overflow. We know that safeOffset
133 // is <= size, therefore the subtraction will not overflow.
134 if (count > this->size() - safeOffset) SK_UNLIKELY {
135 // The count is too large.
136 SkUNREACHABLE;
137 }
138 return SkSpan{fPtr + safeOffset, count};
139 }
140
141 private:
142 T* fPtr;
143 size_t fSize;
144 };
145
146 template <typename Container>
147 SkSpan(Container&&) ->
148 SkSpan<std::remove_pointer_t<decltype(std::data(std::declval<Container>()))>>;
149
150 #else
151
152 #include <span>
153
154 template <typename T> using SkSpan = std::span<T>;
155
156 #endif // legacy
157
158 #endif // SkSpan_DEFINED