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