18#ifndef FLATBUFFERS_STL_EMULATION_H_
19#define FLATBUFFERS_STL_EMULATION_H_
30#if defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
31 #define FLATBUFFERS_CPP98_STL
34#if defined(FLATBUFFERS_CPP98_STL)
40#if defined(FLATBUFFERS_USE_STD_OPTIONAL) \
41 || (defined(__cplusplus) && __cplusplus >= 201703L) \
42 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))
44 #ifndef FLATBUFFERS_USE_STD_OPTIONAL
45 #define FLATBUFFERS_USE_STD_OPTIONAL
50#if defined(FLATBUFFERS_USE_STD_SPAN)
52#elif defined(__cpp_lib_span) && defined(__has_include)
53 #if __has_include(<span>)
55 #define FLATBUFFERS_USE_STD_SPAN
59 #if !defined(FLATBUFFERS_TEMPLATES_ALIASES) || defined(FLATBUFFERS_CPP98_STL)
60 #define FLATBUFFERS_SPAN_MINIMAL
73 return value[value.length() - 1];
77 return value[value.length() - 1];
82template <
typename T>
inline T *
vector_data(std::vector<T> &vector) {
85 return vector.empty() ? nullptr : &vector[0];
89 const std::vector<T> &vector) {
90 return vector.empty() ? nullptr : &vector[0];
93template <
typename T,
typename V>
95 #if defined(FLATBUFFERS_CPP98_STL)
96 vector->push_back(
data);
98 vector->emplace_back(std::forward<V>(
data));
102#ifndef FLATBUFFERS_CPP98_STL
103 #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
104 template <
typename T>
105 using numeric_limits = std::numeric_limits<T>;
108 public std::numeric_limits<T> {};
112 public std::numeric_limits<T> {
116 return std::numeric_limits<T>::min();
120 template <>
class numeric_limits<float> :
121 public std::numeric_limits<float> {
123 static float lowest() {
return -FLT_MAX; }
126 template <>
class numeric_limits<double> :
127 public std::numeric_limits<double> {
129 static double lowest() {
return -DBL_MAX; }
132 template <>
class numeric_limits<unsigned long long> {
134 static unsigned long long min() {
return 0ULL; }
135 static unsigned long long max() {
return ~0ULL; }
136 static unsigned long long lowest() {
137 return numeric_limits<unsigned long long>::min();
141 template <>
class numeric_limits<long long> {
143 static long long min() {
144 return static_cast<long long>(1ULL << ((
sizeof(
long long) << 3) - 1));
146 static long long max() {
147 return static_cast<long long>(
148 (1ULL << ((
sizeof(
long long) << 3) - 1)) - 1);
150 static long long lowest() {
151 return numeric_limits<long long>::min();
156#if defined(FLATBUFFERS_TEMPLATES_ALIASES)
157 #ifndef FLATBUFFERS_CPP98_STL
158 template <
typename T>
using is_scalar = std::is_scalar<T>;
159 template <
typename T,
typename U>
using is_same = std::is_same<T,U>;
160 template <
typename T>
using is_floating_point = std::is_floating_point<T>;
161 template <
typename T>
using is_unsigned = std::is_unsigned<T>;
162 template <
typename T>
using is_enum = std::is_enum<T>;
163 template <
typename T>
using make_unsigned = std::make_unsigned<T>;
164 template<
bool B,
class T,
class F>
165 using conditional = std::conditional<B, T, F>;
166 template<
class T, T v>
167 using integral_constant = std::integral_constant<T, v>;
169 using bool_constant = integral_constant<bool, B>;
172 template <
typename T>
using is_scalar = std::tr1::is_scalar<T>;
173 template <
typename T,
typename U>
using is_same = std::tr1::is_same<T,U>;
174 template <
typename T>
using is_floating_point =
175 std::tr1::is_floating_point<T>;
176 template <
typename T>
using is_unsigned = std::tr1::is_unsigned<T>;
177 template <
typename T>
using is_enum = std::tr1::is_enum<T>;
179 template<
typename T>
struct make_unsigned {
180 static_assert(is_unsigned<T>::value,
"Specialization not implemented!");
183 template<>
struct make_unsigned<char> {
using type =
unsigned char; };
184 template<>
struct make_unsigned<short> {
using type =
unsigned short; };
185 template<>
struct make_unsigned<
int> {
using type =
unsigned int; };
186 template<>
struct make_unsigned<long> {
using type =
unsigned long; };
188 struct make_unsigned<long long> {
using type =
unsigned long long; };
189 template<
bool B,
class T,
class F>
190 using conditional = std::tr1::conditional<B, T, F>;
191 template<
class T, T v>
192 using integral_constant = std::tr1::integral_constant<T, v>;
194 using bool_constant = integral_constant<bool, B>;
198 template <
typename T>
struct is_scalar :
public std::is_scalar<T> {};
199 template <
typename T,
typename U>
struct is_same :
public std::is_same<T,U> {};
201 public std::is_floating_point<T> {};
202 template <
typename T>
struct is_unsigned :
public std::is_unsigned<T> {};
203 template <
typename T>
struct is_enum :
public std::is_enum<T> {};
204 template <
typename T>
struct make_unsigned :
public std::make_unsigned<T> {};
205 template<
bool B,
class T,
class F>
207 template<
class T, T v>
213#ifndef FLATBUFFERS_CPP98_STL
214 #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
215 template <
class T>
using unique_ptr = std::unique_ptr<T>;
222 template <
class T>
class unique_ptr :
public std::unique_ptr<T> {
229 std::unique_ptr<T>::reset(u.release());
233 std::unique_ptr<T>::reset(u.release());
237 return std::unique_ptr<T>::operator=(p);
245 template <
class T>
class unique_ptr {
247 typedef T element_type;
253 reset(
const_cast<unique_ptr*
>(&u)->release());
255 ~unique_ptr() { reset(); }
258 reset(
const_cast<unique_ptr*
>(&u)->release());
272 const T&
operator*()
const {
return *ptr_; }
273 T* operator->()
const {
return ptr_; }
274 T*
get() const noexcept {
return ptr_; }
275 explicit operator bool()
const {
return ptr_ !=
nullptr; }
284 void reset(T* p =
nullptr) {
287 if (value)
delete value;
300 template <
class T>
bool operator==(
const unique_ptr<T>& x,
301 const unique_ptr<T>& y) {
302 return x.get() == y.get();
305 template <
class T,
class D>
bool operator==(
const unique_ptr<T>& x,
307 return static_cast<D*
>(x.get()) == y;
310 template <
class T>
bool operator==(
const unique_ptr<T>& x, intptr_t y) {
311 return reinterpret_cast<intptr_t
>(x.get()) == y;
314 template <
class T>
bool operator!=(
const unique_ptr<T>& x,
decltype(
nullptr)) {
318 template <
class T>
bool operator!=(
decltype(
nullptr),
const unique_ptr<T>& x) {
322 template <
class T>
bool operator==(
const unique_ptr<T>& x,
decltype(
nullptr)) {
326 template <
class T>
bool operator==(
decltype(
nullptr),
const unique_ptr<T>& x) {
332#ifdef FLATBUFFERS_USE_STD_OPTIONAL
334using Optional = std::optional<T>;
335using nullopt_t = std::nullopt_t;
336inline constexpr nullopt_t nullopt = std::nullopt;
348#if defined(FLATBUFFERS_CONSTEXPR_DEFINED)
350 template <
class>
struct nullopt_holder {
351 static constexpr nullopt_t
instance_ = nullopt_t(0);
353 template<
class Dummy>
363 template<
class Dummy>
380 FLATBUFFERS_CONSTEXPR_CPP11
Optional() FLATBUFFERS_NOEXCEPT
381 : value_(), has_value_(false) {}
384 : value_(), has_value_(
false) {}
386 FLATBUFFERS_CONSTEXPR_CPP11
Optional(T val) FLATBUFFERS_NOEXCEPT
387 : value_(val), has_value_(
true) {}
389 FLATBUFFERS_CONSTEXPR_CPP11
Optional(
const Optional &other) FLATBUFFERS_NOEXCEPT
390 : value_(other.value_), has_value_(other.has_value_) {}
392 FLATBUFFERS_CONSTEXPR_CPP14 Optional &
operator=(
const Optional &other) FLATBUFFERS_NOEXCEPT {
393 value_ = other.value_;
394 has_value_ = other.has_value_;
404 FLATBUFFERS_CONSTEXPR_CPP14 Optional &
operator=(T val) FLATBUFFERS_NOEXCEPT {
414 void swap(Optional &other) FLATBUFFERS_NOEXCEPT {
415 std::swap(value_, other.value_);
416 std::swap(has_value_, other.has_value_);
419 FLATBUFFERS_CONSTEXPR_CPP11 FLATBUFFERS_EXPLICIT_CPP11
operator bool() const FLATBUFFERS_NOEXCEPT {
423 FLATBUFFERS_CONSTEXPR_CPP11
bool has_value() const FLATBUFFERS_NOEXCEPT {
427 FLATBUFFERS_CONSTEXPR_CPP11
const T&
operator*() const FLATBUFFERS_NOEXCEPT {
436 T
value_or(T default_value)
const FLATBUFFERS_NOEXCEPT {
437 return has_value() ? value_ : default_value;
454template<
class T,
class U>
455FLATBUFFERS_CONSTEXPR_CPP11
bool operator==(
const Optional<T>& lhs,
const U& rhs) FLATBUFFERS_NOEXCEPT {
456 return static_cast<bool>(lhs) && (*lhs == rhs);
459template<
class T,
class U>
460FLATBUFFERS_CONSTEXPR_CPP11
bool operator==(
const T& lhs,
const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
461 return static_cast<bool>(rhs) && (lhs == *rhs);
464template<
class T,
class U>
465FLATBUFFERS_CONSTEXPR_CPP11
bool operator==(
const Optional<T>& lhs,
const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
466 return static_cast<bool>(lhs) !=
static_cast<bool>(rhs)
468 : !
static_cast<bool>(lhs) ?
false : (*lhs == *rhs);
474#if defined(FLATBUFFERS_USE_STD_SPAN)
475 inline constexpr std::size_t
dynamic_extent = std::dynamic_extent;
476 template<
class T, std::
size_t Extent = std::dynamic_extent>
477 using span = std::span<T, Extent>;
484#if !defined(FLATBUFFERS_SPAN_MINIMAL)
491 template<
class E, std::
size_t Extent,
class U, std::
size_t N>
492 struct is_span_convertable {
494 typename std::conditional<std::is_convertible<U (*)[], E (*)[]>::value
505template<
class T, std::
size_t Extent = dynamic_extent>
506class span FLATBUFFERS_FINAL_CLASS {
523 FLATBUFFERS_CONSTEXPR_CPP11
529 FLATBUFFERS_CONSTEXPR_CPP11
bool empty() const FLATBUFFERS_NOEXCEPT {
534 FLATBUFFERS_CONSTEXPR_CPP11
pointer data() const FLATBUFFERS_NOEXCEPT {
544 FLATBUFFERS_CONSTEXPR_CPP11
span(
const span &other) FLATBUFFERS_NOEXCEPT
545 : data_(other.data_), count_(other.count_) {}
547 FLATBUFFERS_CONSTEXPR_CPP14 span &
operator=(
const span &other)
548 FLATBUFFERS_NOEXCEPT {
550 count_ = other.count_;
560 FLATBUFFERS_CONSTEXPR_CPP11
562 : data_ (Extent == dynamic_extent ? first : (Extent == count ? first : nullptr)),
563 count_(Extent ==
dynamic_extent ? count : (Extent == count ? Extent : 0)) {
569 #if defined(FLATBUFFERS_SPAN_MINIMAL)
570 FLATBUFFERS_CONSTEXPR_CPP11
span() FLATBUFFERS_NOEXCEPT : data_(
nullptr),
572 static_assert(extent == 0 || extent ==
dynamic_extent,
"invalid span");
580 template<std::size_t N = 0,
581 typename internal::is_span_convertable<element_type, Extent, element_type, (N - N)>::type = 0>
582 FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT : data_(
nullptr),
584 static_assert(extent == 0 || extent ==
dynamic_extent,
"invalid span");
593 template<std::size_t N,
594 typename internal::is_span_convertable<element_type, Extent, element_type, N>::type = 0>
595 FLATBUFFERS_CONSTEXPR_CPP11 span(element_type (&arr)[N]) FLATBUFFERS_NOEXCEPT
596 : data_(arr), count_(N) {}
598 template<
class U, std::size_t N,
599 typename internal::is_span_convertable<element_type, Extent, U, N>::type = 0>
600 FLATBUFFERS_CONSTEXPR_CPP11 span(std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT
601 : data_(
arr.data()), count_(N) {}
608 template<
class U, std::size_t N,
609 typename internal::is_span_convertable<element_type, Extent, U, N>::type = 0>
610 FLATBUFFERS_CONSTEXPR_CPP11 span(
const std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT
611 : data_(
arr.data()), count_(N) {}
618 template<
class U, std::size_t N,
619 typename internal::is_span_convertable<element_type, Extent, U, N>::type = 0>
620 FLATBUFFERS_CONSTEXPR_CPP11 span(
const flatbuffers::span<U, N> &s) FLATBUFFERS_NOEXCEPT
621 : span(
s.data(),
s.size()) {
629 const size_type count_;
632 #if !defined(FLATBUFFERS_SPAN_MINIMAL)
633 template<
class U, std::
size_t N>
634 FLATBUFFERS_CONSTEXPR_CPP11
635 flatbuffers::span<U, N> make_span(U(&arr)[N]) FLATBUFFERS_NOEXCEPT {
636 return span<U, N>(arr);
639 template<
class U, std::
size_t N>
640 FLATBUFFERS_CONSTEXPR_CPP11
641 flatbuffers::span<const U, N> make_span(
const U(&arr)[N]) FLATBUFFERS_NOEXCEPT {
642 return span<const U, N>(arr);
645 template<
class U, std::
size_t N>
646 FLATBUFFERS_CONSTEXPR_CPP11
647 flatbuffers::span<U, N> make_span(std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
648 return span<U, N>(arr);
651 template<
class U, std::
size_t N>
652 FLATBUFFERS_CONSTEXPR_CPP11
653 flatbuffers::span<const U, N> make_span(
const std::array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
654 return span<const U, N>(arr);
657 template<
class U, std::
size_t N>
658 FLATBUFFERS_CONSTEXPR_CPP11
659 flatbuffers::span<U, dynamic_extent> make_span(U *first, std::size_t count) FLATBUFFERS_NOEXCEPT {
660 return span<U, dynamic_extent>(first, count);
663 template<
class U, std::
size_t N>
664 FLATBUFFERS_CONSTEXPR_CPP11
665 flatbuffers::span<const U, dynamic_extent> make_span(
const U *first, std::size_t count) FLATBUFFERS_NOEXCEPT {
666 return span<const U, dynamic_extent>(first, count);
nest::Expr operator*(const nest::Expr &, const nest::Expr &)
#define FLATBUFFERS_ASSERT
void reset() FLATBUFFERS_NOEXCEPT
T value_or(T default_value) const FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP14 Optional & operator=(T val) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 pointer data() const FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 Optional(const Optional &other) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 Optional(T val) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 Optional(nullopt_t) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 span() FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 Optional() FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 span(const span &other) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 size_type size_bytes() const FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP14 span & operator=(const span &other) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 bool has_value() const FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 reference operator[](size_type idx) const
FLATBUFFERS_CONSTEXPR_CPP11 const T & operator*() const FLATBUFFERS_NOEXCEPT
void swap(Optional &other) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 size_type size() const FLATBUFFERS_NOEXCEPT
const T & const_reference
FLATBUFFERS_CONSTEXPR_CPP14 Optional & operator=(const Optional &other) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 bool empty() const FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP11 span(pointer first, size_type count) FLATBUFFERS_NOEXCEPT
FLATBUFFERS_CONSTEXPR_CPP14 Optional & operator=(nullopt_t) FLATBUFFERS_NOEXCEPT
unique_ptr & operator=(std::unique_ptr< T > &&u)
unique_ptr(std::unique_ptr< T > &&u)
unique_ptr & operator=(unique_ptr &&u)
unique_ptr(unique_ptr &&u)
unique_ptr & operator=(T *p)
KnobTrait< K >::ValueType get(void)
bool operator!=(const EnumVal &lhs, const EnumVal &rhs)
char & string_back(std::string &value)
bool operator==(const EnumVal &lhs, const EnumVal &rhs)
void vector_emplace_back(std::vector< T > *vector, V &&data)
T * vector_data(std::vector< T > &vector)
FLATBUFFERS_CONSTEXPR std::size_t dynamic_extent
const T * data(const std::vector< T, Alloc > &v)
static const nullopt_t instance_
FLATBUFFERS_CONSTEXPR_CPP11 nullopt_t(int)