Photon microGUI widgets library 0.6.0
core.h
1#ifdef __cplusplus
2
3#ifndef _STDEX_CORE_H
4#define _STDEX_CORE_H
5
6#if _MSC_VER > 1000
7#pragma once
8#endif // _MSC_VER > 1000
9
10#include <cerrno>
11
12
13#ifndef __has_feature // Optional of course.
14 #define _STDEX_HAS_FEATURE_BUILTIN(xxx) 0 // Compatibility with non-clang compilers.
15#else
16 #define _STDEX_HAS_FEATURE_BUILTIN(xxx) __has_feature(xxx)
17#endif
18#ifndef __has_extension
19 #define _STDEX_HAS_EXTENSION_BUILTIN(xxx) _STDEX_HAS_FEATURE_BUILTIN(xxx) // Compatibility with pre-3.0 compilers.
20#else
21 #define _STDEX_HAS_EXTENSION_BUILTIN(xxx) __has_extension(xxx)
22#endif
23
24//#define STDEX_FORCE_CPP11_TYPES_SUPPORT //uncomment to force support of char16_t and char32_t in C++03
25
26// Any compiler claiming C++11 supports, Visual C++ 2015 and Clang version supporting constexpr
27#if ((__cplusplus >= 201103L) || (_MSC_VER >= 1900) || (_STDEX_HAS_FEATURE_BUILTIN(cxx_constexpr) || (_STDEX_HAS_EXTENSION_BUILTIN(cxx_constexpr)))) // C++ 11 implementation
28
29 #define _STDEX_NATIVE_CPP11_SUPPORT
30 #define _STDEX_NATIVE_CPP11_TYPES_SUPPORT
31 #define _STDEX_NATIVE_CPP_98_SUPPORT
32
33#endif
34
35// LWG 310 C++98 it is unspecified whether errno is a macro or an identifier with external linkage errno must be macro
36#ifdef errno
37 #define _STDEX_NATIVE_CPP_98_SUPPORT
38#endif
39
40#if !defined(_STDEX_NATIVE_CPP11_TYPES_SUPPORT)
41
42 #if ((__cplusplus > 199711L) || defined(__CODEGEARC__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__cpp_unicode_characters) || _STDEX_HAS_FEATURE_BUILTIN(cxx_unicode_literals))
43 #define _STDEX_NATIVE_CPP11_TYPES_SUPPORT
44 #endif
45
46 #if (defined(__apple_build_version__) && (__clang_major__ >= 3))
47 #ifndef _STDEX_NATIVE_CPP11_TYPES_SUPPORT
48 #define _STDEX_NATIVE_CPP11_TYPES_SUPPORT
49 #endif
50 #endif
51
52#endif
53
54#if (_STDEX_HAS_FEATURE_BUILTIN(cxx_nullptr) || _STDEX_HAS_EXTENSION_BUILTIN(cxx_nullptr))
55 #define _STDEX_NATIVE_NULLPTR_SUPPORT
56#endif
57
58#if ((!defined(_MSC_VER) || _MSC_VER < 1600) && !defined(_STDEX_NATIVE_CPP11_SUPPORT))
59
60
61 #if !defined(_STDEX_NATIVE_NULLPTR_SUPPORT)
62 #if !defined(nullptr)
63 #define _STDEX_IMPLEMENTS_NULLPTR_SUPPORT
64 #else
65 #define STRINGIZE_HELPER(xxx) #xxx
66 #define STRINGIZE(xxx) STRINGIZE_HELPER(xxx)
67 #define WARNING(desc) message(__FILE__ "(" STRINGIZE(__LINE__) ") : warning: " desc)
68
69 #pragma WARNING("stdex library - macro 'nullptr' was previously defined by user; ignoring stdex macro definition")
70
71 #undef STRINGIZE_HELPER
72 #undef STRINGIZE
73 #undef WARNING
74 #endif
75 #endif
76
77 #if (_STDEX_HAS_FEATURE_BUILTIN(cxx_static_assert) || _STDEX_HAS_EXTENSION_BUILTIN(cxx_static_assert))
78 #define _STDEX_NATIVE_STATIC_ASSERT_SUPPORT
79 #else
80 #if !defined(static_assert)
81 #define _STDEX_IMPLEMENTS_STATIC_ASSERT_SUPPORT
82 #else
83 #define STRINGIZE_HELPER(x) #x
84 #define STRINGIZE(x) STRINGIZE_HELPER(x)
85 #define WARNING(desc) message(__FILE__ "(" STRINGIZE(__LINE__) ") : warning: " desc)
86
87 #pragma WARNING("stdex library - macro 'static_assert' was previously defined by user; ignoring stdex macro definition")
88
89 #undef STRINGIZE_HELPER
90 #undef STRINGIZE
91 #undef WARNING
92 #endif
93 #endif
94
95#else
96
97#if !defined(_STDEX_NATIVE_NULLPTR_SUPPORT)
98 #define _STDEX_NATIVE_NULLPTR_SUPPORT
99#endif
100 #define _STDEX_NATIVE_STATIC_ASSERT_SUPPORT
101
102#endif
103
104#if (_MSC_VER >= 1600)
105
106 #ifndef _STDEX_NATIVE_CPP11_TYPES_SUPPORT
107 #define _STDEX_NATIVE_CPP11_TYPES_SUPPORT
108 #endif
109
110#endif
111
112
113
114#if _MSC_VER // Visual C++ fallback
115
116 #define _STDEX_NATIVE_MICROSOFT_COMPILER_EXTENSIONS_SUPPORT
117 #define _STDEX_CDECL __cdecl
118 #define _STDEX_STDCALL __stdcall
119 #define _STDEX_FASTCALL __fastcall
120
121 #if (__cplusplus >= 199711L)
122 #define _STDEX_NATIVE_CPP_98_SUPPORT
123 #endif
124
125#endif
126
127// C++ 98 trick:
128#if (__cplusplus >= 199711L)
129 #if (defined(__INTEL_COMPILER) || defined(__clang__))
130 #ifndef _STDEX_NATIVE_CPP_98_SUPPORT
131 #define _STDEX_NATIVE_CPP_98_SUPPORT
132 #endif
133 #endif
134#endif
135
136#if defined(__GNUC__)
137 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
138 #ifndef _STDEX_NATIVE_CPP_98_SUPPORT
139 #define _STDEX_NATIVE_CPP_98_SUPPORT
140 #endif
141 #endif
142 #if !__x86_64__ && !__ppc64__
143 #define _STDEX_CDECL __attribute__((cdecl))
144 #define _STDEX_STDCALL __attribute__((stdcall))
145 #define _STDEX_FASTCALL __attribute__((fastcall))
146 #endif
147#endif
148
149#if !defined(forever)
150 #define forever for(;;)
151#else
152 #define STRINGIZE_HELPER(xxx) #xxx
153 #define STRINGIZE(xxx) STRINGIZE_HELPER(xxx)
154 #define WARNING(desc) message(__FILE__ "(" STRINGIZE(__LINE__) ") : warning: " desc)
155
156 #pragma WARNING("stdex library - macro 'forever' was previously defined by user; ignoring stdex macro definition")
157
158 #undef STRINGIZE_HELPER
159 #undef STRINGIZE
160 #undef WARNING
161#endif
162
163#undef stdex_assert
164
165#if defined(assert)
166#ifndef NDEBUG
167 #include <iostream>
168 #define stdex_assert(condition, message) \
169 do { \
170 if (! (condition)) { \
171 std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
172 << " line " << __LINE__ << ": " << message << std::endl; \
173 std::terminate(); \
174 } \
175 } while (false)
176#else
177 #define stdex_assert(condition, message) ((void)0)
178#endif
179#endif
180
181#ifdef _STDEX_NATIVE_CPP11_SUPPORT
182
183 #include <cstddef>
184 namespace stdex
185 {
186 namespace detail
187 {
188 template <class _Tp, std::size_t _Count>
189 constexpr std::size_t _my_countof(_Tp const (&)[_Count]) noexcept
190 {
191 return _Count;
192 }
193 } // namespace detail
194 }
195 #define countof(arr) stdex::detail::_my_countof(arr)
196 #define STATIC_ASSERT(expression, message) static_assert((expression), #message)
197
198 #ifdef _STDEX_NATIVE_NULLPTR_SUPPORT
199 namespace stdex
200 {
201 typedef std::nullptr_t nullptr_t;
202 }
203 #else
204 #include "nullptr.h"
205 #endif
206
207#else //no C++11 support
208
209 // nullptr and nullptr_t implementation
210 #if defined(_STDEX_IMPLEMENTS_NULLPTR_SUPPORT) && !defined(_STDEX_NATIVE_NULLPTR_SUPPORT)
211 #include "nullptr.h"
212 #else
213 namespace stdex
214 {
215 typedef std::nullptr_t nullptr_t;
216 }
217 #endif // _STDEX_IMPLEMENTS_NULLPTR_SUPPORT && !_STDEX_NATIVE_NULLPTR_SUPPORT
218
219 namespace stdex
220 {
221 namespace detail {
222
223 template <bool>
224 struct StaticAssertion;
225
226 template <>
227 struct StaticAssertion<true>
228 {
229 }; // StaticAssertion<true>
230
231 template<int>
232 struct StaticAssertionTest
233 {
234 }; // StaticAssertionTest<int>
235 }
236 }
237
238 #define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
239 #define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
240 #define CONCATENATE2(arg1, arg2) arg1##arg2
241
242 #define STATIC_ASSERT(expression, message)\
243 struct CONCATENATE(__static_assertion_at_line_, __LINE__)\
244 {\
245 stdex::detail::StaticAssertion<bool((expression))> CONCATENATE(CONCATENATE(CONCATENATE(STATIC_ASSERTION_FAILED_AT_LINE_, __LINE__), _WITH__), message);\
246 };\
247 typedef stdex::detail::StaticAssertionTest<sizeof(CONCATENATE(__static_assertion_at_line_, __LINE__))> CONCATENATE(__static_assertion_test_at_line_, __LINE__)
248
249#ifdef _STDEX_IMPLEMENTS_STATIC_ASSERT_SUPPORT
250 #ifdef static_assert
251 #undef static_assert
252 #endif
253 #define static_assert(expression, message) STATIC_ASSERT(expression, ERROR_MESSAGE_STRING)
254#endif
255
256
257 #ifdef _STDEX_NATIVE_MICROSOFT_COMPILER_EXTENSIONS_SUPPORT // Visual C++ fallback
258 #include <stdlib.h>
259 #define countof(arr) _countof(arr)
260
261 #elif defined(_STDEX_NATIVE_CPP_98_SUPPORT)// C++ 98 trick
262 #include <cstddef>
263 template <class _Tp, std::size_t _Count>
264 char(&COUNTOF_REQUIRES_ARRAY_ARGUMENT(_Tp(&)[_Count]))[_Count];
265
266 #define countof(xxx) sizeof(COUNTOF_REQUIRES_ARRAY_ARGUMENT(xxx))
267 #else
268 #define countof(arr) sizeof(arr) / sizeof(arr[0])
269 #endif
270#endif
271
272#ifdef _STDEX_NATIVE_CPP11_SUPPORT
273
274#define stdex_noexcept noexcept
275
276#else
277
278#define stdex_noexcept throw()
279
280#endif
281
282//#ifndef _STDEX_NATIVE_CPP11_TYPES_SUPPORT
283 //#ifndef __CHAR16_TYPE__
284 #define stdex_char16_t stdex::uint_least16_t
285 //#endif
286 //#ifndef __CHAR32_TYPE__
287 #define stdex_char32_t stdex::uint_least32_t
288 //#endif
289//#endif
290
291#undef _STDEX_HAS_FEATURE_BUILTIN
292#undef _STDEX_HAS_EXTENSION_BUILTIN
293
294#endif // _STDEX_CORE_H
295
296#endif // __cplusplus