Sane C++ Libraries
C++ Platform Abstraction Libraries
Compiler.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#if defined(SC_COMPILER_ENABLE_CONFIG)
5#include "SCConfig.h"
6#endif
11
14
15#if __clang__
16#define SC_COMPILER_CLANG 1
17#define SC_COMPILER_GCC 0
18#define SC_COMPILER_MSVC 0
19
20#if _MSC_VER
21#define SC_COMPILER_CLANG_CL 1
22#else
23#define SC_COMPILER_CLANG_CL 0
24#endif
25
26#elif _MSC_VER
27#define SC_COMPILER_CLANG 0
28#define SC_COMPILER_GCC 0
29#define SC_COMPILER_MSVC 1
30#define SC_COMPILER_CLANG_CL 0
31
32#else
33#define SC_COMPILER_CLANG 0
34#define SC_COMPILER_GCC 1
35#define SC_COMPILER_MSVC 0
36#define SC_COMPILER_CLANG_CL 0
37#endif
38
39#if SC_COMPILER_MSVC
40
41#define SC_COMPILER_FORCE_INLINE __forceinline
42#define SC_COMPILER_DEBUG_BREAK __debugbreak()
43
44#else
45
46#define SC_COMPILER_FORCE_INLINE __attribute__((always_inline)) inline
47#if defined(__has_builtin)
48#if __has_builtin(__builtin_debugtrap)
49#define SC_COMPILER_DEBUG_BREAK __builtin_debugtrap()
50#elif __has_builtin(__builtin_trap)
51#define SC_COMPILER_DEBUG_BREAK __builtin_trap()
52#else
53#error "No __builtin_trap or __builtin_debugtrap"
54#endif
55#else
56#error "No __has_builtin"
57#endif
58
59#endif
60
62#if SC_COMPILER_MSVC
63
64#if defined(SC_PLUGIN_LIBRARY)
65#define SC_COMPILER_EXPORT __declspec(dllimport)
66#define SC_COMPILER_EXTERN extern
67#else
68#define SC_COMPILER_EXPORT __declspec(dllexport)
69#define SC_COMPILER_EXTERN
70#endif
71
72#else
73
74#define SC_COMPILER_EXTERN
75#if defined(SC_PLUGIN_LIBRARY)
76#define SC_COMPILER_EXPORT
77#else
78#define SC_COMPILER_EXPORT \
79 __attribute__((visibility("default")))
80#endif
81
82#endif
83
84#if !DOXYGEN && defined(SC_LIBRARY_PATH)
85#define SC_COMPILER_MACRO_ESCAPE(input) __SC_COMPILER_MACRO_ESCAPE_HELPER(input)
86#define __SC_COMPILER_MACRO_ESCAPE_HELPER(input) #input
87#define __SC_COMPILER_MACRO_TO_LITERAL(string) #string
88#define SC_COMPILER_MACRO_TO_LITERAL(string) \
89 __SC_COMPILER_MACRO_TO_LITERAL(string)
90#define SC_COMPILER_LIBRARY_PATH \
91 SC_COMPILER_MACRO_TO_LITERAL(SC_COMPILER_MACRO_ESCAPE(SC_LIBRARY_PATH))
92#endif
93
95#if defined(__SANITIZE_ADDRESS__)
96#define SC_COMPILER_ASAN 1
97#else
98#define SC_COMPILER_ASAN 0
99#endif
100
102#if defined(__clang__)
103#define SC_COMPILER_WARNING_POP _Pragma("clang diagnostic pop")
104#elif defined(__GNUC__)
105#define SC_COMPILER_WARNING_POP _Pragma("GCC diagnostic pop")
106#else
107#define SC_COMPILER_WARNING_POP _Pragma("warning(pop)")
108#endif
109
111#define SC_COMPILER_OFFSETOF(Class, Field) __builtin_offsetof(Class, Field)
112
113namespace SC
114{
115template <int offset, typename T, typename R>
116T& fieldOffset(R& object)
117{
118 return *reinterpret_cast<T*>(reinterpret_cast<char*>(&object) - offset);
119}
120} // namespace SC
121
122#define SC_COMPILER_FIELD_OFFSET(Class, Field, Value) \
123 SC::fieldOffset<SC_COMPILER_OFFSETOF(Class, Field), Class, decltype(Class::Field)>(Value);
124
126#if SC_COMPILER_CLANG
127#define SC_COMPILER_WARNING_PUSH_OFFSETOF \
128 _Pragma("clang diagnostic push"); \
129 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"");
130#elif SC_COMPILER_GCC
131#define SC_COMPILER_WARNING_PUSH_OFFSETOF \
132 _Pragma("GCC diagnostic push"); \
133 _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"");
134#else
135#define SC_COMPILER_WARNING_PUSH_OFFSETOF _Pragma("warning(push)")
136#endif
137
139#define SC_COMPILER_UNUSED(param) ((void)param)
140
142#if SC_COMPILER_CLANG
143#define SC_COMPILER_WARNING_PUSH_UNUSED_RESULT \
144 _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wunused-result\"")
145#elif SC_COMPILER_GCC
146#define SC_COMPILER_WARNING_PUSH_UNUSED_RESULT \
147 _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wunused-result\"")
148#else
149#define SC_COMPILER_WARNING_PUSH_UNUSED_RESULT _Pragma("warning(push)") _Pragma("warning(disable : 4834 6031)")
150#endif
151
154#if defined(SC_LANGUAGE_FORCE_STANDARD_CPP)
155#if SC_LANGUAGE_FORCE_STANDARD_CPP == 14
156#define SC_LANGUAGE_CPP_VERSION 201402L
157#elif SC_LANGUAGE_FORCE_STANDARD_CPP == 17
158#define SC_LANGUAGE_CPP_VERSION 201703L
159#elif SC_LANGUAGE_FORCE_STANDARD_CPP == 20
160#define SC_LANGUAGE_CPP_VERSION 202002L
161#else
162#error "SC_LANGUAGE_FORCE_STANDARD_CPP has invalid value"
163#endif
164#else
165
167#if SC_COMPILER_MSVC
168#define SC_LANGUAGE_CPP_VERSION _MSVC_LANG
169#else
170#define SC_LANGUAGE_CPP_VERSION __cplusplus
171#endif
172
173#endif
174
175#if SC_LANGUAGE_CPP_VERSION >= 202002L
176#define SC_LANGUAGE_CPP_LESS_THAN_20 0
177#define SC_LANGUAGE_CPP_AT_LEAST_20 1
178#define SC_LANGUAGE_CPP_AT_LEAST_17 1
179#define SC_LANGUAGE_CPP_AT_LEAST_14 1
180
181#elif SC_LANGUAGE_CPP_VERSION >= 201703L
182
183#define SC_LANGUAGE_CPP_LESS_THAN_20 1
184#define SC_LANGUAGE_CPP_AT_LEAST_20 0
185#define SC_LANGUAGE_CPP_AT_LEAST_17 1
186#define SC_LANGUAGE_CPP_AT_LEAST_14 1
187
188#elif SC_LANGUAGE_CPP_VERSION >= 201402L
189
190#define SC_LANGUAGE_CPP_LESS_THAN_20 1
191#define SC_LANGUAGE_CPP_AT_LEAST_20 0
192#define SC_LANGUAGE_CPP_AT_LEAST_17 0
193#define SC_LANGUAGE_CPP_AT_LEAST_14 1
194
195#else
196
197#define SC_LANGUAGE_CPP_LESS_THAN_20 1
198#define SC_LANGUAGE_CPP_AT_LEAST_20 0
199#define SC_LANGUAGE_CPP_AT_LEAST_17 0
200#define SC_LANGUAGE_CPP_AT_LEAST_14 0
201
202#endif
203
204#undef SC_LANGUAGE_CPP_VERSION
205
206#if SC_LANGUAGE_CPP_AT_LEAST_20
207#define SC_LANGUAGE_LIKELY [[likely]]
208#define SC_LANGUAGE_UNLIKELY [[unlikely]]
209#else
210#define SC_LANGUAGE_LIKELY
211#define SC_LANGUAGE_UNLIKELY
212#endif
213
214#if SC_LANGUAGE_CPP_AT_LEAST_17
215#define SC_LANGUAGE_IF_CONSTEXPR constexpr
216#else
217#define SC_LANGUAGE_IF_CONSTEXPR
218#endif
219
220#if __cpp_exceptions == 199711 || _EXCEPTIONS
221#define SC_LANGUAGE_EXCEPTIONS 1
222#else
223#define SC_LANGUAGE_EXCEPTIONS 0
224#endif
225
226#ifndef __has_cpp_attribute
227#define SC_LANGUAGE_LIFETIME_BOUND
228#elif __has_cpp_attribute(msvc::lifetimebound)
229#define SC_LANGUAGE_LIFETIME_BOUND [[msvc::lifetimebound]]
230#elif __has_cpp_attribute(clang::lifetimebound)
231#define SC_LANGUAGE_LIFETIME_BOUND [[clang::lifetimebound]]
232#elif __has_cpp_attribute(lifetimebound)
233#define SC_LANGUAGE_LIFETIME_BOUND [[lifetimebound]]
234#else
235#define SC_LANGUAGE_LIFETIME_BOUND
236#endif
237
239// clang-format off
240namespace SC
241{
242namespace TypeTraits
243{
246
248template <class T> struct RemoveReference { using type = T; };
249template <class T> struct RemoveReference<T&> { using type = T; };
250template <class T> struct RemoveReference<T&&> { using type = T; };
252template <class T> struct IsLValueReference { static constexpr bool value = false; };
253template <class T> struct IsLValueReference<T&> { static constexpr bool value = true; };
255template <class T> struct IsRValueReference { static constexpr bool value = false; };
256template <class T> struct IsRValueReference<T&&>{ static constexpr bool value = true; };
258
259}
264
267
269template <typename T> constexpr T&& move(T& value) { return static_cast<T&&>(value); }
270
272template <typename T> constexpr T&& forward(typename TypeTraits::RemoveReference<T>::type& value) { return static_cast<T&&>(value); }
273
275template <typename T> constexpr T&& forward(typename TypeTraits::RemoveReference<T>::type&& value)
276{
277 static_assert(!TypeTraits::IsLValueReference<T>::value, "Forward an rvalue as an lvalue is not allowed");
278 return static_cast<T&&>(value);
279}
280
282template <typename T> constexpr inline void swap(T& t1, T& t2)
283{
284 T temp = move(t1);
285 t1 = move(t2);
286 t2 = move(temp);
287}
289
290#ifdef max
291#undef max
292#endif
293#ifdef min
294#undef min
295#endif
298
300template <typename T> constexpr const T& min(const T& t1, const T& t2) { return t1 < t2 ? t1 : t2; }
302template <typename T> constexpr const T& max(const T& t1, const T& t2) { return t1 > t2 ? t1 : t2; }
303
305
306}
307// clang-format on
constexpr const T & min(const T &t1, const T &t2)
Finds the minimum of two values.
Definition: Compiler.h:300
constexpr const T & max(const T &t1, const T &t2)
Finds the maximum of two values.
Definition: Compiler.h:302
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition: Compiler.h:269
constexpr void swap(T &t1, T &t2)
Swaps the values of two objects.
Definition: Compiler.h:282
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &&value)
Forwards an rvalue as an rvalue reference, with a check that it's not an lvalue reference.
Definition: Compiler.h:275
Determines if a type is an lvalue reference.
Definition: Compiler.h:252
Determines if a type is an rvalue reference.
Definition: Compiler.h:255
Removes reference from a type T.
Definition: Compiler.h:248