blob: 452ddb832f117f9ad3e2ad1c24a74dc01e220cd9 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
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 Schuh272c6132020-11-14 16:37:52 -080021#include "flatbuffers/base.h"
Austin Schuhe89fa2d2019-08-14 20:24:23 -070022
23#include <string>
24#include <type_traits>
25#include <vector>
26#include <memory>
27#include <limits>
28
Austin Schuh2dd86a92022-09-14 21:19:23 -070029#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 Schuh272c6132020-11-14 16:37:52 -080041 #include <optional>
Austin Schuh2dd86a92022-09-14 21:19:23 -070042#endif
Austin Schuh272c6132020-11-14 16:37:52 -080043
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 Schuh2dd86a92022-09-14 21:19:23 -070049 #include <array>
Austin Schuh272c6132020-11-14 16:37:52 -080050 #include <span>
51 #define FLATBUFFERS_USE_STD_SPAN
52 #endif
53#else
54 // Disable non-trivial ctors if FLATBUFFERS_SPAN_MINIMAL defined.
James Kuszmaul8e62b022022-03-22 09:33:25 -070055 #if !defined(FLATBUFFERS_TEMPLATES_ALIASES)
Austin Schuh272c6132020-11-14 16:37:52 -080056 #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 Schuhe89fa2d2019-08-14 20:24:23 -070062
James Kuszmaul8e62b022022-03-22 09:33:25 -070063// This header provides backwards compatibility for older versions of the STL.
Austin Schuhe89fa2d2019-08-14 20:24:23 -070064namespace flatbuffers {
65
James Kuszmaul8e62b022022-03-22 09:33:25 -070066#if defined(FLATBUFFERS_TEMPLATES_ALIASES)
67 template <typename T>
68 using numeric_limits = std::numeric_limits<T>;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070069#else
70 template <typename T> class numeric_limits :
James Kuszmaul8e62b022022-03-22 09:33:25 -070071 public std::numeric_limits<T> {};
72#endif // defined(FLATBUFFERS_TEMPLATES_ALIASES)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070073
74#if defined(FLATBUFFERS_TEMPLATES_ALIASES)
James Kuszmaul8e62b022022-03-22 09:33:25 -070075 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 Schuhe89fa2d2019-08-14 20:24:23 -070089#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 Schuh272c6132020-11-14 16:37:52 -080096 template <typename T> struct is_enum : public std::is_enum<T> {};
Austin Schuhe89fa2d2019-08-14 20:24:23 -070097 template <typename T> struct make_unsigned : public std::make_unsigned<T> {};
Austin Schuh272c6132020-11-14 16:37:52 -080098 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 Kuszmaul8e62b022022-03-22 09:33:25 -0700102 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 Schuhe89fa2d2019-08-14 20:24:23 -0700106#endif // defined(FLATBUFFERS_TEMPLATES_ALIASES)
107
James Kuszmaul8e62b022022-03-22 09:33:25 -0700108#if defined(FLATBUFFERS_TEMPLATES_ALIASES)
109 template <class T> using unique_ptr = std::unique_ptr<T>;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700110#else
James Kuszmaul8e62b022022-03-22 09:33:25 -0700111 // 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 Schuhe89fa2d2019-08-14 20:24:23 -0700124 return *this;
125 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700126 unique_ptr& operator=(unique_ptr&& u) {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700127 std::unique_ptr<T>::reset(u.release());
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700128 return *this;
129 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700130 unique_ptr& operator=(T* p) {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700131 return std::unique_ptr<T>::operator=(p);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700132 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700133 };
James Kuszmaul8e62b022022-03-22 09:33:25 -0700134#endif // defined(FLATBUFFERS_TEMPLATES_ALIASES)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700135
Austin Schuh2dd86a92022-09-14 21:19:23 -0700136#if FLATBUFFERS_USE_STD_OPTIONAL
Austin Schuh272c6132020-11-14 16:37:52 -0800137template<class T>
138using Optional = std::optional<T>;
139using nullopt_t = std::nullopt_t;
140inline 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>.
148struct 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
174template<class T>
175class 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
249template<class T>
250FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& opt, nullopt_t) FLATBUFFERS_NOEXCEPT {
251 return !opt;
252}
253template<class T>
254FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(nullopt_t, const Optional<T>& opt) FLATBUFFERS_NOEXCEPT {
255 return !opt;
256}
257
258template<class T, class U>
259FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const U& rhs) FLATBUFFERS_NOEXCEPT {
260 return static_cast<bool>(lhs) && (*lhs == rhs);
261}
262
263template<class T, class U>
264FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const T& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
265 return static_cast<bool>(rhs) && (lhs == *rhs);
266}
267
268template<class T, class U>
269FLATBUFFERS_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)
284FLATBUFFERS_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)
289namespace internal {
290 // This is SFINAE helper class for checking of a common condition:
291 // > This overload only participates in overload resolution
Austin Schuh2dd86a92022-09-14 21:19:23 -0700292 // > 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 Schuh272c6132020-11-14 16:37:52 -0800297 using type =
Austin Schuh2dd86a92022-09-14 21:19:23 -0700298 typename std::conditional<std::is_convertible<From (*)[], To (*)[]>::value
Austin Schuh272c6132020-11-14 16:37:52 -0800299 && (Extent == dynamic_extent || N == Extent),
300 int, void>::type;
301 };
302
James Kuszmaul8e62b022022-03-22 09:33:25 -0700303 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 Schuh272c6132020-11-14 16:37:52 -0800329} // 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.
335template<class T, std::size_t Extent = dynamic_extent>
336class 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 Kuszmaul8e62b022022-03-22 09:33:25 -0700368 #if !defined(FLATBUFFERS_SPAN_MINIMAL)
369 using Iterator = internal::SpanIterator<T>;
James Kuszmaul8e62b022022-03-22 09:33:25 -0700370
371 Iterator begin() const { return Iterator(data()); }
372 Iterator end() const { return Iterator(data() + size()); }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700373 #endif
374
Austin Schuh272c6132020-11-14 16:37:52 -0800375 // 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 Schuh2dd86a92022-09-14 21:19:23 -0700418 typename internal::is_span_convertible<element_type, Extent, element_type, (N - N)>::type = 0>
Austin Schuh272c6132020-11-14 16:37:52 -0800419 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 Schuh2dd86a92022-09-14 21:19:23 -0700431 typename internal::is_span_convertible<element_type, Extent, element_type, N>::type = 0>
Austin Schuh272c6132020-11-14 16:37:52 -0800432 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 Schuh2dd86a92022-09-14 21:19:23 -0700436 typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0>
Austin Schuh272c6132020-11-14 16:37:52 -0800437 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 Schuh2dd86a92022-09-14 21:19:23 -0700446 typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0>
Austin Schuh272c6132020-11-14 16:37:52 -0800447 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 Schuh2dd86a92022-09-14 21:19:23 -0700456 typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0>
Austin Schuh272c6132020-11-14 16:37:52 -0800457 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 Schuh2dd86a92022-09-14 21:19:23 -0700466 size_type count_;
Austin Schuh272c6132020-11-14 16:37:52 -0800467};
Austin Schuh272c6132020-11-14 16:37:52 -0800468#endif // defined(FLATBUFFERS_USE_STD_SPAN)
469
James Kuszmaul8e62b022022-03-22 09:33:25 -0700470#if !defined(FLATBUFFERS_SPAN_MINIMAL)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700471template<class ElementType, std::size_t Extent>
James Kuszmaul8e62b022022-03-22 09:33:25 -0700472FLATBUFFERS_CONSTEXPR_CPP11
Austin Schuh2dd86a92022-09-14 21:19:23 -0700473flatbuffers::span<ElementType, Extent> make_span(ElementType(&arr)[Extent]) FLATBUFFERS_NOEXCEPT {
474 return span<ElementType, Extent>(arr);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700475}
476
Austin Schuh2dd86a92022-09-14 21:19:23 -0700477template<class ElementType, std::size_t Extent>
James Kuszmaul8e62b022022-03-22 09:33:25 -0700478FLATBUFFERS_CONSTEXPR_CPP11
Austin Schuh2dd86a92022-09-14 21:19:23 -0700479flatbuffers::span<const ElementType, Extent> make_span(const ElementType(&arr)[Extent]) FLATBUFFERS_NOEXCEPT {
480 return span<const ElementType, Extent>(arr);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700481}
482
Austin Schuh2dd86a92022-09-14 21:19:23 -0700483template<class ElementType, std::size_t Extent>
James Kuszmaul8e62b022022-03-22 09:33:25 -0700484FLATBUFFERS_CONSTEXPR_CPP11
Austin Schuh2dd86a92022-09-14 21:19:23 -0700485flatbuffers::span<ElementType, Extent> make_span(std::array<ElementType, Extent> &arr) FLATBUFFERS_NOEXCEPT {
486 return span<ElementType, Extent>(arr);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700487}
488
Austin Schuh2dd86a92022-09-14 21:19:23 -0700489template<class ElementType, std::size_t Extent>
James Kuszmaul8e62b022022-03-22 09:33:25 -0700490FLATBUFFERS_CONSTEXPR_CPP11
Austin Schuh2dd86a92022-09-14 21:19:23 -0700491flatbuffers::span<const ElementType, Extent> make_span(const std::array<ElementType, Extent> &arr) FLATBUFFERS_NOEXCEPT {
492 return span<const ElementType, Extent>(arr);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700493}
494
Austin Schuh2dd86a92022-09-14 21:19:23 -0700495template<class ElementType, std::size_t Extent>
James Kuszmaul8e62b022022-03-22 09:33:25 -0700496FLATBUFFERS_CONSTEXPR_CPP11
Austin Schuh2dd86a92022-09-14 21:19:23 -0700497flatbuffers::span<ElementType, dynamic_extent> make_span(ElementType *first, std::size_t count) FLATBUFFERS_NOEXCEPT {
498 return span<ElementType, dynamic_extent>(first, count);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700499}
500
Austin Schuh2dd86a92022-09-14 21:19:23 -0700501template<class ElementType, std::size_t Extent>
James Kuszmaul8e62b022022-03-22 09:33:25 -0700502FLATBUFFERS_CONSTEXPR_CPP11
Austin Schuh2dd86a92022-09-14 21:19:23 -0700503flatbuffers::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 Kuszmaul8e62b022022-03-22 09:33:25 -0700505}
506#endif // !defined(FLATBUFFERS_SPAN_MINIMAL)
507
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700508} // namespace flatbuffers
509
510#endif // FLATBUFFERS_STL_EMULATION_H_