MongoDB C++ Driver mongocxx-3.11.0
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1// Convert the given macro argument to a string literal, after macro expansion.
2#define BSONCXX_STRINGIFY(...) BSONCXX_STRINGIFY_IMPL(__VA_ARGS__)
3#define BSONCXX_STRINGIFY_IMPL(...) #__VA_ARGS__
4
5// Token-paste two macro arguments, after macro expansion
6#define BSONCXX_CONCAT(A, ...) BSONCXX_CONCAT_IMPL(A, __VA_ARGS__)
7#define BSONCXX_CONCAT_IMPL(A, ...) A##__VA_ARGS__
8
9// Expands to a _Pragma() preprocessor directive, after macro expansion
10//
11// The arguments an arbitrary "token soup", and should not be quoted like a regular
12// _Pragma. This macro will stringify-them itself.
13//
14// Example:
15//
16// BSONCXX_PRAGMA(GCC diagnostic ignore "-Wconversion")
17//
18// will become:
19//
20// _Pragma("GCC diagnostic ignore \"-Wconversion\"")
21//
22#define BSONCXX_PRAGMA(...) _bsoncxxPragma(__VA_ARGS__)
23#ifdef _MSC_VER
24// Old MSVC doesn't recognize C++11 _Pragma(), but it always recognized __pragma
25#define _bsoncxxPragma(...) __pragma(__VA_ARGS__)
26#else
27#define _bsoncxxPragma(...) _Pragma(BSONCXX_STRINGIFY(__VA_ARGS__))
28#endif
29
30// Use in a declaration position to force the appearence of a semicolon
31// as the next token. Use this for statement-like or declaration-like macros to
32// enforce that their call sites are followed by a semicolon
33#define BSONCXX_FORCE_SEMICOLON static_assert(true, "")
34
35// Add a trailing noexcept, decltype-return, and return-body to a
36// function definition. (Not compatible with lambda expressions.)
37//
38// Example:
39//
40// template <typename T>
41// auto foo(T x, T y) BSONCXX_RETURNS(x + y);
42//
43// Becomes:
44//
45// template <typename T>
46// auto foo(T x, T y) noexcept(noexcept(x + y))
47// -> decltype(x + y)
48// { return x + y };
49//
50#define BSONCXX_RETURNS(...) \
51 noexcept(noexcept(__VA_ARGS__))->decltype(__VA_ARGS__) { \
52 return __VA_ARGS__; \
53 } \
54 BSONCXX_FORCE_SEMICOLON
55
56// @macro mongocxx_cxx14_constexpr
57// Expands to `constexpr` if compiling as c++14 or greater, otherwise
58// expands to `inline`.
59//
60// Use this on functions that can only be constexpr in C++14 or newer, including
61// non-const member functions.
62#if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L && _MSC_VER > 1910)
63#define bsoncxx_cxx14_constexpr constexpr
64#else
65#define bsoncxx_cxx14_constexpr inline
66#endif
67
68// Disable a warning for a particular compiler.
69//
70// The argument should be of the form:
71//
72// - Clang(<flag-string-literal>)
73// - GCC(<flag-string-literal>)
74// - GNU(<flag-string-literal>)
75// - MSVC(<id-integer-literal>)
76//
77// The "GNU" form applies to both GCC and Clang
78#define BSONCXX_DISABLE_WARNING(Spec) \
79 BSONCXX_CONCAT(_bsoncxxDisableWarningImpl_for_, Spec) \
80 BSONCXX_FORCE_SEMICOLON
81
82// Push the current compiler diagnostics settings state
83#define BSONCXX_PUSH_WARNINGS() \
84 BSONCXX_IF_GNU_LIKE(BSONCXX_PRAGMA(GCC diagnostic push)) \
85 BSONCXX_IF_MSVC(BSONCXX_PRAGMA(warning(push))) \
86 BSONCXX_FORCE_SEMICOLON
87
88// Restore prior compiler diagnostics settings from before the most
89// recent BSONCXX_PUSH_WARNINGS()
90#define BSONCXX_POP_WARNINGS() \
91 BSONCXX_IF_GNU_LIKE(BSONCXX_PRAGMA(GCC diagnostic pop)) \
92 BSONCXX_IF_MSVC(BSONCXX_PRAGMA(warning(pop))) \
93 BSONCXX_FORCE_SEMICOLON
94
95#define _bsoncxxDisableWarningImpl_for_GCC(...) \
96 BSONCXX_IF_GCC(BSONCXX_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
97
98#define _bsoncxxDisableWarningImpl_for_Clang(...) \
99 BSONCXX_IF_CLANG(BSONCXX_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
100
101#define _bsoncxxDisableWarningImpl_for_GNU(...) \
102 _bsoncxxDisableWarningImpl_for_GCC(__VA_ARGS__) \
103 _bsoncxxDisableWarningImpl_for_Clang(__VA_ARGS__)
104
105#define _bsoncxxDisableWarningImpl_for_MSVC(...) \
106 BSONCXX_IF_MSVC(BSONCXX_PRAGMA(warning(disable : __VA_ARGS__)))
107
108#define BSONCXX_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
109