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