File indexing completed on 2025-08-27 09:30:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef FLATBUFFERS_STL_EMULATION_H_
0018 #define FLATBUFFERS_STL_EMULATION_H_
0019
0020
0021 #include "flatbuffers/base.h"
0022
0023 #include <string>
0024 #include <type_traits>
0025 #include <vector>
0026 #include <memory>
0027 #include <limits>
0028
0029 #ifndef FLATBUFFERS_USE_STD_OPTIONAL
0030
0031
0032 #if (defined(__cplusplus) && __cplusplus >= 201703L) \
0033 || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
0034 #define FLATBUFFERS_USE_STD_OPTIONAL 1
0035 #else
0036 #define FLATBUFFERS_USE_STD_OPTIONAL 0
0037 #endif
0038 #endif
0039
0040 #if FLATBUFFERS_USE_STD_OPTIONAL
0041 #include <optional>
0042 #endif
0043
0044 #ifndef FLATBUFFERS_USE_STD_SPAN
0045
0046
0047
0048 #if defined(__cplusplus) && __cplusplus >= 202002L \
0049 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
0050 #define FLATBUFFERS_USE_STD_SPAN 1
0051 #endif
0052 #endif
0053
0054 #if defined(FLATBUFFERS_USE_STD_SPAN)
0055 #include <array>
0056 #include <span>
0057 #else
0058
0059 #if !defined(FLATBUFFERS_TEMPLATES_ALIASES)
0060 #define FLATBUFFERS_SPAN_MINIMAL
0061 #else
0062
0063 #include <array>
0064 #endif
0065 #endif
0066
0067
0068 namespace flatbuffers {
0069
0070 #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
0071 template <typename T>
0072 using numeric_limits = std::numeric_limits<T>;
0073 #else
0074 template <typename T> class numeric_limits :
0075 public std::numeric_limits<T> {};
0076 #endif
0077
0078 #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
0079 template <typename T> using is_scalar = std::is_scalar<T>;
0080 template <typename T, typename U> using is_same = std::is_same<T,U>;
0081 template <typename T> using is_floating_point = std::is_floating_point<T>;
0082 template <typename T> using is_unsigned = std::is_unsigned<T>;
0083 template <typename T> using is_enum = std::is_enum<T>;
0084 template <typename T> using make_unsigned = std::make_unsigned<T>;
0085 template<bool B, class T, class F>
0086 using conditional = std::conditional<B, T, F>;
0087 template<class T, T v>
0088 using integral_constant = std::integral_constant<T, v>;
0089 template <bool B>
0090 using bool_constant = integral_constant<bool, B>;
0091 using true_type = std::true_type;
0092 using false_type = std::false_type;
0093 #else
0094
0095 template <typename T> struct is_scalar : public std::is_scalar<T> {};
0096 template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
0097 template <typename T> struct is_floating_point :
0098 public std::is_floating_point<T> {};
0099 template <typename T> struct is_unsigned : public std::is_unsigned<T> {};
0100 template <typename T> struct is_enum : public std::is_enum<T> {};
0101 template <typename T> struct make_unsigned : public std::make_unsigned<T> {};
0102 template<bool B, class T, class F>
0103 struct conditional : public std::conditional<B, T, F> {};
0104 template<class T, T v>
0105 struct integral_constant : public std::integral_constant<T, v> {};
0106 template <bool B>
0107 struct bool_constant : public integral_constant<bool, B> {};
0108 typedef bool_constant<true> true_type;
0109 typedef bool_constant<false> false_type;
0110 #endif
0111
0112 #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
0113 template <class T> using unique_ptr = std::unique_ptr<T>;
0114 #else
0115
0116
0117
0118
0119
0120 template <class T> class unique_ptr : public std::unique_ptr<T> {
0121 public:
0122 unique_ptr() {}
0123 explicit unique_ptr(T* p) : std::unique_ptr<T>(p) {}
0124 unique_ptr(std::unique_ptr<T>&& u) { *this = std::move(u); }
0125 unique_ptr(unique_ptr&& u) { *this = std::move(u); }
0126 unique_ptr& operator=(std::unique_ptr<T>&& u) {
0127 std::unique_ptr<T>::reset(u.release());
0128 return *this;
0129 }
0130 unique_ptr& operator=(unique_ptr&& u) {
0131 std::unique_ptr<T>::reset(u.release());
0132 return *this;
0133 }
0134 unique_ptr& operator=(T* p) {
0135 return std::unique_ptr<T>::operator=(p);
0136 }
0137 };
0138 #endif
0139
0140 #if FLATBUFFERS_USE_STD_OPTIONAL
0141 template<class T>
0142 using Optional = std::optional<T>;
0143 using nullopt_t = std::nullopt_t;
0144 inline constexpr nullopt_t nullopt = std::nullopt;
0145
0146 #else
0147
0148
0149
0150
0151
0152 struct nullopt_t {
0153 explicit FLATBUFFERS_CONSTEXPR_CPP11 nullopt_t(int) {}
0154 };
0155
0156 #if defined(FLATBUFFERS_CONSTEXPR_DEFINED)
0157 namespace internal {
0158 template <class> struct nullopt_holder {
0159 static constexpr nullopt_t instance_ = nullopt_t(0);
0160 };
0161 template<class Dummy>
0162 constexpr nullopt_t nullopt_holder<Dummy>::instance_;
0163 }
0164 static constexpr const nullopt_t &nullopt = internal::nullopt_holder<void>::instance_;
0165
0166 #else
0167 namespace internal {
0168 template <class> struct nullopt_holder {
0169 static const nullopt_t instance_;
0170 };
0171 template<class Dummy>
0172 const nullopt_t nullopt_holder<Dummy>::instance_ = nullopt_t(0);
0173 }
0174 static const nullopt_t &nullopt = internal::nullopt_holder<void>::instance_;
0175
0176 #endif
0177
0178 template<class T>
0179 class Optional FLATBUFFERS_FINAL_CLASS {
0180
0181
0182
0183 static_assert(flatbuffers::is_scalar<T>::value, "unexpected type T");
0184
0185 public:
0186 ~Optional() {}
0187
0188 FLATBUFFERS_CONSTEXPR_CPP11 Optional() FLATBUFFERS_NOEXCEPT
0189 : value_(), has_value_(false) {}
0190
0191 FLATBUFFERS_CONSTEXPR_CPP11 Optional(nullopt_t) FLATBUFFERS_NOEXCEPT
0192 : value_(), has_value_(false) {}
0193
0194 FLATBUFFERS_CONSTEXPR_CPP11 Optional(T val) FLATBUFFERS_NOEXCEPT
0195 : value_(val), has_value_(true) {}
0196
0197 FLATBUFFERS_CONSTEXPR_CPP11 Optional(const Optional &other) FLATBUFFERS_NOEXCEPT
0198 : value_(other.value_), has_value_(other.has_value_) {}
0199
0200 FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(const Optional &other) FLATBUFFERS_NOEXCEPT {
0201 value_ = other.value_;
0202 has_value_ = other.has_value_;
0203 return *this;
0204 }
0205
0206 FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(nullopt_t) FLATBUFFERS_NOEXCEPT {
0207 value_ = T();
0208 has_value_ = false;
0209 return *this;
0210 }
0211
0212 FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(T val) FLATBUFFERS_NOEXCEPT {
0213 value_ = val;
0214 has_value_ = true;
0215 return *this;
0216 }
0217
0218 void reset() FLATBUFFERS_NOEXCEPT {
0219 *this = nullopt;
0220 }
0221
0222 void swap(Optional &other) FLATBUFFERS_NOEXCEPT {
0223 std::swap(value_, other.value_);
0224 std::swap(has_value_, other.has_value_);
0225 }
0226
0227 FLATBUFFERS_CONSTEXPR_CPP11 FLATBUFFERS_EXPLICIT_CPP11 operator bool() const FLATBUFFERS_NOEXCEPT {
0228 return has_value_;
0229 }
0230
0231 FLATBUFFERS_CONSTEXPR_CPP11 bool has_value() const FLATBUFFERS_NOEXCEPT {
0232 return has_value_;
0233 }
0234
0235 FLATBUFFERS_CONSTEXPR_CPP11 const T& operator*() const FLATBUFFERS_NOEXCEPT {
0236 return value_;
0237 }
0238
0239 const T& value() const {
0240 FLATBUFFERS_ASSERT(has_value());
0241 return value_;
0242 }
0243
0244 T value_or(T default_value) const FLATBUFFERS_NOEXCEPT {
0245 return has_value() ? value_ : default_value;
0246 }
0247
0248 private:
0249 T value_;
0250 bool has_value_;
0251 };
0252
0253 template<class T>
0254 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& opt, nullopt_t) FLATBUFFERS_NOEXCEPT {
0255 return !opt;
0256 }
0257 template<class T>
0258 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(nullopt_t, const Optional<T>& opt) FLATBUFFERS_NOEXCEPT {
0259 return !opt;
0260 }
0261
0262 template<class T, class U>
0263 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const U& rhs) FLATBUFFERS_NOEXCEPT {
0264 return static_cast<bool>(lhs) && (*lhs == rhs);
0265 }
0266
0267 template<class T, class U>
0268 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const T& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
0269 return static_cast<bool>(rhs) && (lhs == *rhs);
0270 }
0271
0272 template<class T, class U>
0273 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
0274 return static_cast<bool>(lhs) != static_cast<bool>(rhs)
0275 ? false
0276 : !static_cast<bool>(lhs) ? true : (*lhs == *rhs);
0277 }
0278 #endif
0279
0280
0281
0282 #if defined(FLATBUFFERS_USE_STD_SPAN)
0283 inline constexpr std::size_t dynamic_extent = std::dynamic_extent;
0284 template<class T, std::size_t Extent = std::dynamic_extent>
0285 using span = std::span<T, Extent>;
0286
0287 #else
0288 FLATBUFFERS_CONSTEXPR std::size_t dynamic_extent = static_cast<std::size_t>(-1);
0289
0290
0291
0292 #if !defined(FLATBUFFERS_SPAN_MINIMAL)
0293 namespace internal {
0294
0295
0296
0297
0298
0299 template<class To, std::size_t Extent, class From, std::size_t N>
0300 struct is_span_convertible {
0301 using type =
0302 typename std::conditional<std::is_convertible<From (*)[], To (*)[]>::value
0303 && (Extent == dynamic_extent || N == Extent),
0304 int, void>::type;
0305 };
0306
0307 template<typename T>
0308 struct SpanIterator {
0309
0310 using iterator_category = std::forward_iterator_tag;
0311 using difference_type = std::ptrdiff_t;
0312 using value_type = typename std::remove_cv<T>::type;
0313 using reference = T&;
0314 using pointer = T*;
0315
0316
0317 #ifdef _MSC_VER
0318 using _Unchecked_type = pointer;
0319 #endif
0320
0321 SpanIterator(pointer ptr) : ptr_(ptr) {}
0322 reference operator*() const { return *ptr_; }
0323 pointer operator->() { return ptr_; }
0324 SpanIterator& operator++() { ptr_++; return *this; }
0325 SpanIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
0326
0327 friend bool operator== (const SpanIterator& lhs, const SpanIterator& rhs) { return lhs.ptr_ == rhs.ptr_; }
0328 friend bool operator!= (const SpanIterator& lhs, const SpanIterator& rhs) { return lhs.ptr_ != rhs.ptr_; }
0329
0330 private:
0331 pointer ptr_;
0332 };
0333 }
0334 #endif
0335
0336
0337
0338
0339 template<class T, std::size_t Extent = dynamic_extent>
0340 class span FLATBUFFERS_FINAL_CLASS {
0341 public:
0342 typedef T element_type;
0343 typedef T& reference;
0344 typedef const T& const_reference;
0345 typedef T* pointer;
0346 typedef const T* const_pointer;
0347 typedef std::size_t size_type;
0348
0349 static FLATBUFFERS_CONSTEXPR size_type extent = Extent;
0350
0351
0352 FLATBUFFERS_CONSTEXPR_CPP11 size_type size() const FLATBUFFERS_NOEXCEPT {
0353 return count_;
0354 }
0355
0356
0357 FLATBUFFERS_CONSTEXPR_CPP11
0358 size_type size_bytes() const FLATBUFFERS_NOEXCEPT {
0359 return size() * sizeof(element_type);
0360 }
0361
0362
0363 FLATBUFFERS_CONSTEXPR_CPP11 bool empty() const FLATBUFFERS_NOEXCEPT {
0364 return size() == 0;
0365 }
0366
0367
0368 FLATBUFFERS_CONSTEXPR_CPP11 pointer data() const FLATBUFFERS_NOEXCEPT {
0369 return data_;
0370 }
0371
0372 #if !defined(FLATBUFFERS_SPAN_MINIMAL)
0373 using Iterator = internal::SpanIterator<T>;
0374
0375 Iterator begin() const { return Iterator(data()); }
0376 Iterator end() const { return Iterator(data() + size()); }
0377 #endif
0378
0379
0380
0381 FLATBUFFERS_CONSTEXPR_CPP11 reference operator[](size_type idx) const {
0382 return data()[idx];
0383 }
0384
0385 FLATBUFFERS_CONSTEXPR_CPP11 span(const span &other) FLATBUFFERS_NOEXCEPT
0386 : data_(other.data_), count_(other.count_) {}
0387
0388 FLATBUFFERS_CONSTEXPR_CPP14 span &operator=(const span &other)
0389 FLATBUFFERS_NOEXCEPT {
0390 data_ = other.data_;
0391 count_ = other.count_;
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401 FLATBUFFERS_CONSTEXPR_CPP11
0402 explicit span(pointer first, size_type count) FLATBUFFERS_NOEXCEPT
0403 : data_ (Extent == dynamic_extent ? first : (Extent == count ? first : nullptr)),
0404 count_(Extent == dynamic_extent ? count : (Extent == count ? Extent : 0)) {
0405
0406 }
0407
0408
0409
0410 #if defined(FLATBUFFERS_SPAN_MINIMAL)
0411 FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(nullptr),
0412 count_(0) {
0413 static_assert(extent == 0 || extent == dynamic_extent, "invalid span");
0414 }
0415
0416 #else
0417
0418
0419
0420
0421 template<std::size_t N = 0,
0422 typename internal::is_span_convertible<element_type, Extent, element_type, (N - N)>::type = 0>
0423 FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(nullptr),
0424 count_(0) {
0425 static_assert(extent == 0 || extent == dynamic_extent, "invalid span");
0426 }
0427
0428
0429
0430
0431
0432
0433
0434 template<std::size_t N,
0435 typename internal::is_span_convertible<element_type, Extent, element_type, N>::type = 0>
0436 FLATBUFFERS_CONSTEXPR_CPP11 span(element_type (&arr)[N]) FLATBUFFERS_NOEXCEPT
0437 : data_(arr), count_(N) {}
0438
0439 template<class U, std::size_t N,
0440 typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0>
0441 FLATBUFFERS_CONSTEXPR_CPP11 span(std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT
0442 : data_(arr.data()), count_(N) {}
0443
0444
0445
0446
0447
0448
0449 template<class U, std::size_t N,
0450 typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0>
0451 FLATBUFFERS_CONSTEXPR_CPP11 span(const std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT
0452 : data_(arr.data()), count_(N) {}
0453
0454
0455
0456
0457
0458
0459 template<class U, std::size_t N,
0460 typename internal::is_span_convertible<element_type, Extent, U, N>::type = 0>
0461 FLATBUFFERS_CONSTEXPR_CPP11 span(const flatbuffers::span<U, N> &s) FLATBUFFERS_NOEXCEPT
0462 : span(s.data(), s.size()) {
0463 }
0464
0465 #endif
0466
0467 private:
0468
0469 pointer const data_;
0470 size_type count_;
0471 };
0472 #endif
0473
0474 #if !defined(FLATBUFFERS_SPAN_MINIMAL)
0475 template<class ElementType, std::size_t Extent>
0476 FLATBUFFERS_CONSTEXPR_CPP11
0477 flatbuffers::span<ElementType, Extent> make_span(ElementType(&arr)[Extent]) FLATBUFFERS_NOEXCEPT {
0478 return span<ElementType, Extent>(arr);
0479 }
0480
0481 template<class ElementType, std::size_t Extent>
0482 FLATBUFFERS_CONSTEXPR_CPP11
0483 flatbuffers::span<const ElementType, Extent> make_span(const ElementType(&arr)[Extent]) FLATBUFFERS_NOEXCEPT {
0484 return span<const ElementType, Extent>(arr);
0485 }
0486
0487 template<class ElementType, std::size_t Extent>
0488 FLATBUFFERS_CONSTEXPR_CPP11
0489 flatbuffers::span<ElementType, Extent> make_span(std::array<ElementType, Extent> &arr) FLATBUFFERS_NOEXCEPT {
0490 return span<ElementType, Extent>(arr);
0491 }
0492
0493 template<class ElementType, std::size_t Extent>
0494 FLATBUFFERS_CONSTEXPR_CPP11
0495 flatbuffers::span<const ElementType, Extent> make_span(const std::array<ElementType, Extent> &arr) FLATBUFFERS_NOEXCEPT {
0496 return span<const ElementType, Extent>(arr);
0497 }
0498
0499 template<class ElementType, std::size_t Extent>
0500 FLATBUFFERS_CONSTEXPR_CPP11
0501 flatbuffers::span<ElementType, dynamic_extent> make_span(ElementType *first, std::size_t count) FLATBUFFERS_NOEXCEPT {
0502 return span<ElementType, dynamic_extent>(first, count);
0503 }
0504
0505 template<class ElementType, std::size_t Extent>
0506 FLATBUFFERS_CONSTEXPR_CPP11
0507 flatbuffers::span<const ElementType, dynamic_extent> make_span(const ElementType *first, std::size_t count) FLATBUFFERS_NOEXCEPT {
0508 return span<const ElementType, dynamic_extent>(first, count);
0509 }
0510 #endif
0511
0512 }
0513
0514 #endif