stl_emulation.h
Go to the documentation of this file.
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 // This header provides backwards compatibility for C++98 STLs like stlport.
37 namespace flatbuffers {
38 
39 // Retrieve ::back() from a string in a way that is compatible with pre C++11
40 // STLs (e.g stlport).
41 inline char& string_back(std::string &value) {
42  return value[value.length() - 1];
43 }
44 
45 inline char string_back(const std::string &value) {
46  return value[value.length() - 1];
47 }
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 
57 template <typename T> inline const T *vector_data(
58  const std::vector<T> &vector) {
59  return vector.empty() ? nullptr : &vector[0];
60 }
61 
62 template <typename T, typename V>
63 inline void vector_emplace_back(std::vector<T> *vector, V &&data) {
64  #if defined(FLATBUFFERS_CPP98_STL)
65  vector->push_back(data);
66  #else
67  vector->emplace_back(std::forward<V>(data));
68  #endif // defined(FLATBUFFERS_CPP98_STL)
69 }
70 
71 #ifndef FLATBUFFERS_CPP98_STL
72  #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
73  template <typename T>
74  using numeric_limits = std::numeric_limits<T>;
75  #else
76  template <typename T> class numeric_limits :
77  public std::numeric_limits<T> {};
78  #endif // !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
79 #else
80  template <typename T> class numeric_limits :
81  public std::numeric_limits<T> {};
82 
83  template <> class numeric_limits<unsigned long long> {
84  public:
85  static unsigned long long min() { return 0ULL; }
86  static unsigned long long max() { return ~0ULL; }
87  };
88 
89  template <> class numeric_limits<long long> {
90  public:
91  static long long min() {
92  return static_cast<long long>(1ULL << ((sizeof(long long) << 3) - 1));
93  }
94  static long long max() {
95  return static_cast<long long>(
96  (1ULL << ((sizeof(long long) << 3) - 1)) - 1);
97  }
98  };
99 #endif // FLATBUFFERS_CPP98_STL
100 
101 #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
102  #ifndef FLATBUFFERS_CPP98_STL
103  template <typename T> using is_scalar = std::is_scalar<T>;
104  template <typename T, typename U> using is_same = std::is_same<T,U>;
105  template <typename T> using is_floating_point = std::is_floating_point<T>;
106  template <typename T> using is_unsigned = std::is_unsigned<T>;
107  #else
108  // Map C++ TR1 templates defined by stlport.
109  template <typename T> using is_scalar = std::tr1::is_scalar<T>;
110  template <typename T, typename U> using is_same = std::tr1::is_same<T,U>;
111  template <typename T> using is_floating_point =
112  std::tr1::is_floating_point<T>;
113  template <typename T> using is_unsigned = std::tr1::is_unsigned<T>;
114  #endif // !FLATBUFFERS_CPP98_STL
115 #else
116  // MSVC 2010 doesn't support C++11 aliases.
117  template <typename T> struct is_scalar : public std::is_scalar<T> {};
118  template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
119  template <typename T> struct is_floating_point :
120  public std::is_floating_point<T> {};
121  template <typename T> struct is_unsigned : public std::is_unsigned<T> {};
122 #endif // !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
123 
124 #ifndef FLATBUFFERS_CPP98_STL
125  #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
126  template <class T> using unique_ptr = std::unique_ptr<T>;
127  #else
128  // MSVC 2010 doesn't support C++11 aliases.
129  // We're manually "aliasing" the class here as we want to bring unique_ptr
130  // into the flatbuffers namespace. We have unique_ptr in the flatbuffers
131  // namespace we have a completely independent implemenation (see below)
132  // for C++98 STL implementations.
133  template <class T> class unique_ptr : public std::unique_ptr<T> {
134  public:
135  unique_ptr() {}
136  explicit unique_ptr(T* p) : std::unique_ptr<T>(p) {}
137  unique_ptr(std::unique_ptr<T>&& u) { *this = std::move(u); }
138  unique_ptr(unique_ptr&& u) { *this = std::move(u); }
139  unique_ptr& operator=(std::unique_ptr<T>&& u) {
140  std::unique_ptr<T>::reset(u.release());
141  return *this;
142  }
143  unique_ptr& operator=(unique_ptr&& u) {
144  std::unique_ptr<T>::reset(u.release());
145  return *this;
146  }
147  unique_ptr& operator=(T* p) {
148  return std::unique_ptr<T>::operator=(p);
149  }
150  };
151  #endif // !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
152 #else
153  // Very limited implementation of unique_ptr.
154  // This is provided simply to allow the C++ code generated from the default
155  // settings to function in C++98 environments with no modifications.
156  template <class T> class unique_ptr {
157  public:
158  typedef T element_type;
159 
160  unique_ptr() : ptr_(nullptr) {}
161  explicit unique_ptr(T* p) : ptr_(p) {}
162  unique_ptr(unique_ptr&& u) : ptr_(nullptr) { reset(u.release()); }
163  unique_ptr(const unique_ptr& u) : ptr_(nullptr) {
164  reset(const_cast<unique_ptr*>(&u)->release());
165  }
166  ~unique_ptr() { reset(); }
167 
168  unique_ptr& operator=(const unique_ptr& u) {
169  reset(const_cast<unique_ptr*>(&u)->release());
170  return *this;
171  }
172 
173  unique_ptr& operator=(unique_ptr&& u) {
174  reset(u.release());
175  return *this;
176  }
177 
178  unique_ptr& operator=(T* p) {
179  reset(p);
180  return *this;
181  }
182 
183  const T& operator*() const { return *ptr_; }
184  T* operator->() const { return ptr_; }
185  T* get() const noexcept { return ptr_; }
186  explicit operator bool() const { return ptr_ != nullptr; }
187 
188  // modifiers
189  T* release() {
190  T* value = ptr_;
191  ptr_ = nullptr;
192  return value;
193  }
194 
195  void reset(T* p = nullptr) {
196  T* value = ptr_;
197  ptr_ = p;
198  if (value) delete value;
199  }
200 
201  void swap(unique_ptr& u) {
202  T* temp_ptr = ptr_;
203  ptr_ = u.ptr_;
204  u.ptr_ = temp_ptr;
205  }
206 
207  private:
208  T* ptr_;
209  };
210 
211  template <class T> bool operator==(const unique_ptr<T>& x,
212  const unique_ptr<T>& y) {
213  return x.get() == y.get();
214  }
215 
216  template <class T, class D> bool operator==(const unique_ptr<T>& x,
217  const D* y) {
218  return static_cast<D*>(x.get()) == y;
219  }
220 
221  template <class T> bool operator==(const unique_ptr<T>& x, intptr_t y) {
222  return reinterpret_cast<intptr_t>(x.get()) == y;
223  }
224 #endif // !FLATBUFFERS_CPP98_STL
225 
226 } // namespace flatbuffers
227 
228 #endif // FLATBUFFERS_STL_EMULATION_H_
const T * data(const std::vector< T, Alloc > &v)
Definition: flatbuffers.h:709
std::is_unsigned< T > is_unsigned
void swap(linb::any &lhs, linb::any &rhs) noexcept
Definition: any.hpp:457
T * vector_data(std::vector< T > &vector)
Definition: stl_emulation.h:51
std::unique_ptr< T > unique_ptr
optional_constexpr bool operator==(optional< T > const &x, optional< U > const &y)
Definition: optional.hpp:954
std::numeric_limits< T > numeric_limits
Definition: stl_emulation.h:74
std::is_floating_point< T > is_floating_point
std::is_scalar< T > is_scalar
std::is_same< T, U > is_same
void vector_emplace_back(std::vector< T > *vector, V &&data)
Definition: stl_emulation.h:63
char & string_back(std::string &value)
Definition: stl_emulation.h:41


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53