| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef API_ARRAY_VIEW_H_ |
| 12 |
#define API_ARRAY_VIEW_H_ |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <array> |
| 16 |
#include <iterator> |
| 17 |
#include <type_traits> |
| 18 |
|
| 19 |
#include "rtc_base/checks.h" |
| 20 |
#include "rtc_base/type_traits.h" |
| 21 |
|
| 22 |
namespace rtc { |
| 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 |
namespace impl { |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
enum : std::ptrdiff_t { kArrayViewVarSize = -4711 }; |
| 92 |
|
| 93 |
|
| 94 |
template <typename T, std::ptrdiff_t Size> |
| 95 |
class ArrayViewBase { |
| 96 |
static_assert(Size > 0, "ArrayView size must be variable or non-negative"); |
| 97 |
|
| 98 |
public: |
| 99 |
ArrayViewBase(T* data, size_t size) : data_(data) {} |
| 100 |
|
| 101 |
static constexpr size_t size() { return Size; } |
| 102 |
static constexpr bool empty() { return false; } |
| 103 |
T* data() const { return data_; } |
| 104 |
|
| 105 |
protected: |
| 106 |
static constexpr bool fixed_size() { return true; } |
| 107 |
|
| 108 |
private: |
| 109 |
T* data_; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
template <typename T> |
| 114 |
class ArrayViewBase<T, 0> { |
| 115 |
public: |
| 116 |
explicit ArrayViewBase(T* data, size_t size) {} |
| 117 |
|
| 118 |
static constexpr size_t size() { return 0; } |
| 119 |
static constexpr bool empty() { return true; } |
| 120 |
T* data() const { return nullptr; } |
| 121 |
|
| 122 |
protected: |
| 123 |
static constexpr bool fixed_size() { return true; } |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
template <typename T> |
| 128 |
class ArrayViewBase<T, impl::kArrayViewVarSize> { |
| 129 |
public: |
| 130 |
ArrayViewBase(T* data, size_t size) |
| 131 |
: data_(size == 0 ? nullptr : data), size_(size) {} |
| 132 |
|
| 133 |
size_t size() const { return size_; } |
| 134 |
bool empty() const { return size_ == 0; } |
| 135 |
T* data() const { return data_; } |
| 136 |
|
| 137 |
protected: |
| 138 |
static constexpr bool fixed_size() { return false; } |
| 139 |
|
| 140 |
private: |
| 141 |
T* data_; |
| 142 |
size_t size_; |
| 143 |
}; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
template <typename T, std::ptrdiff_t Size = impl::kArrayViewVarSize> |
| 148 |
class ArrayView final : public impl::ArrayViewBase<T, Size> { |
| 149 |
public: |
| 150 |
using value_type = T; |
| 151 |
using const_iterator = const T*; |
| 152 |
|
| 153 |
|
| 154 |
template <typename U> |
| 155 |
ArrayView(U* data, size_t size) |
| 156 |
: impl::ArrayViewBase<T, Size>::ArrayViewBase(data, size) { |
| 157 |
RTC_DCHECK_EQ(size == 0 ? nullptr : data, this->data()); |
| 158 |
RTC_DCHECK_EQ(size, this->size()); |
| 159 |
RTC_DCHECK_EQ(!this->data(), |
| 160 |
this->size() == 0); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
ArrayView() : ArrayView(nullptr, 0) {} |
| 166 |
ArrayView(std::nullptr_t) |
| 167 |
: ArrayView() {} |
| 168 |
ArrayView(std::nullptr_t, size_t size) |
| 169 |
: ArrayView(static_cast<T*>(nullptr), size) { |
| 170 |
static_assert(Size == 0 || Size == impl::kArrayViewVarSize, ""); |
| 171 |
RTC_DCHECK_EQ(0, size); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
template <typename U, size_t N> |
| 176 |
ArrayView(U (&array)[N]) |
| 177 |
: ArrayView(array, N) { |
| 178 |
static_assert(Size == N || Size == impl::kArrayViewVarSize, |
| 179 |
"Array size must match ArrayView size"); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
template <typename U, |
| 186 |
size_t N, |
| 187 |
typename std::enable_if< |
| 188 |
Size == static_cast<std::ptrdiff_t>(N)>::type* = nullptr> |
| 189 |
ArrayView(std::array<U, N>& u) |
| 190 |
: ArrayView(u.data(), u.size()) {} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
template <typename U, |
| 196 |
size_t N, |
| 197 |
typename std::enable_if< |
| 198 |
Size == static_cast<std::ptrdiff_t>(N)>::type* = nullptr> |
| 199 |
ArrayView(const std::array<U, N>& u) |
| 200 |
: ArrayView(u.data(), u.size()) {} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
template < |
| 210 |
typename U, |
| 211 |
typename std::enable_if<Size != impl::kArrayViewVarSize && |
| 212 |
HasDataAndSize<U, T>::value>::type* = nullptr> |
| 213 |
ArrayView(U& u) |
| 214 |
: ArrayView(u.data(), u.size()) { |
| 215 |
static_assert(U::size() == Size, "Sizes must match exactly"); |
| 216 |
} |
| 217 |
template < |
| 218 |
typename U, |
| 219 |
typename std::enable_if<Size != impl::kArrayViewVarSize && |
| 220 |
HasDataAndSize<U, T>::value>::type* = nullptr> |
| 221 |
ArrayView(const U& u) |
| 222 |
: ArrayView(u.data(), u.size()) { |
| 223 |
static_assert(U::size() == Size, "Sizes must match exactly"); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
template < |
| 238 |
typename U, |
| 239 |
typename std::enable_if<Size == impl::kArrayViewVarSize && |
| 240 |
HasDataAndSize<U, T>::value>::type* = nullptr> |
| 241 |
ArrayView(U& u) |
| 242 |
: ArrayView(u.data(), u.size()) {} |
| 243 |
template < |
| 244 |
typename U, |
| 245 |
typename std::enable_if<Size == impl::kArrayViewVarSize && |
| 246 |
HasDataAndSize<U, T>::value>::type* = nullptr> |
| 247 |
ArrayView(const U& u) |
| 248 |
: ArrayView(u.data(), u.size()) {} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
T& operator[](size_t idx) const { |
| 254 |
RTC_DCHECK_LT(idx, this->size()); |
| 255 |
RTC_DCHECK(this->data()); |
| 256 |
return this->data()[idx]; |
| 257 |
} |
| 258 |
T* begin() const { return this->data(); } |
| 259 |
T* end() const { return this->data() + this->size(); } |
| 260 |
const T* cbegin() const { return this->data(); } |
| 261 |
const T* cend() const { return this->data() + this->size(); } |
| 262 |
std::reverse_iterator<T*> rbegin() const { |
| 263 |
return std::make_reverse_iterator(end()); |
| 264 |
} |
| 265 |
std::reverse_iterator<T*> rend() const { |
| 266 |
return std::make_reverse_iterator(begin()); |
| 267 |
} |
| 268 |
std::reverse_iterator<const T*> crbegin() const { |
| 269 |
return std::make_reverse_iterator(cend()); |
| 270 |
} |
| 271 |
std::reverse_iterator<const T*> crend() const { |
| 272 |
return std::make_reverse_iterator(cbegin()); |
| 273 |
} |
| 274 |
|
| 275 |
ArrayView<T> subview(size_t offset, size_t size) const { |
| 276 |
return offset < this->size() |
| 277 |
? ArrayView<T>(this->data() + offset, |
| 278 |
std::min(size, this->size() - offset)) |
| 279 |
: ArrayView<T>(); |
| 280 |
} |
| 281 |
ArrayView<T> subview(size_t offset) const { |
| 282 |
return subview(offset, this->size()); |
| 283 |
} |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
template <typename T, std::ptrdiff_t Size1, std::ptrdiff_t Size2> |
| 289 |
bool operator==(const ArrayView<T, Size1>& a, const ArrayView<T, Size2>& b) { |
| 290 |
return a.data() == b.data() && a.size() == b.size(); |
| 291 |
} |
| 292 |
template <typename T, std::ptrdiff_t Size1, std::ptrdiff_t Size2> |
| 293 |
bool operator!=(const ArrayView<T, Size1>& a, const ArrayView<T, Size2>& b) { |
| 294 |
return !(a == b); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
static_assert(sizeof(ArrayView<int>) == 2 * sizeof(int*), ""); |
| 301 |
static_assert(sizeof(ArrayView<int, 17>) == sizeof(int*), ""); |
| 302 |
static_assert(std::is_empty<ArrayView<int, 0>>::value, ""); |
| 303 |
|
| 304 |
template <typename T> |
| 305 |
inline ArrayView<T> MakeArrayView(T* data, size_t size) { |
| 306 |
return ArrayView<T>(data, size); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
template <typename U, typename T, std::ptrdiff_t Size> |
| 315 |
inline ArrayView<U, Size> reinterpret_array_view(ArrayView<T, Size> view) { |
| 316 |
static_assert(sizeof(U) == sizeof(T) && alignof(U) == alignof(T), |
| 317 |
"ArrayView reinterpret_cast is only supported for casting " |
| 318 |
"between views that represent the same chunk of memory."); |
| 319 |
static_assert( |
| 320 |
std::is_fundamental<T>::value && std::is_fundamental<U>::value, |
| 321 |
"ArrayView reinterpret_cast is only supported for casting between " |
| 322 |
"fundamental types."); |
| 323 |
return ArrayView<U, Size>(reinterpret_cast<U*>(view.data()), view.size()); |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
#endif |