stl_emulation.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Google Inc. All rights reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef FLATBUFFERS_STL_EMULATION_H_
00018 #define FLATBUFFERS_STL_EMULATION_H_
00019 
00020 // clang-format off
00021 
00022 #include <string>
00023 #include <type_traits>
00024 #include <vector>
00025 #include <memory>
00026 #include <limits>
00027 
00028 #if defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
00029   #define FLATBUFFERS_CPP98_STL
00030 #endif  // defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
00031 
00032 #if defined(FLATBUFFERS_CPP98_STL)
00033   #include <cctype>
00034 #endif  // defined(FLATBUFFERS_CPP98_STL)
00035 
00036 // This header provides backwards compatibility for C++98 STLs like stlport.
00037 namespace flatbuffers {
00038 
00039 // Retrieve ::back() from a string in a way that is compatible with pre C++11
00040 // STLs (e.g stlport).
00041 inline char& string_back(std::string &value) {
00042   return value[value.length() - 1];
00043 }
00044 
00045 inline char string_back(const std::string &value) {
00046   return value[value.length() - 1];
00047 }
00048 
00049 // Helper method that retrieves ::data() from a vector in a way that is
00050 // compatible with pre C++11 STLs (e.g stlport).
00051 template <typename T> inline T *vector_data(std::vector<T> &vector) {
00052   // In some debug environments, operator[] does bounds checking, so &vector[0]
00053   // can't be used.
00054   return vector.empty() ? nullptr : &vector[0];
00055 }
00056 
00057 template <typename T> inline const T *vector_data(
00058     const std::vector<T> &vector) {
00059   return vector.empty() ? nullptr : &vector[0];
00060 }
00061 
00062 template <typename T, typename V>
00063 inline void vector_emplace_back(std::vector<T> *vector, V &&data) {
00064   #if defined(FLATBUFFERS_CPP98_STL)
00065     vector->push_back(data);
00066   #else
00067     vector->emplace_back(std::forward<V>(data));
00068   #endif  // defined(FLATBUFFERS_CPP98_STL)
00069 }
00070 
00071 #ifndef FLATBUFFERS_CPP98_STL
00072   #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
00073     template <typename T>
00074     using numeric_limits = std::numeric_limits<T>;
00075   #else
00076     template <typename T> class numeric_limits :
00077       public std::numeric_limits<T> {};
00078   #endif  // !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
00079 #else
00080   template <typename T> class numeric_limits :
00081       public std::numeric_limits<T> {};
00082 
00083   template <> class numeric_limits<unsigned long long> {
00084    public:
00085     static unsigned long long min() { return 0ULL; }
00086     static unsigned long long max() { return ~0ULL; }
00087   };
00088 
00089   template <> class numeric_limits<long long> {
00090    public:
00091     static long long min() {
00092       return static_cast<long long>(1ULL << ((sizeof(long long) << 3) - 1));
00093     }
00094     static long long max() {
00095       return static_cast<long long>(
00096           (1ULL << ((sizeof(long long) << 3) - 1)) - 1);
00097     }
00098   };
00099 #endif  // FLATBUFFERS_CPP98_STL
00100 
00101 #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
00102   #ifndef FLATBUFFERS_CPP98_STL
00103     template <typename T> using is_scalar = std::is_scalar<T>;
00104     template <typename T, typename U> using is_same = std::is_same<T,U>;
00105     template <typename T> using is_floating_point = std::is_floating_point<T>;
00106     template <typename T> using is_unsigned = std::is_unsigned<T>;
00107   #else
00108     // Map C++ TR1 templates defined by stlport.
00109     template <typename T> using is_scalar = std::tr1::is_scalar<T>;
00110     template <typename T, typename U> using is_same = std::tr1::is_same<T,U>;
00111     template <typename T> using is_floating_point =
00112         std::tr1::is_floating_point<T>;
00113     template <typename T> using is_unsigned = std::tr1::is_unsigned<T>;
00114   #endif  // !FLATBUFFERS_CPP98_STL
00115 #else
00116   // MSVC 2010 doesn't support C++11 aliases.
00117   template <typename T> struct is_scalar : public std::is_scalar<T> {};
00118   template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
00119   template <typename T> struct is_floating_point :
00120         public std::is_floating_point<T> {};
00121   template <typename T> struct is_unsigned : public std::is_unsigned<T> {};
00122 #endif  // !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
00123 
00124 #ifndef FLATBUFFERS_CPP98_STL
00125   #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
00126     template <class T> using unique_ptr = std::unique_ptr<T>;
00127   #else
00128     // MSVC 2010 doesn't support C++11 aliases.
00129     // We're manually "aliasing" the class here as we want to bring unique_ptr
00130     // into the flatbuffers namespace.  We have unique_ptr in the flatbuffers
00131     // namespace we have a completely independent implemenation (see below)
00132     // for C++98 STL implementations.
00133     template <class T> class unique_ptr : public std::unique_ptr<T> {
00134      public:
00135       unique_ptr() {}
00136       explicit unique_ptr(T* p) : std::unique_ptr<T>(p) {}
00137       unique_ptr(std::unique_ptr<T>&& u) { *this = std::move(u); }
00138       unique_ptr(unique_ptr&& u) { *this = std::move(u); }
00139       unique_ptr& operator=(std::unique_ptr<T>&& u) {
00140         std::unique_ptr<T>::reset(u.release());
00141         return *this;
00142       }
00143       unique_ptr& operator=(unique_ptr&& u) {
00144         std::unique_ptr<T>::reset(u.release());
00145         return *this;
00146       }
00147       unique_ptr& operator=(T* p) {
00148         return std::unique_ptr<T>::operator=(p);
00149       }
00150     };
00151   #endif  // !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
00152 #else
00153   // Very limited implementation of unique_ptr.
00154   // This is provided simply to allow the C++ code generated from the default
00155   // settings to function in C++98 environments with no modifications.
00156   template <class T> class unique_ptr {
00157    public:
00158     typedef T element_type;
00159 
00160     unique_ptr() : ptr_(nullptr) {}
00161     explicit unique_ptr(T* p) : ptr_(p) {}
00162     unique_ptr(unique_ptr&& u) : ptr_(nullptr) { reset(u.release()); }
00163     unique_ptr(const unique_ptr& u) : ptr_(nullptr) {
00164       reset(const_cast<unique_ptr*>(&u)->release());
00165     }
00166     ~unique_ptr() { reset(); }
00167 
00168     unique_ptr& operator=(const unique_ptr& u) {
00169       reset(const_cast<unique_ptr*>(&u)->release());
00170       return *this;
00171     }
00172 
00173     unique_ptr& operator=(unique_ptr&& u) {
00174       reset(u.release());
00175       return *this;
00176     }
00177 
00178     unique_ptr& operator=(T* p) {
00179       reset(p);
00180       return *this;
00181     }
00182 
00183     const T& operator*() const { return *ptr_; }
00184     T* operator->() const { return ptr_; }
00185     T* get() const noexcept { return ptr_; }
00186     explicit operator bool() const { return ptr_ != nullptr; }
00187 
00188     // modifiers
00189     T* release() {
00190       T* value = ptr_;
00191       ptr_ = nullptr;
00192       return value;
00193     }
00194 
00195     void reset(T* p = nullptr) {
00196       T* value = ptr_;
00197       ptr_ = p;
00198       if (value) delete value;
00199     }
00200 
00201     void swap(unique_ptr& u) {
00202       T* temp_ptr = ptr_;
00203       ptr_ = u.ptr_;
00204       u.ptr_ = temp_ptr;
00205     }
00206 
00207    private:
00208     T* ptr_;
00209   };
00210 
00211   template <class T> bool operator==(const unique_ptr<T>& x,
00212                                      const unique_ptr<T>& y) {
00213     return x.get() == y.get();
00214   }
00215 
00216   template <class T, class D> bool operator==(const unique_ptr<T>& x,
00217                                               const D* y) {
00218     return static_cast<D*>(x.get()) == y;
00219   }
00220 
00221   template <class T> bool operator==(const unique_ptr<T>& x, intptr_t y) {
00222     return reinterpret_cast<intptr_t>(x.get()) == y;
00223   }
00224 #endif  // !FLATBUFFERS_CPP98_STL
00225 
00226 }  // namespace flatbuffers
00227 
00228 #endif  // FLATBUFFERS_STL_EMULATION_H_


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10