plugify  1.0.0.0
macro.hpp
1 #pragma once
2 
3 #ifndef __has_cpp_attribute
4 # define __has_cpp_attribute(x) 0
5 #endif
6 #ifndef __has_extension
7 # define __has_extension(x) 0
8 #endif
9 #ifndef __has_feature
10 # define __has_feature(x) 0
11 #endif
12 #ifndef __has_include
13 # define __has_include(x) 0
14 #endif
15 #ifndef __has_builtin
16 # define __has_builtin(x) 0
17 #endif
18 
19 #define PLUGIFY_HAS_EXCEPTIONS (__cpp_exceptions || __EXCEPTIONS || _HAS_EXCEPTIONS)
20 
21 #ifndef PLUGIFY_EXCEPTIONS
22 # if PLUGIFY_HAS_EXCEPTIONS
23 # define PLUGIFY_EXCEPTIONS 1
24 # else
25 # define PLUGIFY_EXCEPTIONS 0
26 # endif
27 #endif
28 
29 #if PLUGIFY_EXCEPTIONS && (!PLUGIFY_HAS_EXCEPTIONS || !__has_include(<stdexcept>))
30 # undef PLUGIFY_EXCEPTIONS
31 # define PLUGIFY_EXCEPTIONS 0
32 #endif
33 
34 #ifndef PLUGIFY_FALLBACK_ASSERT
35 # define PLUGIFY_FALLBACK_ASSERT 1
36 #endif
37 
38 #if PLUGIFY_FALLBACK_ASSERT && !__has_include(<cassert>)
39 # undef PLUGIFY_FALLBACK_ASSERT
40 # define PLUGIFY_FALLBACK_ASSERT 0
41 #endif
42 
43 #ifndef PLUGIFY_FALLBACK_ABORT
44 # define PLUGIFY_FALLBACK_ABORT 1
45 #endif
46 
47 #if PLUGIFY_FALLBACK_ABORT && !__has_include(<cstdlib>)
48 # undef PLUGIFY_FALLBACK_ABORT
49 # define PLUGIFY_FALLBACK_ABORT 0
50 #endif
51 
52 #ifndef PLUGIFY_FALLBACK_ABORT_FUNCTION
53 # define PLUGIFY_FALLBACK_ABORT_FUNCTION [] (auto) { }
54 #endif
55 
56 #if PLUGIFY_EXCEPTIONS
57 # include <stdexcept>
58 # define PLUGIFY_ASSERT(x, str, e) do { if (!(x)) [[unlikely]] throw e(str); } while (0)
59 #elif PLUGIFY_FALLBACK_ASSERT
60 # include <cassert>
61 # define PLUGIFY_ASSERT(x, str, ...) assert(x && str)
62 #elif PLUGIFY_FALLBACK_ABORT
63 # include <cstdlib>
64 # define PLUGIFY_ASSERT(x, ...) do { if (!(x)) [[unlikely]] { std::abort(); } } while (0)
65 #else
66 # define PLUGIFY_ASSERT(x, str, ...) do { if (!(x)) [[unlikely]] { PLUGIFY_FALLBACK_ABORT_FUNCTION (str); { while (true) { [] { } (); } } } } while (0)
67 #endif
68 
69 #define PLUGIFY_PRAGMA_IMPL(x) _Pragma(#x)
70 #define PLUGIFY_PRAGMA(x) PLUGIFY_PRAGMA_IMPL(x)
71 
72 #if defined(__clang__)
73 # define PLUGIFY_PRAGMA_DIAG_PREFIX clang
74 #elif defined(__GNUC__)
75 # define PLUGIFY_PRAGMA_DIAG_PREFIX GCC
76 #endif
77 
78 #if defined(__GNUC__) || defined(__clang__)
79 # define PLUGIFY_WARN_PUSH() PLUGIFY_PRAGMA(PLUGIFY_PRAGMA_DIAG_PREFIX diagnostic push)
80 # define PLUGIFY_WARN_IGNORE(wrn) PLUGIFY_PRAGMA(PLUGIFY_PRAGMA_DIAG_PREFIX diagnostic ignored wrn)
81 # define PLUGIFY_WARN_POP() PLUGIFY_PRAGMA(PLUGIFY_PRAGMA_DIAG_PREFIX diagnostic pop)
82 #elif defined(_MSC_VER)
83 # define PLUGIFY_WARN_PUSH() __pragma(warning(push))
84 # define PLUGIFY_WARN_IGNORE(wrn) __pragma(warning(disable: wrn))
85 # define PLUGIFY_WARN_POP() __pragma(warning(pop))
86 #endif
87 
88 #if defined(__GNUC__) || defined(__clang__)
89 # define PLUGIFY_PACK(decl) decl __attribute__((__packed__))
90 #elif defined(_MSC_VER)
91 # define PLUGIFY_PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop))
92 #else
93 # define PLUGIFY_PACK(decl) decl
94 #endif
95 
96 #if defined(__GNUC__) || defined(__clang__)
97 # define PLUGIFY_UNREACHABLE() __builtin_unreachable()
98 #elif defined (_MSC_VER)
99 # define PLUGIFY_UNREACHABLE() __assume(false)
100 #else
101 # error "Compiler not supported, please report an issue."
102 #endif
103 
104 #if defined(_MSC_VER)
105 # define PLUGIFY_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
106 #else
107 # define PLUGIFY_NO_UNIQUE_ADDRESS [[no_unique_address]]
108 #endif
109 
110 #if defined(__clang__)
111 # define PLUGIFY_FORCE_INLINE [[gnu::always_inline]] [[gnu::gnu_inline]] extern inline
112 #elif defined(__GNUC__)
113 # define PLUGIFY_FORCE_INLINE [[gnu::always_inline]] inline
114 #elif defined(_MSC_VER)
115 # pragma warning(error: 4714)
116 # define PLUGIFY_FORCE_INLINE __forceinline
117 #else
118 # define PLUGIFY_FORCE_INLINE inline
119 #endif
120 
121 #if defined(__GNUC__) || defined(__clang__)
122 # define PLUGIFY_RESTRICT __restrict__
123 #elif defined(_MSC_VER)
124 # define PLUGIFY_RESTRICT __restrict
125 #else
126 # define PLUGIFY_RESTRICT
127 #endif