COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
CompileControl.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 2025 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
10
11#pragma once
12
13#ifdef __GNUC__
14
15#define GCC_DIAG_STR(s) #s
16#define GCC_DIAG_JOINSTR(x,y) GCC_DIAG_STR(x ## y)
17#define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
18#define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
19#define CC_DISABLE_WARNINGS() \
20 GCC_DIAG_PRAGMA(push) \
21 GCC_DIAG_PRAGMA(ignored "-Wpedantic") \
22 GCC_DIAG_PRAGMA(ignored "-Wctor-dtor-privacy")\
23 GCC_DIAG_PRAGMA(ignored "-Wold-style-cast") \
24 GCC_DIAG_PRAGMA(ignored "-Wconversion")
25
26#define CC_ENABLE_WARNINGS() GCC_DIAG_PRAGMA(pop)
27
28#define COMMS_GNU_WARNING_PUSH GCC_DIAG_PRAGMA(push)
29#define COMMS_GNU_WARNING_DISABLE(x_) GCC_DIAG_PRAGMA(ignored x_)
30#define COMMS_GNU_WARNING_POP GCC_DIAG_PRAGMA(pop)
31
32#else
33
34#define CC_DISABLE_WARNINGS()
35#define CC_ENABLE_WARNINGS()
36#define COMMS_GNU_WARNING_PUSH
37#define COMMS_GNU_WARNING_DISABLE(x_)
38#define COMMS_GNU_WARNING_POP
39
40#endif
41
42#define COMMS_IS_MSVC false
43#define COMMS_IS_GCC false
44#define COMMS_IS_CLANG false
45#define COMMS_IS_USING_GNUC false
46
47#if defined(__GNUC__)
48#undef COMMS_IS_USING_GNUC
49#define COMMS_IS_USING_GNUC true
50#endif // #if defined(__GNUC__) && !defined(__clang__)
51
52#if defined(_MSC_VER) && !defined(__clang__)
53#undef COMMS_IS_MSVC
54#define COMMS_IS_MSVC true
55#endif // #if defined(_MSC_VER) && !defined(__clang__)
56
57#if defined(__GNUC__) && !defined(__clang__)
58#undef COMMS_IS_GCC
59#define COMMS_IS_GCC true
60#endif // #if defined(__GNUC__) && !defined(__clang__)
61
62#if defined(__clang__)
63
64#undef COMMS_IS_CLANG
65#define COMMS_IS_CLANG true
66#endif // #if defined(__clang__)
67
68#define COMMS_IS_GCC_47_OR_BELOW (COMMS_IS_GCC && (__GNUC__ == 4) && (__GNUC_MINOR__ < 8))
69#define COMMS_IS_GCC_9 (COMMS_IS_GCC && (__GNUC__ == 9))
70#define COMMS_IS_GCC_9_OR_BELOW (COMMS_IS_GCC_9 && (__GNUC__ <= 9))
71#define COMMS_IS_GCC_9_OR_ABOVE (COMMS_IS_GCC_9 && (__GNUC__ >= 9))
72#define COMMS_IS_GCC_10 (COMMS_IS_GCC && (__GNUC__ == 10))
73#define COMMS_IS_GCC_10_OR_ABOVE (COMMS_IS_GCC && (__GNUC__ >= 10))
74#define COMMS_IS_GCC_10_OR_BELOW (COMMS_IS_GCC && (__GNUC__ <= 10))
75#define COMMS_IS_GCC_11 (COMMS_IS_GCC && (__GNUC__ == 11))
76#define COMMS_IS_GCC_11_OR_ABOVE (COMMS_IS_GCC && (__GNUC__ >= 11))
77#define COMMS_IS_GCC_12 (COMMS_IS_GCC && (__GNUC__ == 12))
78#define COMMS_IS_GCC_12_OR_ABOVE (COMMS_IS_GCC && (__GNUC__ >= 12))
79#define COMMS_IS_GCC_13 (COMMS_IS_GCC && (__GNUC__ == 13))
80#define COMMS_IS_GCC_14 (COMMS_IS_GCC && (__GNUC__ == 14))
81#define COMMS_IS_GCC_14_OR_BELOW (COMMS_IS_GCC && (__GNUC__ <= 14))
82#define COMMS_IS_CLANG_7_OR_ABOVE (COMMS_IS_CLANG && (__clang_major__ >= 7))
83#define COMMS_IS_CLANG_8 (COMMS_IS_CLANG && (__clang_major__ == 8))
84#define COMMS_IS_CLANG_8_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 8))
85#define COMMS_IS_CLANG_9_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 9))
86#define COMMS_IS_CLANG_9_OR_ABOVE (COMMS_IS_CLANG && (__clang_major__ >= 9))
87#define COMMS_IS_CLANG_16_OR_ABOVE (COMMS_IS_CLANG && (__clang_major__ >= 16))
88#define COMMS_IS_CLANG_18_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 18))
89#define COMMS_IS_CLANG_19_OR_BELOW (COMMS_IS_CLANG && (__clang_major__ <= 19))
90#define COMMS_IS_MSVC_2025 (COMMS_IS_MSVC && (_MSC_VER >= 1940) && (_MSC_VER < 1949))
91#define COMMS_IS_MSVC_2025_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1950))
92#define COMMS_IS_MSVC_2022 (COMMS_IS_MSVC && (_MSC_VER >= 1930) && (_MSC_VER < 1939))
93#define COMMS_IS_MSVC_2019 (COMMS_IS_MSVC && (_MSC_VER >= 1920) && (_MSC_VER < 1930))
94#define COMMS_IS_MSVC_2019_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1930))
95#define COMMS_IS_MSVC_2017_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1920))
96#define COMMS_IS_MSVC_2015_OR_BELOW (COMMS_IS_MSVC && (_MSC_VER < 1910))
97
98#if !defined(COMMS_COMPILER_GCC47) && COMMS_IS_GCC_47_OR_BELOW
99#define COMMS_COMPILER_GCC47
100#endif
101
102#define COMMS_IS_CPP14 (__cplusplus >= 201402L)
103#define COMMS_IS_CPP17 (__cplusplus >= 201703L)
104#define COMMS_IS_CPP20 (__cplusplus >= 202002L)
105#define COMMS_IS_CPP23 (__cplusplus >= 202302L)
106
107#if COMMS_IS_MSVC_2025_OR_BELOW
108#undef COMMS_IS_CPP23
109#define COMMS_IS_CPP23 (__cplusplus >= 202003L)
110#endif
111
112#if COMMS_IS_MSVC_2019_OR_BELOW
113#undef COMMS_IS_CPP20
114#define COMMS_IS_CPP20 (__cplusplus >= 201704L)
115#endif
116
117#if COMMS_IS_GCC_10_OR_BELOW
118#undef COMMS_IS_CPP20
119#define COMMS_IS_CPP20 (__cplusplus >= 201709L)
120#endif
121
122#if COMMS_IS_CLANG_9_OR_BELOW
123#undef COMMS_IS_CPP20
124#define COMMS_IS_CPP20 (__cplusplus >= 201709L)
125#endif
126
127#if COMMS_IS_CLANG_8_OR_BELOW
128#undef COMMS_IS_CPP20
129#define COMMS_IS_CPP20 (__cplusplus >= 201707L)
130#endif
131
132#define COMMS_CLANG_HAS_STRING_VIEW false
133#define COMMS_CLANG_HAS_VERSION_HEADER false
134
135#if COMMS_IS_CLANG
136
137// The defines below are seperate because VS2015 doesn't
138// behave well with angle brackets inside macro arguments.
139
140#undef COMMS_CLANG_HAS_STRING_VIEW
141#define COMMS_CLANG_HAS_STRING_VIEW (__has_include(<string_view>))
142
143#undef COMMS_CLANG_HAS_VERSION_HEADER
144#define COMMS_CLANG_HAS_VERSION_HEADER (__has_include(<version>))
145
146#endif // #if COMMS_IS_CLANG
147
148#define COMMS_HAS_CPP20_VERSION_HEADER \
149 COMMS_IS_CPP20 && \
150 (\
151 (COMMS_IS_USING_GNUC && (__GNUC__ >= 9)) || \
152 (COMMS_IS_CLANG && COMMS_CLANG_HAS_VERSION_HEADER) || \
153 (COMMS_IS_MSVC && (_MSC_VER >= 1922)) \
154 )
155
156#if COMMS_HAS_CPP20_VERSION_HEADER
157#include <version>
158#endif
159
160#define COMMS_HAS_CPP17_STRING_VIEW false
161
162#ifndef COMMS_NO_CPP17_STRING_VIEW
163#undef COMMS_HAS_CPP17_STRING_VIEW
164#define COMMS_HAS_CPP17_STRING_VIEW \
165 COMMS_IS_CPP17 && \
166 (\
167 (COMMS_IS_USING_GNUC && (__GNUC__ >= 7)) || \
168 (COMMS_IS_CLANG && COMMS_CLANG_HAS_STRING_VIEW) || \
169 (COMMS_IS_MSVC && (_MSC_VER >= 1910)) \
170 )
171
172#endif // #ifndef COMMS_NO_CPP17_STRING_VIEW
173
174#define COMMS_HAS_CPP20_SPAN false
175#if !defined(COMMS_NO_CPP20_SPAN) && COMMS_IS_CPP20 && defined(__cpp_lib_span)
176#undef COMMS_HAS_CPP20_SPAN
177#define COMMS_HAS_CPP20_SPAN true
178#endif // #if COMMS_IS_CPP20 && defined(__cpp_lib_span)
179
180#if COMMS_IS_MSVC
181
182#define COMMS_MSVC_WARNING_PRAGMA(s_) __pragma(s_)
183#define COMMS_MSVC_WARNING_PUSH __pragma(warning(push))
184#define COMMS_MSVC_WARNING_POP __pragma(warning(pop))
185#define COMMS_MSVC_WARNING_DISABLE(w_) __pragma(warning(disable:w_))
186#define COMMS_MSVC_WARNING_SUPPRESS(w_) __pragma(warning(suppress:w_))
187
188#else // #if COMMS_IS_MSVC
189
190#define COMMS_MSVC_WARNING_PRAGMA(s_)
191#define COMMS_MSVC_WARNING_PUSH
192#define COMMS_MSVC_WARNING_POP
193#define COMMS_MSVC_WARNING_DISABLE(w_)
194#define COMMS_MSVC_WARNING_SUPPRESS(w_)
195
196#endif // #if COMMS_IS_MSVC
197