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