stl_emulation.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  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. #ifndef FLATBUFFERS_STL_EMULATION_H_
  17. #define FLATBUFFERS_STL_EMULATION_H_
  18. // clang-format off
  19. #include <string>
  20. #include <type_traits>
  21. #include <vector>
  22. #include <memory>
  23. #include <limits>
  24. #if defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
  25. #define FLATBUFFERS_CPP98_STL
  26. #endif // defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
  27. #if defined(FLATBUFFERS_CPP98_STL)
  28. #include <cctype>
  29. #endif // defined(FLATBUFFERS_CPP98_STL)
  30. // Check if we can use template aliases
  31. // Not possible if Microsoft Compiler before 2012
  32. // Possible is the language feature __cpp_alias_templates is defined well
  33. // Or possible if the C++ std is C+11 or newer
  34. #if (defined(_MSC_VER) && _MSC_VER > 1700 /* MSVC2012 */) \
  35. || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \
  36. || (defined(__cplusplus) && __cplusplus >= 201103L)
  37. #define FLATBUFFERS_TEMPLATES_ALIASES
  38. #endif
  39. // This header provides backwards compatibility for C++98 STLs like stlport.
  40. namespace flatbuffers {
  41. // Retrieve ::back() from a string in a way that is compatible with pre C++11
  42. // STLs (e.g stlport).
  43. inline char& string_back(std::string &value) {
  44. return value[value.length() - 1];
  45. }
  46. inline char string_back(const std::string &value) {
  47. return value[value.length() - 1];
  48. }
  49. // Helper method that retrieves ::data() from a vector in a way that is
  50. // compatible with pre C++11 STLs (e.g stlport).
  51. template <typename T> inline T *vector_data(std::vector<T> &vector) {
  52. // In some debug environments, operator[] does bounds checking, so &vector[0]
  53. // can't be used.
  54. return vector.empty() ? nullptr : &vector[0];
  55. }
  56. template <typename T> inline const T *vector_data(
  57. const std::vector<T> &vector) {
  58. return vector.empty() ? nullptr : &vector[0];
  59. }
  60. template <typename T, typename V>
  61. inline void vector_emplace_back(std::vector<T> *vector, V &&data) {
  62. #if defined(FLATBUFFERS_CPP98_STL)
  63. vector->push_back(data);
  64. #else
  65. vector->emplace_back(std::forward<V>(data));
  66. #endif // defined(FLATBUFFERS_CPP98_STL)
  67. }
  68. #ifndef FLATBUFFERS_CPP98_STL
  69. #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
  70. template <typename T>
  71. using numeric_limits = std::numeric_limits<T>;
  72. #else
  73. template <typename T> class numeric_limits :
  74. public std::numeric_limits<T> {};
  75. #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES)
  76. #else
  77. template <typename T> class numeric_limits :
  78. public std::numeric_limits<T> {
  79. public:
  80. // Android NDK fix.
  81. static T lowest() {
  82. return std::numeric_limits<T>::min();
  83. }
  84. };
  85. template <> class numeric_limits<float> :
  86. public std::numeric_limits<float> {
  87. public:
  88. static float lowest() { return -FLT_MAX; }
  89. };
  90. template <> class numeric_limits<double> :
  91. public std::numeric_limits<double> {
  92. public:
  93. static double lowest() { return -DBL_MAX; }
  94. };
  95. template <> class numeric_limits<unsigned long long> {
  96. public:
  97. static unsigned long long min() { return 0ULL; }
  98. static unsigned long long max() { return ~0ULL; }
  99. static unsigned long long lowest() {
  100. return numeric_limits<unsigned long long>::min();
  101. }
  102. };
  103. template <> class numeric_limits<long long> {
  104. public:
  105. static long long min() {
  106. return static_cast<long long>(1ULL << ((sizeof(long long) << 3) - 1));
  107. }
  108. static long long max() {
  109. return static_cast<long long>(
  110. (1ULL << ((sizeof(long long) << 3) - 1)) - 1);
  111. }
  112. static long long lowest() {
  113. return numeric_limits<long long>::min();
  114. }
  115. };
  116. #endif // FLATBUFFERS_CPP98_STL
  117. #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
  118. #ifndef FLATBUFFERS_CPP98_STL
  119. template <typename T> using is_scalar = std::is_scalar<T>;
  120. template <typename T, typename U> using is_same = std::is_same<T,U>;
  121. template <typename T> using is_floating_point = std::is_floating_point<T>;
  122. template <typename T> using is_unsigned = std::is_unsigned<T>;
  123. template <typename T> using is_enum = std::is_enum<T>;
  124. template <typename T> using make_unsigned = std::make_unsigned<T>;
  125. template<bool B, class T, class F>
  126. using conditional = std::conditional<B, T, F>;
  127. template<class T, T v>
  128. using integral_constant = std::integral_constant<T, v>;
  129. #else
  130. // Map C++ TR1 templates defined by stlport.
  131. template <typename T> using is_scalar = std::tr1::is_scalar<T>;
  132. template <typename T, typename U> using is_same = std::tr1::is_same<T,U>;
  133. template <typename T> using is_floating_point =
  134. std::tr1::is_floating_point<T>;
  135. template <typename T> using is_unsigned = std::tr1::is_unsigned<T>;
  136. template <typename T> using is_enum = std::tr1::is_enum<T>;
  137. // Android NDK doesn't have std::make_unsigned or std::tr1::make_unsigned.
  138. template<typename T> struct make_unsigned {
  139. static_assert(is_unsigned<T>::value, "Specialization not implemented!");
  140. using type = T;
  141. };
  142. template<> struct make_unsigned<char> { using type = unsigned char; };
  143. template<> struct make_unsigned<short> { using type = unsigned short; };
  144. template<> struct make_unsigned<int> { using type = unsigned int; };
  145. template<> struct make_unsigned<long> { using type = unsigned long; };
  146. template<>
  147. struct make_unsigned<long long> { using type = unsigned long long; };
  148. template<bool B, class T, class F>
  149. using conditional = std::tr1::conditional<B, T, F>;
  150. template<class T, T v>
  151. using integral_constant = std::tr1::integral_constant<T, v>;
  152. #endif // !FLATBUFFERS_CPP98_STL
  153. #else
  154. // MSVC 2010 doesn't support C++11 aliases.
  155. template <typename T> struct is_scalar : public std::is_scalar<T> {};
  156. template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
  157. template <typename T> struct is_floating_point :
  158. public std::is_floating_point<T> {};
  159. template <typename T> struct is_unsigned : public std::is_unsigned<T> {};
  160. template <typename T> struct is_enum : public std::is_enum<T> {};
  161. template <typename T> struct make_unsigned : public std::make_unsigned<T> {};
  162. template<bool B, class T, class F>
  163. struct conditional : public std::conditional<B, T, F> {};
  164. template<class T, T v>
  165. struct integral_constant : public std::integral_constant<T, v> {};
  166. #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES)
  167. #ifndef FLATBUFFERS_CPP98_STL
  168. #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
  169. template <class T> using unique_ptr = std::unique_ptr<T>;
  170. #else
  171. // MSVC 2010 doesn't support C++11 aliases.
  172. // We're manually "aliasing" the class here as we want to bring unique_ptr
  173. // into the flatbuffers namespace. We have unique_ptr in the flatbuffers
  174. // namespace we have a completely independent implemenation (see below)
  175. // for C++98 STL implementations.
  176. template <class T> class unique_ptr : public std::unique_ptr<T> {
  177. public:
  178. unique_ptr() {}
  179. explicit unique_ptr(T* p) : std::unique_ptr<T>(p) {}
  180. unique_ptr(std::unique_ptr<T>&& u) { *this = std::move(u); }
  181. unique_ptr(unique_ptr&& u) { *this = std::move(u); }
  182. unique_ptr& operator=(std::unique_ptr<T>&& u) {
  183. std::unique_ptr<T>::reset(u.release());
  184. return *this;
  185. }
  186. unique_ptr& operator=(unique_ptr&& u) {
  187. std::unique_ptr<T>::reset(u.release());
  188. return *this;
  189. }
  190. unique_ptr& operator=(T* p) {
  191. return std::unique_ptr<T>::operator=(p);
  192. }
  193. };
  194. #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES)
  195. #else
  196. // Very limited implementation of unique_ptr.
  197. // This is provided simply to allow the C++ code generated from the default
  198. // settings to function in C++98 environments with no modifications.
  199. template <class T> class unique_ptr {
  200. public:
  201. typedef T element_type;
  202. unique_ptr() : ptr_(nullptr) {}
  203. explicit unique_ptr(T* p) : ptr_(p) {}
  204. unique_ptr(unique_ptr&& u) : ptr_(nullptr) { reset(u.release()); }
  205. unique_ptr(const unique_ptr& u) : ptr_(nullptr) {
  206. reset(const_cast<unique_ptr*>(&u)->release());
  207. }
  208. ~unique_ptr() { reset(); }
  209. unique_ptr& operator=(const unique_ptr& u) {
  210. reset(const_cast<unique_ptr*>(&u)->release());
  211. return *this;
  212. }
  213. unique_ptr& operator=(unique_ptr&& u) {
  214. reset(u.release());
  215. return *this;
  216. }
  217. unique_ptr& operator=(T* p) {
  218. reset(p);
  219. return *this;
  220. }
  221. const T& operator*() const { return *ptr_; }
  222. T* operator->() const { return ptr_; }
  223. T* get() const noexcept { return ptr_; }
  224. explicit operator bool() const { return ptr_ != nullptr; }
  225. // modifiers
  226. T* release() {
  227. T* value = ptr_;
  228. ptr_ = nullptr;
  229. return value;
  230. }
  231. void reset(T* p = nullptr) {
  232. T* value = ptr_;
  233. ptr_ = p;
  234. if (value) delete value;
  235. }
  236. void swap(unique_ptr& u) {
  237. T* temp_ptr = ptr_;
  238. ptr_ = u.ptr_;
  239. u.ptr_ = temp_ptr;
  240. }
  241. private:
  242. T* ptr_;
  243. };
  244. template <class T> bool operator==(const unique_ptr<T>& x,
  245. const unique_ptr<T>& y) {
  246. return x.get() == y.get();
  247. }
  248. template <class T, class D> bool operator==(const unique_ptr<T>& x,
  249. const D* y) {
  250. return static_cast<D*>(x.get()) == y;
  251. }
  252. template <class T> bool operator==(const unique_ptr<T>& x, intptr_t y) {
  253. return reinterpret_cast<intptr_t>(x.get()) == y;
  254. }
  255. template <class T> bool operator!=(const unique_ptr<T>& x, decltype(nullptr)) {
  256. return !!x;
  257. }
  258. template <class T> bool operator!=(decltype(nullptr), const unique_ptr<T>& x) {
  259. return !!x;
  260. }
  261. template <class T> bool operator==(const unique_ptr<T>& x, decltype(nullptr)) {
  262. return !x;
  263. }
  264. template <class T> bool operator==(decltype(nullptr), const unique_ptr<T>& x) {
  265. return !x;
  266. }
  267. #endif // !FLATBUFFERS_CPP98_STL
  268. } // namespace flatbuffers
  269. #endif // FLATBUFFERS_STL_EMULATION_H_