/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FLATBUFFERS_STL_EMULATION_H_ #define FLATBUFFERS_STL_EMULATION_H_ // clang-format off #include "flatbuffers/base.h" #include #include #include #include #include // Detect C++17 compatible compiler. // __cplusplus >= 201703L - a compiler has support of 'static inline' variables. #if defined(FLATBUFFERS_USE_STD_OPTIONAL) \ || (defined(__cplusplus) && __cplusplus >= 201703L) \ || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) #include #ifndef FLATBUFFERS_USE_STD_OPTIONAL #define FLATBUFFERS_USE_STD_OPTIONAL #endif #endif #if defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL) #define FLATBUFFERS_CPP98_STL #endif // defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL) #if defined(FLATBUFFERS_CPP98_STL) #include #endif // defined(FLATBUFFERS_CPP98_STL) // This header provides backwards compatibility for C++98 STLs like stlport. namespace flatbuffers { // Retrieve ::back() from a string in a way that is compatible with pre C++11 // STLs (e.g stlport). inline char& string_back(std::string &value) { return value[value.length() - 1]; } inline char string_back(const std::string &value) { return value[value.length() - 1]; } // Helper method that retrieves ::data() from a vector in a way that is // compatible with pre C++11 STLs (e.g stlport). template inline T *vector_data(std::vector &vector) { // In some debug environments, operator[] does bounds checking, so &vector[0] // can't be used. return vector.empty() ? nullptr : &vector[0]; } template inline const T *vector_data( const std::vector &vector) { return vector.empty() ? nullptr : &vector[0]; } template inline void vector_emplace_back(std::vector *vector, V &&data) { #if defined(FLATBUFFERS_CPP98_STL) vector->push_back(data); #else vector->emplace_back(std::forward(data)); #endif // defined(FLATBUFFERS_CPP98_STL) } #ifndef FLATBUFFERS_CPP98_STL #if defined(FLATBUFFERS_TEMPLATES_ALIASES) template using numeric_limits = std::numeric_limits; #else template class numeric_limits : public std::numeric_limits {}; #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) #else template class numeric_limits : public std::numeric_limits { public: // Android NDK fix. static T lowest() { return std::numeric_limits::min(); } }; template <> class numeric_limits : public std::numeric_limits { public: static float lowest() { return -FLT_MAX; } }; template <> class numeric_limits : public std::numeric_limits { public: static double lowest() { return -DBL_MAX; } }; template <> class numeric_limits { public: static unsigned long long min() { return 0ULL; } static unsigned long long max() { return ~0ULL; } static unsigned long long lowest() { return numeric_limits::min(); } }; template <> class numeric_limits { public: static long long min() { return static_cast(1ULL << ((sizeof(long long) << 3) - 1)); } static long long max() { return static_cast( (1ULL << ((sizeof(long long) << 3) - 1)) - 1); } static long long lowest() { return numeric_limits::min(); } }; #endif // FLATBUFFERS_CPP98_STL #if defined(FLATBUFFERS_TEMPLATES_ALIASES) #ifndef FLATBUFFERS_CPP98_STL template using is_scalar = std::is_scalar; template using is_same = std::is_same; template using is_floating_point = std::is_floating_point; template using is_unsigned = std::is_unsigned; template using is_enum = std::is_enum; template using make_unsigned = std::make_unsigned; template using conditional = std::conditional; template using integral_constant = std::integral_constant; #else // Map C++ TR1 templates defined by stlport. template using is_scalar = std::tr1::is_scalar; template using is_same = std::tr1::is_same; template using is_floating_point = std::tr1::is_floating_point; template using is_unsigned = std::tr1::is_unsigned; template using is_enum = std::tr1::is_enum; // Android NDK doesn't have std::make_unsigned or std::tr1::make_unsigned. template struct make_unsigned { static_assert(is_unsigned::value, "Specialization not implemented!"); using type = T; }; template<> struct make_unsigned { using type = unsigned char; }; template<> struct make_unsigned { using type = unsigned short; }; template<> struct make_unsigned { using type = unsigned int; }; template<> struct make_unsigned { using type = unsigned long; }; template<> struct make_unsigned { using type = unsigned long long; }; template using conditional = std::tr1::conditional; template using integral_constant = std::tr1::integral_constant; #endif // !FLATBUFFERS_CPP98_STL #else // MSVC 2010 doesn't support C++11 aliases. template struct is_scalar : public std::is_scalar {}; template struct is_same : public std::is_same {}; template struct is_floating_point : public std::is_floating_point {}; template struct is_unsigned : public std::is_unsigned {}; template struct is_enum : public std::is_enum {}; template struct make_unsigned : public std::make_unsigned {}; template struct conditional : public std::conditional {}; template struct integral_constant : public std::integral_constant {}; #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) #ifndef FLATBUFFERS_CPP98_STL #if defined(FLATBUFFERS_TEMPLATES_ALIASES) template using unique_ptr = std::unique_ptr; #else // MSVC 2010 doesn't support C++11 aliases. // We're manually "aliasing" the class here as we want to bring unique_ptr // into the flatbuffers namespace. We have unique_ptr in the flatbuffers // namespace we have a completely independent implementation (see below) // for C++98 STL implementations. template class unique_ptr : public std::unique_ptr { public: unique_ptr() {} explicit unique_ptr(T* p) : std::unique_ptr(p) {} unique_ptr(std::unique_ptr&& u) { *this = std::move(u); } unique_ptr(unique_ptr&& u) { *this = std::move(u); } unique_ptr& operator=(std::unique_ptr&& u) { std::unique_ptr::reset(u.release()); return *this; } unique_ptr& operator=(unique_ptr&& u) { std::unique_ptr::reset(u.release()); return *this; } unique_ptr& operator=(T* p) { return std::unique_ptr::operator=(p); } }; #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) #else // Very limited implementation of unique_ptr. // This is provided simply to allow the C++ code generated from the default // settings to function in C++98 environments with no modifications. template class unique_ptr { public: typedef T element_type; unique_ptr() : ptr_(nullptr) {} explicit unique_ptr(T* p) : ptr_(p) {} unique_ptr(unique_ptr&& u) : ptr_(nullptr) { reset(u.release()); } unique_ptr(const unique_ptr& u) : ptr_(nullptr) { reset(const_cast(&u)->release()); } ~unique_ptr() { reset(); } unique_ptr& operator=(const unique_ptr& u) { reset(const_cast(&u)->release()); return *this; } unique_ptr& operator=(unique_ptr&& u) { reset(u.release()); return *this; } unique_ptr& operator=(T* p) { reset(p); return *this; } const T& operator*() const { return *ptr_; } T* operator->() const { return ptr_; } T* get() const noexcept { return ptr_; } explicit operator bool() const { return ptr_ != nullptr; } // modifiers T* release() { T* value = ptr_; ptr_ = nullptr; return value; } void reset(T* p = nullptr) { T* value = ptr_; ptr_ = p; if (value) delete value; } void swap(unique_ptr& u) { T* temp_ptr = ptr_; ptr_ = u.ptr_; u.ptr_ = temp_ptr; } private: T* ptr_; }; template bool operator==(const unique_ptr& x, const unique_ptr& y) { return x.get() == y.get(); } template bool operator==(const unique_ptr& x, const D* y) { return static_cast(x.get()) == y; } template bool operator==(const unique_ptr& x, intptr_t y) { return reinterpret_cast(x.get()) == y; } template bool operator!=(const unique_ptr& x, decltype(nullptr)) { return !!x; } template bool operator!=(decltype(nullptr), const unique_ptr& x) { return !!x; } template bool operator==(const unique_ptr& x, decltype(nullptr)) { return !x; } template bool operator==(decltype(nullptr), const unique_ptr& x) { return !x; } #endif // !FLATBUFFERS_CPP98_STL #ifdef FLATBUFFERS_USE_STD_OPTIONAL template using Optional = std::optional; using nullopt_t = std::nullopt_t; inline constexpr nullopt_t nullopt = std::nullopt; #else // Limited implementation of Optional type for a scalar T. // This implementation limited by trivial types compatible with // std::is_arithmetic or std::is_enum type traits. // A tag to indicate an empty flatbuffers::optional. struct nullopt_t { explicit FLATBUFFERS_CONSTEXPR_CPP11 nullopt_t(int) {} }; #if defined(FLATBUFFERS_CONSTEXPR_DEFINED) namespace internal { template struct nullopt_holder { static constexpr nullopt_t instance_ = nullopt_t(0); }; template constexpr nullopt_t nullopt_holder::instance_; } static constexpr const nullopt_t &nullopt = internal::nullopt_holder::instance_; #else namespace internal { template struct nullopt_holder { static const nullopt_t instance_; }; template const nullopt_t nullopt_holder::instance_ = nullopt_t(0); } static const nullopt_t &nullopt = internal::nullopt_holder::instance_; #endif template class Optional FLATBUFFERS_FINAL_CLASS { // Non-scalar 'T' would extremely complicated Optional. // Use is_scalar checking because flatbuffers flatbuffers::is_arithmetic // isn't implemented. static_assert(flatbuffers::is_scalar::value, "unexpected type T"); public: ~Optional() {} FLATBUFFERS_CONSTEXPR_CPP11 Optional() FLATBUFFERS_NOEXCEPT : value_(), has_value_(false) {} FLATBUFFERS_CONSTEXPR_CPP11 Optional(nullopt_t) FLATBUFFERS_NOEXCEPT : value_(), has_value_(false) {} FLATBUFFERS_CONSTEXPR_CPP11 Optional(T val) FLATBUFFERS_NOEXCEPT : value_(val), has_value_(true) {} FLATBUFFERS_CONSTEXPR_CPP11 Optional(const Optional &other) FLATBUFFERS_NOEXCEPT : value_(other.value_), has_value_(other.has_value_) {} FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(const Optional &other) FLATBUFFERS_NOEXCEPT { value_ = other.value_; has_value_ = other.has_value_; return *this; } FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(nullopt_t) FLATBUFFERS_NOEXCEPT { value_ = T(); has_value_ = false; return *this; } FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(T val) FLATBUFFERS_NOEXCEPT { value_ = val; has_value_ = true; return *this; } void reset() FLATBUFFERS_NOEXCEPT { *this = nullopt; } void swap(Optional &other) FLATBUFFERS_NOEXCEPT { std::swap(value_, other.value_); std::swap(has_value_, other.has_value_); } FLATBUFFERS_CONSTEXPR_CPP11 FLATBUFFERS_EXPLICIT_CPP11 operator bool() const FLATBUFFERS_NOEXCEPT { return has_value_; } FLATBUFFERS_CONSTEXPR_CPP11 bool has_value() const FLATBUFFERS_NOEXCEPT { return has_value_; } FLATBUFFERS_CONSTEXPR_CPP11 const T& operator*() const FLATBUFFERS_NOEXCEPT { return value_; } const T& value() const { FLATBUFFERS_ASSERT(has_value()); return value_; } T value_or(T default_value) const FLATBUFFERS_NOEXCEPT { return has_value() ? value_ : default_value; } private: T value_; bool has_value_; }; template FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional& opt, nullopt_t) FLATBUFFERS_NOEXCEPT { return !opt; } template FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(nullopt_t, const Optional& opt) FLATBUFFERS_NOEXCEPT { return !opt; } template FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional& lhs, const U& rhs) FLATBUFFERS_NOEXCEPT { return static_cast(lhs) && (*lhs == rhs); } template FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const T& lhs, const Optional& rhs) FLATBUFFERS_NOEXCEPT { return static_cast(rhs) && (lhs == *rhs); } template FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional& lhs, const Optional& rhs) FLATBUFFERS_NOEXCEPT { return static_cast(lhs) != static_cast(rhs) ? false : !static_cast(lhs) ? false : (*lhs == *rhs); } #endif // FLATBUFFERS_USE_STD_OPTIONAL } // namespace flatbuffers #endif // FLATBUFFERS_STL_EMULATION_H_