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 
17 #ifndef FLATBUFFERS_STL_EMULATION_H_
18 #define FLATBUFFERS_STL_EMULATION_H_
19 
20 // clang-format off
21 #include "flatbuffers/base.h"
22 
23 #include <string>
24 #include <type_traits>
25 #include <vector>
26 #include <memory>
27 #include <limits>
28 
29 // Detect C++17 compatible compiler.
30 // __cplusplus >= 201703L - a compiler has support of 'static inline' variables.
31 #if defined(FLATBUFFERS_USE_STD_OPTIONAL) \
32     || (defined(__cplusplus) && __cplusplus >= 201703L) \
33     || (defined(_MSVC_LANG) &&  (_MSVC_LANG >= 201703L))
34   #include <optional>
35   #ifndef FLATBUFFERS_USE_STD_OPTIONAL
36     #define FLATBUFFERS_USE_STD_OPTIONAL
37   #endif
38 #endif
39 
40 #if defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
41   #define FLATBUFFERS_CPP98_STL
42 #endif  // defined(_STLPORT_VERSION) && !defined(FLATBUFFERS_CPP98_STL)
43 
44 #if defined(FLATBUFFERS_CPP98_STL)
45   #include <cctype>
46 #endif  // defined(FLATBUFFERS_CPP98_STL)
47 
48 // This header provides backwards compatibility for C++98 STLs like stlport.
49 namespace flatbuffers {
50 
51 // Retrieve ::back() from a string in a way that is compatible with pre C++11
52 // STLs (e.g stlport).
string_back(std::string & value)53 inline char& string_back(std::string &value) {
54   return value[value.length() - 1];
55 }
56 
string_back(const std::string & value)57 inline char string_back(const std::string &value) {
58   return value[value.length() - 1];
59 }
60 
61 // Helper method that retrieves ::data() from a vector in a way that is
62 // compatible with pre C++11 STLs (e.g stlport).
vector_data(std::vector<T> & vector)63 template <typename T> inline T *vector_data(std::vector<T> &vector) {
64   // In some debug environments, operator[] does bounds checking, so &vector[0]
65   // can't be used.
66   return vector.empty() ? nullptr : &vector[0];
67 }
68 
vector_data(const std::vector<T> & vector)69 template <typename T> inline const T *vector_data(
70     const std::vector<T> &vector) {
71   return vector.empty() ? nullptr : &vector[0];
72 }
73 
74 template <typename T, typename V>
vector_emplace_back(std::vector<T> * vector,V && data)75 inline void vector_emplace_back(std::vector<T> *vector, V &&data) {
76   #if defined(FLATBUFFERS_CPP98_STL)
77     vector->push_back(data);
78   #else
79     vector->emplace_back(std::forward<V>(data));
80   #endif  // defined(FLATBUFFERS_CPP98_STL)
81 }
82 
83 #ifndef FLATBUFFERS_CPP98_STL
84   #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
85     template <typename T>
86     using numeric_limits = std::numeric_limits<T>;
87   #else
88     template <typename T> class numeric_limits :
89       public std::numeric_limits<T> {};
90   #endif  // defined(FLATBUFFERS_TEMPLATES_ALIASES)
91 #else
92   template <typename T> class numeric_limits :
93       public std::numeric_limits<T> {
94     public:
95       // Android NDK fix.
lowest()96       static T lowest() {
97         return std::numeric_limits<T>::min();
98       }
99   };
100 
101   template <> class numeric_limits<float> :
102       public std::numeric_limits<float> {
103     public:
lowest()104       static float lowest() { return -FLT_MAX; }
105   };
106 
107   template <> class numeric_limits<double> :
108       public std::numeric_limits<double> {
109     public:
lowest()110       static double lowest() { return -DBL_MAX; }
111   };
112 
113   template <> class numeric_limits<unsigned long long> {
114    public:
min()115     static unsigned long long min() { return 0ULL; }
max()116     static unsigned long long max() { return ~0ULL; }
lowest()117     static unsigned long long lowest() {
118       return numeric_limits<unsigned long long>::min();
119     }
120   };
121 
122   template <> class numeric_limits<long long> {
123    public:
min()124     static long long min() {
125       return static_cast<long long>(1ULL << ((sizeof(long long) << 3) - 1));
126     }
max()127     static long long max() {
128       return static_cast<long long>(
129           (1ULL << ((sizeof(long long) << 3) - 1)) - 1);
130     }
lowest()131     static long long lowest() {
132       return numeric_limits<long long>::min();
133     }
134   };
135 #endif  // FLATBUFFERS_CPP98_STL
136 
137 #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
138   #ifndef FLATBUFFERS_CPP98_STL
139     template <typename T> using is_scalar = std::is_scalar<T>;
140     template <typename T, typename U> using is_same = std::is_same<T,U>;
141     template <typename T> using is_floating_point = std::is_floating_point<T>;
142     template <typename T> using is_unsigned = std::is_unsigned<T>;
143     template <typename T> using is_enum = std::is_enum<T>;
144     template <typename T> using make_unsigned = std::make_unsigned<T>;
145     template<bool B, class T, class F>
146     using conditional = std::conditional<B, T, F>;
147     template<class T, T v>
148     using integral_constant = std::integral_constant<T, v>;
149   #else
150     // Map C++ TR1 templates defined by stlport.
151     template <typename T> using is_scalar = std::tr1::is_scalar<T>;
152     template <typename T, typename U> using is_same = std::tr1::is_same<T,U>;
153     template <typename T> using is_floating_point =
154         std::tr1::is_floating_point<T>;
155     template <typename T> using is_unsigned = std::tr1::is_unsigned<T>;
156     template <typename T> using is_enum = std::tr1::is_enum<T>;
157     // Android NDK doesn't have std::make_unsigned or std::tr1::make_unsigned.
158     template<typename T> struct make_unsigned {
159       static_assert(is_unsigned<T>::value, "Specialization not implemented!");
160       using type = T;
161     };
162     template<> struct make_unsigned<char> { using type = unsigned char; };
163     template<> struct make_unsigned<short> { using type = unsigned short; };
164     template<> struct make_unsigned<int> { using type = unsigned int; };
165     template<> struct make_unsigned<long> { using type = unsigned long; };
166     template<>
167     struct make_unsigned<long long> { using type = unsigned long long; };
168     template<bool B, class T, class F>
169     using conditional = std::tr1::conditional<B, T, F>;
170     template<class T, T v>
171     using integral_constant = std::tr1::integral_constant<T, v>;
172   #endif  // !FLATBUFFERS_CPP98_STL
173 #else
174   // MSVC 2010 doesn't support C++11 aliases.
175   template <typename T> struct is_scalar : public std::is_scalar<T> {};
176   template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
177   template <typename T> struct is_floating_point :
178         public std::is_floating_point<T> {};
179   template <typename T> struct is_unsigned : public std::is_unsigned<T> {};
180   template <typename T> struct is_enum : public std::is_enum<T> {};
181   template <typename T> struct make_unsigned : public std::make_unsigned<T> {};
182   template<bool B, class T, class F>
183   struct conditional : public std::conditional<B, T, F> {};
184   template<class T, T v>
185   struct integral_constant : public std::integral_constant<T, v> {};
186 #endif  // defined(FLATBUFFERS_TEMPLATES_ALIASES)
187 
188 #ifndef FLATBUFFERS_CPP98_STL
189   #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
190     template <class T> using unique_ptr = std::unique_ptr<T>;
191   #else
192     // MSVC 2010 doesn't support C++11 aliases.
193     // We're manually "aliasing" the class here as we want to bring unique_ptr
194     // into the flatbuffers namespace.  We have unique_ptr in the flatbuffers
195     // namespace we have a completely independent implementation (see below)
196     // for C++98 STL implementations.
197     template <class T> class unique_ptr : public std::unique_ptr<T> {
198      public:
199       unique_ptr() {}
200       explicit unique_ptr(T* p) : std::unique_ptr<T>(p) {}
201       unique_ptr(std::unique_ptr<T>&& u) { *this = std::move(u); }
202       unique_ptr(unique_ptr&& u) { *this = std::move(u); }
203       unique_ptr& operator=(std::unique_ptr<T>&& u) {
204         std::unique_ptr<T>::reset(u.release());
205         return *this;
206       }
207       unique_ptr& operator=(unique_ptr&& u) {
208         std::unique_ptr<T>::reset(u.release());
209         return *this;
210       }
211       unique_ptr& operator=(T* p) {
212         return std::unique_ptr<T>::operator=(p);
213       }
214     };
215   #endif  // defined(FLATBUFFERS_TEMPLATES_ALIASES)
216 #else
217   // Very limited implementation of unique_ptr.
218   // This is provided simply to allow the C++ code generated from the default
219   // settings to function in C++98 environments with no modifications.
220   template <class T> class unique_ptr {
221    public:
222     typedef T element_type;
223 
224     unique_ptr() : ptr_(nullptr) {}
225     explicit unique_ptr(T* p) : ptr_(p) {}
226     unique_ptr(unique_ptr&& u) : ptr_(nullptr) { reset(u.release()); }
227     unique_ptr(const unique_ptr& u) : ptr_(nullptr) {
228       reset(const_cast<unique_ptr*>(&u)->release());
229     }
230     ~unique_ptr() { reset(); }
231 
232     unique_ptr& operator=(const unique_ptr& u) {
233       reset(const_cast<unique_ptr*>(&u)->release());
234       return *this;
235     }
236 
237     unique_ptr& operator=(unique_ptr&& u) {
238       reset(u.release());
239       return *this;
240     }
241 
242     unique_ptr& operator=(T* p) {
243       reset(p);
244       return *this;
245     }
246 
247     const T& operator*() const { return *ptr_; }
248     T* operator->() const { return ptr_; }
249     T* get() const noexcept { return ptr_; }
250     explicit operator bool() const { return ptr_ != nullptr; }
251 
252     // modifiers
253     T* release() {
254       T* value = ptr_;
255       ptr_ = nullptr;
256       return value;
257     }
258 
259     void reset(T* p = nullptr) {
260       T* value = ptr_;
261       ptr_ = p;
262       if (value) delete value;
263     }
264 
265     void swap(unique_ptr& u) {
266       T* temp_ptr = ptr_;
267       ptr_ = u.ptr_;
268       u.ptr_ = temp_ptr;
269     }
270 
271    private:
272     T* ptr_;
273   };
274 
275   template <class T> bool operator==(const unique_ptr<T>& x,
276                                      const unique_ptr<T>& y) {
277     return x.get() == y.get();
278   }
279 
280   template <class T, class D> bool operator==(const unique_ptr<T>& x,
281                                               const D* y) {
282     return static_cast<D*>(x.get()) == y;
283   }
284 
285   template <class T> bool operator==(const unique_ptr<T>& x, intptr_t y) {
286     return reinterpret_cast<intptr_t>(x.get()) == y;
287   }
288 
289   template <class T> bool operator!=(const unique_ptr<T>& x, decltype(nullptr)) {
290     return !!x;
291   }
292 
293   template <class T> bool operator!=(decltype(nullptr), const unique_ptr<T>& x) {
294     return !!x;
295   }
296 
297   template <class T> bool operator==(const unique_ptr<T>& x, decltype(nullptr)) {
298     return !x;
299   }
300 
301   template <class T> bool operator==(decltype(nullptr), const unique_ptr<T>& x) {
302     return !x;
303   }
304 
305 #endif  // !FLATBUFFERS_CPP98_STL
306 
307 #ifdef FLATBUFFERS_USE_STD_OPTIONAL
308 template<class T>
309 using Optional = std::optional<T>;
310 using nullopt_t = std::nullopt_t;
311 inline constexpr nullopt_t nullopt = std::nullopt;
312 
313 #else
314 // Limited implementation of Optional<T> type for a scalar T.
315 // This implementation limited by trivial types compatible with
316 // std::is_arithmetic<T> or std::is_enum<T> type traits.
317 
318 // A tag to indicate an empty flatbuffers::optional<T>.
319 struct nullopt_t {
320   explicit FLATBUFFERS_CONSTEXPR_CPP11 nullopt_t(int) {}
321 };
322 
323 #if defined(FLATBUFFERS_CONSTEXPR_DEFINED)
324   namespace internal {
325     template <class> struct nullopt_holder {
326       static constexpr nullopt_t instance_ = nullopt_t(0);
327     };
328     template<class Dummy>
329     constexpr nullopt_t nullopt_holder<Dummy>::instance_;
330   }
331   static constexpr const nullopt_t &nullopt = internal::nullopt_holder<void>::instance_;
332 
333 #else
334   namespace internal {
335     template <class> struct nullopt_holder {
336       static const nullopt_t instance_;
337     };
338     template<class Dummy>
339     const nullopt_t nullopt_holder<Dummy>::instance_  = nullopt_t(0);
340   }
341   static const nullopt_t &nullopt = internal::nullopt_holder<void>::instance_;
342 
343 #endif
344 
345 template<class T>
346 class Optional FLATBUFFERS_FINAL_CLASS {
347   // Non-scalar 'T' would extremely complicated Optional<T>.
348   // Use is_scalar<T> checking because flatbuffers flatbuffers::is_arithmetic<T>
349   // isn't implemented.
350   static_assert(flatbuffers::is_scalar<T>::value, "unexpected type T");
351 
352  public:
353   ~Optional() {}
354 
355   FLATBUFFERS_CONSTEXPR_CPP11 Optional() FLATBUFFERS_NOEXCEPT
356     : value_(), has_value_(false) {}
357 
358   FLATBUFFERS_CONSTEXPR_CPP11 Optional(nullopt_t) FLATBUFFERS_NOEXCEPT
359     : value_(), has_value_(false) {}
360 
361   FLATBUFFERS_CONSTEXPR_CPP11 Optional(T val) FLATBUFFERS_NOEXCEPT
362     : value_(val), has_value_(true) {}
363 
364   FLATBUFFERS_CONSTEXPR_CPP11 Optional(const Optional &other) FLATBUFFERS_NOEXCEPT
365     : value_(other.value_), has_value_(other.has_value_) {}
366 
367   FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(const Optional &other) FLATBUFFERS_NOEXCEPT {
368     value_ = other.value_;
369     has_value_ = other.has_value_;
370     return *this;
371   }
372 
373   FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(nullopt_t) FLATBUFFERS_NOEXCEPT {
374     value_ = T();
375     has_value_ = false;
376     return *this;
377   }
378 
379   FLATBUFFERS_CONSTEXPR_CPP14 Optional &operator=(T val) FLATBUFFERS_NOEXCEPT {
380     value_ = val;
381     has_value_ = true;
382     return *this;
383   }
384 
385   void reset() FLATBUFFERS_NOEXCEPT {
386     *this = nullopt;
387   }
388 
389   void swap(Optional &other) FLATBUFFERS_NOEXCEPT {
390     std::swap(value_, other.value_);
391     std::swap(has_value_, other.has_value_);
392   }
393 
394   FLATBUFFERS_CONSTEXPR_CPP11 FLATBUFFERS_EXPLICIT_CPP11 operator bool() const FLATBUFFERS_NOEXCEPT {
395     return has_value_;
396   }
397 
398   FLATBUFFERS_CONSTEXPR_CPP11 bool has_value() const FLATBUFFERS_NOEXCEPT {
399     return has_value_;
400   }
401 
402   FLATBUFFERS_CONSTEXPR_CPP11 const T& operator*() const FLATBUFFERS_NOEXCEPT {
403     return value_;
404   }
405 
406   const T& value() const {
407     FLATBUFFERS_ASSERT(has_value());
408     return value_;
409   }
410 
411   T value_or(T default_value) const FLATBUFFERS_NOEXCEPT {
412     return has_value() ? value_ : default_value;
413   }
414 
415  private:
416   T value_;
417   bool has_value_;
418 };
419 
420 template<class T>
421 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& opt, nullopt_t) FLATBUFFERS_NOEXCEPT {
422   return !opt;
423 }
424 template<class T>
425 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(nullopt_t, const Optional<T>& opt) FLATBUFFERS_NOEXCEPT {
426   return !opt;
427 }
428 
429 template<class T, class U>
430 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const U& rhs) FLATBUFFERS_NOEXCEPT {
431   return static_cast<bool>(lhs) && (*lhs == rhs);
432 }
433 
434 template<class T, class U>
435 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const T& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
436   return static_cast<bool>(rhs) && (lhs == *rhs);
437 }
438 
439 template<class T, class U>
440 FLATBUFFERS_CONSTEXPR_CPP11 bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) FLATBUFFERS_NOEXCEPT {
441   return static_cast<bool>(lhs) != static_cast<bool>(rhs)
442               ? false
443               : !static_cast<bool>(lhs) ? false : (*lhs == *rhs);
444 }
445 #endif // FLATBUFFERS_USE_STD_OPTIONAL
446 
447 }  // namespace flatbuffers
448 
449 #endif  // FLATBUFFERS_STL_EMULATION_H_
450