guanaqo 1.0.0-alpha.27
Utilities for scientific software
Loading...
Searching...
No Matches
preprocessor.h
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup macros
5/// Token concatenation and argument-counting helpers.
6
7// Concatenation helper macros
8#define GUANAQO_CONCATENATE_TOKENS_IMPL(a, b) a##b
9/// Concatenate the given tokens.
10/// @ingroup macros
11#define GUANAQO_CONCATENATE_TOKENS(a, b) GUANAQO_CONCATENATE_TOKENS_IMPL(a, b)
12
13// Counting the number of arguments (up to 10 arguments supported here)
14#define GUANAQO_NUM_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
15#define GUANAQO_NUM_ARGS(...) \
16 GUANAQO_NUM_ARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
17
18// Macro to select the correct GUANAQO_JOIN_TOKENS_ARGS_N macro
19#define GUANAQO_JOIN_TOKENS_ARGS_CHOOSER(N) \
20 GUANAQO_CONCATENATE_TOKENS(GUANAQO_JOIN_TOKENS_ARGS_, N)
21/// Join the given tokens with underscores.
22/// @ingroup macros
23#define GUANAQO_JOIN_TOKENS(...) \
24 GUANAQO_JOIN_TOKENS_ARGS_CHOOSER(GUANAQO_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
25
26// GUANAQO_JOIN_TOKENS_ARGS_N macros (up to 10 arguments)
27// clang-format off
28#define GUANAQO_JOIN_TOKENS_ARGS_1(x1) x1
29#define GUANAQO_JOIN_TOKENS_ARGS_2(x1, x2) x1##_##x2
30#define GUANAQO_JOIN_TOKENS_ARGS_3(x1, x2, x3) x1##_##x2##_##x3
31#define GUANAQO_JOIN_TOKENS_ARGS_4(x1, x2, x3, x4) x1##_##x2##_##x3##_##x4
32#define GUANAQO_JOIN_TOKENS_ARGS_5(x1, x2, x3, x4, x5) x1##_##x2##_##x3##_##x4##_##x5
33#define GUANAQO_JOIN_TOKENS_ARGS_6(x1, x2, x3, x4, x5, x6) x1##_##x2##_##x3##_##x4##_##x5##_##x6
34#define GUANAQO_JOIN_TOKENS_ARGS_7(x1, x2, x3, x4, x5, x6, x7) x1##_##x2##_##x3##_##x4##_##x5##_##x6##_##x7
35#define GUANAQO_JOIN_TOKENS_ARGS_8(x1, x2, x3, x4, x5, x6, x7, x8) x1##_##x2##_##x3##_##x4##_##x5##_##x6##_##x7##_##x8
36#define GUANAQO_JOIN_TOKENS_ARGS_9(x1, x2, x3, x4, x5, x6, x7, x8, x9) x1##_##x2##_##x3##_##x4##_##x5##_##x6##_##x7##_##x8##_##x9
37#define GUANAQO_JOIN_TOKENS_ARGS_10(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) x1##_##x2##_##x3##_##x4##_##x5##_##x6##_##x7##_##x8##_##x9##_##x10
38// clang-format on
39
40// Macro to select the correct GUANAQO_JOIN_STRINGS_ARGS_N macro
41#define GUANAQO_JOIN_STRINGS_ARGS_CHOOSER(N) \
42 GUANAQO_CONCATENATE_TOKENS(GUANAQO_JOIN_STRINGS_ARGS_, N)
43/// Join the given strings with the given separator.
44/// @ingroup macros
45#define GUANAQO_JOIN_STRINGS(sep, ...) \
46 GUANAQO_JOIN_STRINGS_ARGS_CHOOSER(GUANAQO_NUM_ARGS(__VA_ARGS__))( \
47 sep, __VA_ARGS__)
48
49// GUANAQO_JOIN_STRINGS_ARGS_N macros (up to 10 arguments)
50// clang-format off
51#define GUANAQO_JOIN_STRINGS_ARGS_1(sep, x1) #x1
52#define GUANAQO_JOIN_STRINGS_ARGS_2(sep, x1, x2) #x1 sep #x2
53#define GUANAQO_JOIN_STRINGS_ARGS_3(sep, x1, x2, x3) #x1 sep #x2 sep #x3
54#define GUANAQO_JOIN_STRINGS_ARGS_4(sep, x1, x2, x3, x4) #x1 sep #x2 sep #x3 sep #x4
55#define GUANAQO_JOIN_STRINGS_ARGS_5(sep, x1, x2, x3, x4, x5) #x1 sep #x2 sep #x3 sep #x4 sep #x5
56#define GUANAQO_JOIN_STRINGS_ARGS_6(sep, x1, x2, x3, x4, x5, x6) #x1 sep #x2 sep #x3 sep #x4 sep #x5 sep #x6
57#define GUANAQO_JOIN_STRINGS_ARGS_7(sep, x1, x2, x3, x4, x5, x6, x7) #x1 sep #x2 sep #x3 sep #x4 sep #x5 sep #x6 sep #x7
58#define GUANAQO_JOIN_STRINGS_ARGS_8(sep, x1, x2, x3, x4, x5, x6, x7, x8) #x1 sep #x2 sep #x3 sep #x4 sep #x5 sep #x6 sep #x7 sep #x8
59#define GUANAQO_JOIN_STRINGS_ARGS_9(sep, x1, x2, x3, x4, x5, x6, x7, x8, x9) #x1 sep #x2 sep #x3 sep #x4 sep #x5 sep #x6 sep #x7 sep #x8 sep #x9
60#define GUANAQO_JOIN_STRINGS_ARGS_10(sep, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) #x1 sep #x2 sep #x3 sep #x4 sep #x5 sep #x6 sep #x7 sep #x8 sep #x9 sep #x10
61// clang-format on
62
63/// @def GUANAQO_NOOP()
64/// @ingroup macros
65/// A no-op statement (swallows a semicolon without causing warnings about empty statements).
66#define GUANAQO_NOOP() \
67 do { \
68 } while (false)