ManaPlus
c++11.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 
4 /*
5  * Copyright (C) 2013-2017 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
37 #ifndef NVWA_CXX11_H
38 #define NVWA_CXX11_H
39 
40 // Only Clang provides these macros; they need to be defined as follows
41 // to get a valid expression in preprocessing by other compilers.
42 #ifndef __has_extension
43 #define __has_extension(x) 0
44 #endif
45 #ifndef __has_feature
46 #define __has_feature(x) 0
47 #endif
48 #ifndef __has_include
49 #define __has_include(x) 0
50 #endif
51 
52 // Detect whether C++11 mode is on (for GCC and Clang). MSVC does not
53 // have a special C++11 mode, so it is always on for Visual C++ 2010 and
54 // later.
55 #if __cplusplus >= 201103L || \
56  defined(__GXX_EXPERIMENTAL_CXX0X__) || \
57  (defined(_MSC_VER) && _MSC_VER >= 1600)
58 #define NVWA_CXX11_MODE 1
59 #else
60 #define NVWA_CXX11_MODE 0
61 #endif
62 
63 
64 /* Feature checks */
65 
66 #if !defined(HAVE_CXX11_ATOMIC)
67 #if NVWA_CXX11_MODE && \
68  ((__has_include(<atomic>) && !defined(__MINGW32__)) || \
69  (defined(_MSC_VER) && _MSC_VER >= 1700) || \
70  (((defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 405) || \
71  defined(__clang__)) && \
72  (!defined(__MINGW32__) || defined(_POSIX_THREADS))))
73 // Note: MinGW GCC, unless built with POSIX threads (as in
74 // MinGW-builds), does not support atomics as of 4.8.
75 #define HAVE_CXX11_ATOMIC 1
76 #else
77 #define HAVE_CXX11_ATOMIC 0
78 #endif
79 #endif
80 
81 #if !defined(HAVE_CXX11_AUTO_TYPE)
82 #if NVWA_CXX11_MODE && \
83  (__has_feature(cxx_auto_type) || \
84  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
85  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 404))
86 #define HAVE_CXX11_AUTO_TYPE 1
87 #else
88 #define HAVE_CXX11_AUTO_TYPE 0
89 #endif
90 #endif
91 
92 #if !defined(HAVE_CXX11_DELETED_FUNCTION)
93 #if NVWA_CXX11_MODE && \
94  (__has_feature(cxx_deleted_functions) || \
95  (defined(_MSC_VER) && _MSC_VER >= 1800) || \
96  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 404))
97 #define HAVE_CXX11_DELETED_FUNCTION 1
98 #else
99 #define HAVE_CXX11_DELETED_FUNCTION 0
100 #endif
101 #endif
102 
103 #if !defined(HAVE_CXX11_EXPLICIT_CONVERSION)
104 #if NVWA_CXX11_MODE && \
105  (__has_feature(cxx_explicit_conversions) || \
106  (defined(_MSC_VER) && _MSC_VER >= 1900) || \
107  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 405))
108 #define HAVE_CXX11_EXPLICIT_CONVERSION 1
109 #else
110 #define HAVE_CXX11_EXPLICIT_CONVERSION 0
111 #endif
112 #endif
113 
114 #if !defined(HAVE_CXX11_FINAL)
115 #if NVWA_CXX11_MODE && \
116  (__has_feature(cxx_override_control) || \
117  (defined(_MSC_VER) && _MSC_VER >= 1700) || \
118  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 407))
119 #define HAVE_CXX11_FINAL 1
120 #else
121 #define HAVE_CXX11_FINAL 0
122 #endif
123 #endif
124 
125 #if !defined(HAVE_CXX11_FUTURE)
126 #if NVWA_CXX11_MODE && \
127  ((__has_include(<future>) && !defined(__MINGW32__)) || \
128  (defined(_MSC_VER) && _MSC_VER >= 1700) || \
129  (((defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 405) || \
130  defined(__clang__)) && \
131  (!defined(__MINGW32__) || defined(_POSIX_THREADS))))
132 // Note: MinGW GCC, unless built with POSIX threads (as in
133 // MinGW-builds), does not support futures as of 4.8.
134 #define HAVE_CXX11_FUTURE 1
135 #else
136 #define HAVE_CXX11_FUTURE 0
137 #endif
138 #endif
139 
140 #if !defined(HAVE_CXX11_GENERALIZED_INITIALIZER)
141 #if NVWA_CXX11_MODE && \
142  (__has_feature(cxx_generalized_initializers) || \
143  (defined(_MSC_VER) && _MSC_VER >= 1800) || \
144  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 404))
145 #define HAVE_CXX11_GENERALIZED_INITIALIZER 1
146 #else
147 #define HAVE_CXX11_GENERALIZED_INITIALIZER 0
148 #endif
149 #endif
150 
151 #if !defined(HAVE_CXX11_LAMBDA)
152 #if NVWA_CXX11_MODE && \
153  (__has_feature(cxx_lambdas) || \
154  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
155  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 405))
156 #define HAVE_CXX11_LAMBDA 1
157 #else
158 #define HAVE_CXX11_LAMBDA 0
159 #endif
160 #endif
161 
162 #if !defined(HAVE_CXX11_MUTEX)
163 #if NVWA_CXX11_MODE && \
164  ((__has_include(<mutex>) && !defined(__MINGW32__)) || \
165  (defined(_MSC_VER) && _MSC_VER >= 1700) || \
166  (((defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 403) || \
167  defined(__clang__)) && \
168  (!defined(__MINGW32__) || defined(_POSIX_THREADS))))
169 // Note: MinGW GCC, unless built with POSIX threads (as in
170 // MinGW-builds), does not support std::mutex as of 4.8.
171 #define HAVE_CXX11_MUTEX 1
172 #else
173 #define HAVE_CXX11_MUTEX 0
174 #endif
175 #endif
176 
177 #if !defined(HAVE_CXX11_NOEXCEPT)
178 #if NVWA_CXX11_MODE && \
179  (__has_feature(cxx_noexcept) || \
180  (defined(_MSC_VER) && _MSC_VER >= 1900) || \
181  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 406))
182 #define HAVE_CXX11_NOEXCEPT 1
183 #else
184 #define HAVE_CXX11_NOEXCEPT 0
185 #endif
186 #endif
187 
188 #if !defined(HAVE_CXX11_NULLPTR)
189 #if NVWA_CXX11_MODE && \
190  (__has_feature(cxx_nullptr) || \
191  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
192  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 406))
193 #define HAVE_CXX11_NULLPTR 1
194 #else
195 #define HAVE_CXX11_NULLPTR 0
196 #endif
197 #endif
198 
199 #if !defined(HAVE_CXX11_OVERRIDE)
200 #if NVWA_CXX11_MODE && \
201  (__has_feature(cxx_override_control) || \
202  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
203  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 407))
204 #define HAVE_CXX11_OVERRIDE 1
205 #else
206 #define HAVE_CXX11_OVERRIDE 0
207 #endif
208 #endif
209 
210 #if !defined(HAVE_CXX11_RANGE_FOR)
211 #if NVWA_CXX11_MODE && \
212  (__has_feature(cxx_range_for) || \
213  (defined(_MSC_VER) && _MSC_VER >= 1700) || \
214  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 406))
215 #define HAVE_CXX11_RANGE_FOR 1
216 #else
217 #define HAVE_CXX11_RANGE_FOR 0
218 #endif
219 #endif
220 
221 #if !defined(HAVE_CXX11_RVALUE_REFERENCE)
222 #if NVWA_CXX11_MODE && \
223  (__has_feature(cxx_rvalue_references) || \
224  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
225  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 405))
226 #define HAVE_CXX11_RVALUE_REFERENCE 1
227 #else
228 #define HAVE_CXX11_RVALUE_REFERENCE 0
229 #endif
230 #endif
231 
232 #if !defined(HAVE_CXX11_STATIC_ASSERT)
233 #if NVWA_CXX11_MODE && \
234  (__has_feature(cxx_static_assert) || \
235  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
236  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 403))
237 #define HAVE_CXX11_STATIC_ASSERT 1
238 #else
239 #define HAVE_CXX11_STATIC_ASSERT 0
240 #endif
241 #endif
242 
243 #if !defined(HAVE_CXX11_THREAD)
244 #if NVWA_CXX11_MODE && \
245  ((__has_include(<thread>) && !defined(__MINGW32__)) || \
246  (defined(_MSC_VER) && _MSC_VER >= 1700) || \
247  (((defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 404) || \
248  defined(__clang__)) && \
249  (!defined(__MINGW32__) || defined(_POSIX_THREADS))))
250 // Note: MinGW GCC, unless built with POSIX threads (as in
251 // MinGW-builds), does not support std::thread as of 4.8.
252 #define HAVE_CXX11_THREAD 1
253 #else
254 #define HAVE_CXX11_THREAD 0
255 #endif
256 #endif
257 
258 #if !defined(HAVE_CXX11_THREAD_LOCAL)
259 #if NVWA_CXX11_MODE && \
260  (__has_feature(cxx_thread_local) || \
261  (defined(_MSC_VER) && _MSC_VER >= 1900) || \
262  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 408))
263 #define HAVE_CXX11_THREAD_LOCAL 1
264 #else
265 #define HAVE_CXX11_THREAD_LOCAL 0
266 #endif
267 #endif
268 
269 #if !defined(HAVE_CXX11_TYPE_TRAITS)
270 #if NVWA_CXX11_MODE && \
271  (__has_include(<type_traits>) || \
272  (defined(_MSC_VER) && _MSC_VER >= 1600) || \
273  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 403))
274 #define HAVE_CXX11_TYPE_TRAITS 1
275 #else
276 #define HAVE_CXX11_TYPE_TRAITS 0
277 #endif
278 #endif
279 
280 #if !defined(HAVE_CXX11_UNICODE_LITERAL)
281 #if NVWA_CXX11_MODE && \
282  (__has_feature(cxx_unicode_literals) || \
283  (defined(_MSC_VER) && _MSC_VER >= 1900) || \
284  (defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 405))
285 #define HAVE_CXX11_UNICODE_LITERAL 1
286 #else
287 #define HAVE_CXX11_UNICODE_LITERAL 0
288 #endif
289 #endif
290 
291 
292 /* Workarounds */
293 
294 #if HAVE_CXX11_DELETED_FUNCTION
295 #define _DELETED = delete
296 #else
297 #define _DELETED
298 #endif
299 
300 #if HAVE_CXX11_FINAL
301 #define _FINAL final
302 #else
303 #define _FINAL
304 #endif
305 
306 #if HAVE_CXX11_OVERRIDE
307 #define _OVERRIDE override
308 #else
309 #define _OVERRIDE
310 #endif
311 
312 #if HAVE_CXX11_NOEXCEPT
313 #define _NOEXCEPT noexcept
314 #define _NOEXCEPT_(x) noexcept(x)
315 #else
316 #ifdef _MSC_VER
317 #define _NOEXCEPT throw ()
318 #else
319 #define _NOEXCEPT throw()
320 #endif
321 #define _NOEXCEPT_(x)
322 #endif
323 
324 #if HAVE_CXX11_NULLPTR
325 #define _NULLPTR nullptr
326 #else
327 #define _NULLPTR NULL
328 #endif
329 
330 #if HAVE_CXX11_THREAD_LOCAL
331 #define _THREAD_LOCAL thread_local
332 #else
333 #ifdef _MSC_VER
334 #define _THREAD_LOCAL __declspec(thread)
335 #else
336 #define _THREAD_LOCAL __thread
337 #endif
338 #endif
339 
340 #endif // NVWA_CXX11_H