Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef FLATBUFFERS_STL_EMULATION_H_ |
| 18 | #define FLATBUFFERS_STL_EMULATION_H_ |
| 19 | |
| 20 | // clang-format off |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 21 | #include "flatbuffers/base.h" |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 22 | |
| 23 | #include <string> |
| 24 | #include <type_traits> |
| 25 | #include <vector> |
| 26 | #include <memory> |
| 27 | #include <limits> |
| 28 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 29 | #ifndef FLATBUFFERS_USE_STD_OPTIONAL |
| 30 | // Detect C++17 compatible compiler. |
| 31 | // __cplusplus >= 201703L - a compiler has support of 'static inline' variables. |
| 32 | #if (defined(__cplusplus) && __cplusplus >= 201703L) \ |
| 33 | || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) |
| 34 | #define FLATBUFFERS_USE_STD_OPTIONAL 1 |
| 35 | #else |
| 36 | #define FLATBUFFERS_USE_STD_OPTIONAL 0 |
| 37 | #endif // (defined(__cplusplus) && __cplusplus >= 201703L) ... |
| 38 | #endif // FLATBUFFERS_USE_STD_OPTIONAL |
| 39 | |
| 40 | #if FLATBUFFERS_USE_STD_OPTIONAL |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 41 | #include <optional> |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 42 | #endif |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 43 | |
| 44 | // The __cpp_lib_span is the predefined feature macro. |
| 45 | #if defined(FLATBUFFERS_USE_STD_SPAN) |
| 46 | #include <span> |
| 47 | #elif defined(__cpp_lib_span) && defined(__has_include) |
| 48 | #if __has_include(<span>) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 49 | #include <array> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 50 | #include <span> |
| 51 | #define FLATBUFFERS_USE_STD_SPAN |
| 52 | #endif |
| 53 | #else |
| 54 | // Disable non-trivial ctors if FLATBUFFERS_SPAN_MINIMAL defined. |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 55 | #if !defined(FLATBUFFERS_TEMPLATES_ALIASES) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 56 | #define FLATBUFFERS_SPAN_MINIMAL |
| 57 | #else |
| 58 | // Enable implicit construction of a span<T,N> from a std::array<T,N>. |
| 59 | #include <array> |
| 60 | #endif |
| 61 | #endif // defined(FLATBUFFERS_USE_STD_SPAN) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 62 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 63 | // This header provides backwards compatibility for older versions of the STL. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 64 | namespace flatbuffers { |
| 65 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 66 | #if defined(FLATBUFFERS_TEMPLATES_ALIASES) |
| 67 | template <typename T> |
| 68 | using numeric_limits = std::numeric_limits<T>; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 69 | #else |
| 70 | template <typename T> class numeric_limits : |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 71 | public std::numeric_limits<T> {}; |
| 72 | #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 73 | |
| 74 | #if defined(FLATBUFFERS_TEMPLATES_ALIASES) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 75 | template <typename T> using is_scalar = std::is_scalar<T>; |
| 76 | template <typename T, typename U> using is_same = std::is_same<T,U>; |
| 77 | template <typename T> using is_floating_point = std::is_floating_point<T>; |
| 78 | template <typename T> using is_unsigned = std::is_unsigned<T>; |
| 79 | template <typename T> using is_enum = std::is_enum<T>; |
| 80 | template <typename T> using make_unsigned = std::make_unsigned<T>; |
| 81 | template<bool B, class T, class F> |
| 82 | using conditional = std::conditional<B, T, F>; |
| 83 | template<class T, T v> |
| 84 | using integral_constant = std::integral_constant<T, v>; |
| 85 | template <bool B> |
| 86 | using bool_constant = integral_constant<bool, B>; |
| 87 | using true_type = std::true_type; |
| 88 | using false_type = std::false_type; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 89 | #else |
| 90 | // MSVC 2010 doesn't support C++11 aliases. |
| 91 | template <typename T> struct is_scalar : public std::is_scalar<T> {}; |
| 92 | template <typename T, typename U> struct is_same : public std::is_same<T,U> {}; |
| 93 | template <typename T> struct is_floating_point : |
| 94 | public std::is_floating_point<T> {}; |
| 95 | template <typename T> struct is_unsigned : public std::is_unsigned<T> {}; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 96 | template <typename T> struct is_enum : public std::is_enum<T> {}; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 97 | template <typename T> struct make_unsigned : public std::make_unsigned<T> {}; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 98 | template<bool B, class T, class F> |
| 99 | struct conditional : public std::conditional<B, T, F> {}; |
| 100 | template<class T, T v> |
| 101 | struct integral_constant : public std::integral_constant<T, v> {}; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 102 | template <bool B> |
| 103 | struct bool_constant : public integral_constant<bool, B> {}; |
| 104 | typedef bool_constant<true> true_type; |
| 105 | typedef bool_constant<false> false_type; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 106 | #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) |
| 107 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 108 | #if defined(FLATBUFFERS_TEMPLATES_ALIASES) |
| 109 | template <class T> using unique_ptr = std::unique_ptr<T>; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 110 | #else |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 111 | // MSVC 2010 doesn't support C++11 aliases. |
| 112 | // We're manually "aliasing" the class here as we want to bring unique_ptr |
| 113 | // into the flatbuffers namespace. We have unique_ptr in the flatbuffers |
| 114 | // namespace we have a completely independent implementation (see below) |
| 115 | // for C++98 STL implementations. |
| 116 | template <class T> class unique_ptr : public std::unique_ptr<T> { |
| 117 | public: |
| 118 | unique_ptr() {} |
| 119 | explicit unique_ptr(T* p) : std::unique_ptr<T>(p) {} |
| 120 | unique_ptr(std::unique_ptr<T>&& u) { *this = std::move(u); } |
| 121 | unique_ptr(unique_ptr&& u) { *this = std::move(u); } |
| 122 | unique_ptr& operator=(std::unique_ptr<T>&& u) { |
| 123 | std::unique_ptr<T>::reset(u.release()); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 124 | return *this; |
| 125 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 126 | unique_ptr& operator=(unique_ptr&& u) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 127 | std::unique_ptr<T>::reset(u.release()); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 128 | return *this; |
| 129 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 130 | unique_ptr& operator=(T* p) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 131 | return std::unique_ptr<T>::operator=(p); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 132 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 133 | }; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 134 | #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 135 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 136 | #if FLATBUFFERS_USE_STD_OPTIONAL |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 137 | template<class T> |
| 138 | using Optional = std::optional<T>; |
| 139 | using nullopt_t = std::nullopt_t; |
| 140 | inline constexpr nullopt_t nullopt = std::nullopt; |
| 141 | |
| 142 | #else |
| 143 | // Limited implementation of Optional<T> type for a scalar T. |
| 144 | // This implementation limited by trivial types compatible with |
| 145 | // std::is_arithmetic<T> or std::is_enum<T> type traits. |
| 146 | |
| 147 | // A tag to indicate an empty flatbuffers::optional<T>. |
| 148 | struct nullopt_t { |
| 149 | explicit FLATBUFFERS_CONSTEXPR_CPP11 nullopt_t(int) {} |
| 150 | }; |
| 151 | |
| 152 | #if defined(FLATBUFFERS_CONSTEXPR_DEFINED) |
| 153 | namespace internal { |
| 154 | template <class> struct nullopt_holder { |
| 155 | static constexpr nullopt_t instance_ = nullopt_t(0); |
| 156 | }; |
| 157 | template<class Dummy> |
| 158 | constexpr nullopt_t nullopt_holder<Dummy>::instance_; |
| 159 | } |
| 160 | static constexpr const nullopt_t &nullopt = internal::nullopt_holder<void>::instance_; |
| 161 | |
| 162 | #else |
| 163 | namespace internal { |
| 164 | template <class> struct nullopt_holder { |
| 165 | static const nullopt_t instance_; |
| 166 | }; |
| 167 | template<class Dummy> |
| 168 | const nullopt_t nullopt_holder<Dummy>::instance_ = nullopt_t(0); |
| 169 | } |
| 170 | static const nullopt_t &nullopt = internal::nullopt_holder<void>::instance_; |
| 171 | |
| 172 | #endif |
| 173 | |
| 174 | template<class T> |
| 175 | class Optional FLATBUFFERS_FINAL_CLASS { |
| 176 | // Non-scalar 'T' would extremely complicated Optional<T>. |
| 177 | // Use is_scalar<T> checking because flatbuffers flatbuffers::is_arithmetic<T> |
| 178 | // isn't implemented. |
| 179 | static_assert(flatbuffers::is_scalar<T>::value, "unexpected type T"); |
| 180 | |
| 181 | public: |
| 182 | ~Optional() {} |
| 183 | |
| 184 | FLATBUFFERS_CONSTEXPR_CPP11 Optional() FLATBUFFERS_NOEXCEPT |
| 185 | : value_(), has_value_(false) {} |
| 186 | |
| 187 | FLATBUFFERS_CONSTEXPR_CPP11 Optional(nullopt_t) FLATBUFFERS_NOEXCEPT |
| 188 | : value_(), has_value_(false) {} |
| 189 | |
| 190 | FLATBUFFERS_CONSTEXPR_CPP11 Optional(T val) FLATBUFFERS_NOEXCEPT |
| 191 | : value_(val), has_value_(true) {} |
| 192 | |
| 193 | FLATBUFFERS_CONSTEXPR_CPP11 Optional(const Optional &other) FLATBUFFERS_NOEXCEPT |
| 194 | : value_(other.value_), has_value_(other.has_value_) {} |
| 195 | |
| 196 | FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(const Optional &other) FLATBUFFERS_NOEXCEPT { |
| 197 | value_ = other.value_; |
| 198 | has_value_ = other.has_value_; |
| 199 | return *this; |
| 200 | } |
| 201 | |
| 202 | FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(nullopt_t) FLATBUFFERS_NOEXCEPT { |
| 203 | value_ = T(); |
| 204 | has_value_ = false; |
| 205 | return *this; |
| 206 | } |
| 207 | |
| 208 | FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(T val) FLATBUFFERS_NOEXCEPT { |
| 209 | value_ = val; |
| 210 | has_value_ = true; |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | void reset() FLATBUFFERS_NOEXCEPT { |
| 215 | *this = nullopt; |
| 216 | } |
| 217 | |
| 218 | void swap(Optional &other) FLATBUFFERS_NOEXCEPT { |
| 219 | std::swap(value_, other.value_); |
| 220 | std::swap(has_value_, other.has_value_); |
| 221 | } |
| 222 | |
| 223 | FLATBUFFERS_CONSTEXPR_CPP11 FLATBUFFERS_EXPLICIT_CPP11 operator bool() const FLATBUFFERS_NOEXCEPT { |
| 224 | return has_value_; |
| 225 | } |
| 226 | |
| 227 | FLATBUFFERS_CONSTEXPR_CPP11 bool has_value() const FLATBUFFERS_NOEXCEPT { |
| 228 | return has_value_; |
| 229 | } |
| 230 | |
| 231 | FLATBUFFERS_CONSTEXPR_CPP11 const T& operator*() const FLATBUFFERS_NOEXCEPT { |
| 232 | return value_; |
| 233 | } |
| 234 | |
| 235 | const T& value() const { |
| 236 | FLATBUFFERS_ASSERT(has_value()); |
| 237 | return value_; |
| 238 | } |
| 239 | |
| 240 | T value_or(T default_value) const FLATBUFFERS_NOEXCEPT { |
| 241 | return has_value() ? value_ : default_value; |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | T value_; |
| 246 | bool has_value_; |
| 247 | }; |
| 248 | |
| 249 | template<class T> |
| 250 | FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& opt, nullopt_t) FLATBUFFERS_NOEXCEPT { |
| 251 | return !opt; |
| 252 | } |
| 253 | template<class T> |
| 254 | FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(nullopt_t, const Optional<T>& opt) FLATBUFFERS_NOEXCEPT { |
| 255 | return !opt; |
| 256 | } |
| 257 | |
| 258 | template<class T, class U> |
| 259 | FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const U& rhs) FLATBUFFERS_NOEXCEPT { |
| 260 | return static_cast<bool>(lhs) && (*lhs == rhs); |
| 261 | } |
| 262 | |
| 263 | template<class T, class U> |
| 264 | FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const T& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT { |
| 265 | return static_cast<bool>(rhs) && (lhs == *rhs); |
| 266 | } |
| 267 | |
| 268 | template<class T, class U> |
| 269 | FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT { |
| 270 | return static_cast<bool>(lhs) != static_cast<bool>(rhs) |
| 271 | ? false |
| 272 | : !static_cast<bool>(lhs) ? false : (*lhs == *rhs); |
| 273 | } |
| 274 | #endif // FLATBUFFERS_USE_STD_OPTIONAL |
| 275 | |
| 276 | |
| 277 | // Very limited and naive partial implementation of C++20 std::span<T,Extent>. |
| 278 | #if defined(FLATBUFFERS_USE_STD_SPAN) |
| 279 | inline constexpr std::size_t dynamic_extent = std::dynamic_extent; |
| 280 | template<class T, std::size_t Extent = std::dynamic_extent> |
| 281 | using span = std::span<T, Extent>; |
| 282 | |
| 283 | #else // !defined(FLATBUFFERS_USE_STD_SPAN) |
| 284 | FLATBUFFERS_CONSTEXPR std::size_t dynamic_extent = static_cast<std::size_t>(-1); |
| 285 | |
| 286 | // Exclude this code if MSVC2010 or non-STL Android is active. |
| 287 | // The non-STL Android doesn't have `std::is_convertible` required for SFINAE. |
| 288 | #if !defined(FLATBUFFERS_SPAN_MINIMAL) |
| 289 | namespace internal { |
| 290 | // This is SFINAE helper class for checking of a common condition: |
| 291 | // > This overload only participates in overload resolution |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 292 | // > Check whether a pointer to an array of From can be converted |
| 293 | // > to a pointer to an array of To. |
| 294 | // This helper is used for checking of 'From -> const From'. |
| 295 | template<class To, std::size_t Extent, class From, std::size_t N> |
| 296 | struct is_span_convertible { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 297 | using type = |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 298 | typename std::conditional<std::is_convertible<From (*)[], To (*)[]>::value |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 299 | && (Extent == dynamic_extent || N == Extent), |
| 300 | int, void>::type; |
| 301 | }; |
| 302 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 303 | template<typename T> |
| 304 | struct SpanIterator { |
| 305 | // TODO: upgrade to std::random_access_iterator_tag. |
| 306 | using iterator_category = std::forward_iterator_tag; |
| 307 | using difference_type = std::ptrdiff_t; |
| 308 | using value_type = typename std::remove_cv<T>::type; |
| 309 | using reference = T&; |
| 310 | using pointer = T*; |
| 311 | |
| 312 | // Convince MSVC compiler that this iterator is trusted (it is verified). |
| 313 | #ifdef _MSC_VER |
| 314 | using _Unchecked_type = pointer; |
| 315 | #endif // _MSC_VER |
| 316 | |
| 317 | SpanIterator(pointer ptr) : ptr_(ptr) {} |
| 318 | reference operator*() const { return *ptr_; } |
| 319 | pointer operator->() { return ptr_; } |
| 320 | SpanIterator& operator++() { ptr_++; return *this; } |
| 321 | SpanIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; } |
| 322 | |
| 323 | friend bool operator== (const SpanIterator& lhs, const SpanIterator& rhs) { return lhs.ptr_ == rhs.ptr_; } |
| 324 | friend bool operator!= (const SpanIterator& lhs, const SpanIterator& rhs) { return lhs.ptr_ != rhs.ptr_; } |
| 325 | |
| 326 | private: |
| 327 | pointer ptr_; |
| 328 | }; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 329 | } // namespace internal |
| 330 | #endif // !defined(FLATBUFFERS_SPAN_MINIMAL) |
| 331 | |
| 332 | // T - element type; must be a complete type that is not an abstract |
| 333 | // class type. |
| 334 | // Extent - the number of elements in the sequence, or dynamic. |
| 335 | template<class T, std::size_t Extent = dynamic_extent> |
| 336 | class span FLATBUFFERS_FINAL_CLASS { |
| 337 | public: |
| 338 | typedef T element_type; |
| 339 | typedef T& reference; |
| 340 | typedef const T& const_reference; |
| 341 | typedef T* pointer; |
| 342 | typedef const T* const_pointer; |
| 343 | typedef std::size_t size_type; |
| 344 | |
| 345 | static FLATBUFFERS_CONSTEXPR size_type extent = Extent; |
| 346 | |
| 347 | // Returns the number of elements in the span. |
| 348 | FLATBUFFERS_CONSTEXPR_CPP11 size_type size() const FLATBUFFERS_NOEXCEPT { |
| 349 | return count_; |
| 350 | } |
| 351 | |
| 352 | // Returns the size of the sequence in bytes. |
| 353 | FLATBUFFERS_CONSTEXPR_CPP11 |
| 354 | size_type size_bytes() const FLATBUFFERS_NOEXCEPT { |
| 355 | return size() * sizeof(element_type); |
| 356 | } |
| 357 | |
| 358 | // Checks if the span is empty. |
| 359 | FLATBUFFERS_CONSTEXPR_CPP11 bool empty() const FLATBUFFERS_NOEXCEPT { |
| 360 | return size() == 0; |
| 361 | } |
| 362 | |
| 363 | // Returns a pointer to the beginning of the sequence. |
| 364 | FLATBUFFERS_CONSTEXPR_CPP11 pointer data() const FLATBUFFERS_NOEXCEPT { |
| 365 | return data_; |
| 366 | } |
| 367 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 368 | #if !defined(FLATBUFFERS_SPAN_MINIMAL) |
| 369 | using Iterator = internal::SpanIterator<T>; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 370 | |
| 371 | Iterator begin() const { return Iterator(data()); } |
| 372 | Iterator end() const { return Iterator(data() + size()); } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 373 | #endif |
| 374 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 375 | // Returns a reference to the idx-th element of the sequence. |
| 376 | // The behavior is undefined if the idx is greater than or equal to size(). |
| 377 | FLATBUFFERS_CONSTEXPR_CPP11 reference operator[](size_type idx) const { |
| 378 | return data()[idx]; |
| 379 | } |
| 380 | |
| 381 | FLATBUFFERS_CONSTEXPR_CPP11 span(const span &other) FLATBUFFERS_NOEXCEPT |
| 382 | : data_(other.data_), count_(other.count_) {} |
| 383 | |
| 384 | FLATBUFFERS_CONSTEXPR_CPP14 span &operator=(const span &other) |
| 385 | FLATBUFFERS_NOEXCEPT { |
| 386 | data_ = other.data_; |
| 387 | count_ = other.count_; |
| 388 | } |
| 389 | |
| 390 | // Limited implementation of |
| 391 | // `template <class It> constexpr std::span(It first, size_type count);`. |
| 392 | // |
| 393 | // Constructs a span that is a view over the range [first, first + count); |
| 394 | // the resulting span has: data() == first and size() == count. |
| 395 | // The behavior is undefined if [first, first + count) is not a valid range, |
| 396 | // or if (extent != flatbuffers::dynamic_extent && count != extent). |
| 397 | FLATBUFFERS_CONSTEXPR_CPP11 |
| 398 | explicit span(pointer first, size_type count) FLATBUFFERS_NOEXCEPT |
| 399 | : data_ (Extent == dynamic_extent ? first : (Extent == count ? first : nullptr)), |
| 400 | count_(Extent == dynamic_extent ? count : (Extent == count ? Extent : 0)) { |
| 401 | // Make span empty if the count argument is incompatible with span<T,N>. |
| 402 | } |
| 403 | |
| 404 | // Exclude this code if MSVC2010 is active. The MSVC2010 isn't C++11 |
| 405 | // compliant, it doesn't support default template arguments for functions. |
| 406 | #if defined(FLATBUFFERS_SPAN_MINIMAL) |
| 407 | FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(nullptr), |
| 408 | count_(0) { |
| 409 | static_assert(extent == 0 || extent == dynamic_extent, "invalid span"); |
| 410 | } |
| 411 | |
| 412 | #else |
| 413 | // Constructs an empty span whose data() == nullptr and size() == 0. |
| 414 | // This overload only participates in overload resolution if |
| 415 | // extent == 0 || extent == flatbuffers::dynamic_extent. |
| 416 | // A dummy template argument N is need dependency for SFINAE. |
| 417 | template<std::size_t N = 0, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 418 | typename internal::is_span_convertible<element_type, Extent, element_type, (N - N)>::type = 0> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 419 | FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(nullptr), |
| 420 | count_(0) { |
| 421 | static_assert(extent == 0 || extent == dynamic_extent, "invalid span"); |
| 422 | } |
| 423 | |
| 424 | // Constructs a span that is a view over the array arr; the resulting span |
| 425 | // has size() == N and data() == std::data(arr). These overloads only |
| 426 | // participate in overload resolution if |
| 427 | // extent == std::dynamic_extent || N == extent is true and |
| 428 | // std::remove_pointer_t<decltype(std::data(arr))>(*)[] |
| 429 | // is convertible to element_type (*)[]. |
| 430 | template<std::size_t N, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 431 | typename internal::is_span_convertible<element_type, Extent, element_type, N>::type = 0> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 432 | FLATBUFFERS_CONSTEXPR_CPP11 span(element_type (&arr)[N]) FLATBUFFERS_NOEXCEPT |
| 433 | : data_(arr), count_(N) {} |
| 434 | |
| 435 | template<class U, std::size_t N, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 436 | typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 437 | FLATBUFFERS_CONSTEXPR_CPP11 span(std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT |
| 438 | : data_(arr.data()), count_(N) {} |
| 439 | |
| 440 | //template<class U, std::size_t N, |
| 441 | // int = 0> |
| 442 | //FLATBUFFERS_CONSTEXPR_CPP11 span(std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT |
| 443 | // : data_(arr.data()), count_(N) {} |
| 444 | |
| 445 | template<class U, std::size_t N, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 446 | typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 447 | FLATBUFFERS_CONSTEXPR_CPP11 span(const std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT |
| 448 | : data_(arr.data()), count_(N) {} |
| 449 | |
| 450 | // Converting constructor from another span s; |
| 451 | // the resulting span has size() == s.size() and data() == s.data(). |
| 452 | // This overload only participates in overload resolution |
| 453 | // if extent == std::dynamic_extent || N == extent is true and U (*)[] |
| 454 | // is convertible to element_type (*)[]. |
| 455 | template<class U, std::size_t N, |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 456 | typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0> |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 457 | FLATBUFFERS_CONSTEXPR_CPP11 span(const flatbuffers::span<U, N> &s) FLATBUFFERS_NOEXCEPT |
| 458 | : span(s.data(), s.size()) { |
| 459 | } |
| 460 | |
| 461 | #endif // !defined(FLATBUFFERS_SPAN_MINIMAL) |
| 462 | |
| 463 | private: |
| 464 | // This is a naive implementation with 'count_' member even if (Extent != dynamic_extent). |
| 465 | pointer const data_; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 466 | size_type count_; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 467 | }; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 468 | #endif // defined(FLATBUFFERS_USE_STD_SPAN) |
| 469 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 470 | #if !defined(FLATBUFFERS_SPAN_MINIMAL) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 471 | template<class ElementType, std::size_t Extent> |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 472 | FLATBUFFERS_CONSTEXPR_CPP11 |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 473 | flatbuffers::span<ElementType, Extent> make_span(ElementType(&arr)[Extent]) FLATBUFFERS_NOEXCEPT { |
| 474 | return span<ElementType, Extent>(arr); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 477 | template<class ElementType, std::size_t Extent> |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 478 | FLATBUFFERS_CONSTEXPR_CPP11 |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 479 | flatbuffers::span<const ElementType, Extent> make_span(const ElementType(&arr)[Extent]) FLATBUFFERS_NOEXCEPT { |
| 480 | return span<const ElementType, Extent>(arr); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 483 | template<class ElementType, std::size_t Extent> |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 484 | FLATBUFFERS_CONSTEXPR_CPP11 |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 485 | flatbuffers::span<ElementType, Extent> make_span(std::array<ElementType, Extent> &arr) FLATBUFFERS_NOEXCEPT { |
| 486 | return span<ElementType, Extent>(arr); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 489 | template<class ElementType, std::size_t Extent> |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 490 | FLATBUFFERS_CONSTEXPR_CPP11 |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 491 | flatbuffers::span<const ElementType, Extent> make_span(const std::array<ElementType, Extent> &arr) FLATBUFFERS_NOEXCEPT { |
| 492 | return span<const ElementType, Extent>(arr); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 495 | template<class ElementType, std::size_t Extent> |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 496 | FLATBUFFERS_CONSTEXPR_CPP11 |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 497 | flatbuffers::span<ElementType, dynamic_extent> make_span(ElementType *first, std::size_t count) FLATBUFFERS_NOEXCEPT { |
| 498 | return span<ElementType, dynamic_extent>(first, count); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 501 | template<class ElementType, std::size_t Extent> |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 502 | FLATBUFFERS_CONSTEXPR_CPP11 |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 503 | flatbuffers::span<const ElementType, dynamic_extent> make_span(const ElementType *first, std::size_t count) FLATBUFFERS_NOEXCEPT { |
| 504 | return span<const ElementType, dynamic_extent>(first, count); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 505 | } |
| 506 | #endif // !defined(FLATBUFFERS_SPAN_MINIMAL) |
| 507 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 508 | } // namespace flatbuffers |
| 509 | |
| 510 | #endif // FLATBUFFERS_STL_EMULATION_H_ |