Blender V5.3
GHOST_utildefines.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include <cstddef>
14
16
17/* -------------------------------------------------------------------- */
20
21/* unpack vector for args */
22#define UNPACK2(a) ((a)[0]), ((a)[1])
23#define UNPACK3(a) UNPACK2(a), ((a)[2])
24#define UNPACK4(a) UNPACK3(a), ((a)[3])
25/* pre may be '&', '*' or func, post may be '->member' */
26#define UNPACK2_EX(pre, a, post) (pre((a)[0]) post), (pre((a)[1]) post)
27#define UNPACK3_EX(pre, a, post) UNPACK2_EX(pre, a, post), (pre((a)[2]) post)
28#define UNPACK4_EX(pre, a, post) UNPACK3_EX(pre, a, post), (pre((a)[3]) post)
29
31
32/* -------------------------------------------------------------------- */
35
36/* Assuming a static array. */
37template<class T, size_t N> constexpr size_t ARRAY_SIZE(const T (&arg)[N]) noexcept
38{
39 (void)arg;
40 return N;
41}
42
44
45/* -------------------------------------------------------------------- */
48
49/* Manual line breaks for readability. */
50/* clang-format off */
51
52/* ELEM#(v, ...): is the first arg equal any others? */
53/* internal helpers. */
54#define _VA_ELEM2(v, a) ((v) == (a))
55#define _VA_ELEM3(v, a, b) \
56 (_VA_ELEM2(v, a) || _VA_ELEM2(v, b))
57#define _VA_ELEM4(v, a, b, c) \
58 (_VA_ELEM3(v, a, b) || _VA_ELEM2(v, c))
59#define _VA_ELEM5(v, a, b, c, d) \
60 (_VA_ELEM4(v, a, b, c) || _VA_ELEM2(v, d))
61#define _VA_ELEM6(v, a, b, c, d, e) \
62 (_VA_ELEM5(v, a, b, c, d) || _VA_ELEM2(v, e))
63#define _VA_ELEM7(v, a, b, c, d, e, f) \
64 (_VA_ELEM6(v, a, b, c, d, e) || _VA_ELEM2(v, f))
65#define _VA_ELEM8(v, a, b, c, d, e, f, g) \
66 (_VA_ELEM7(v, a, b, c, d, e, f) || _VA_ELEM2(v, g))
67#define _VA_ELEM9(v, a, b, c, d, e, f, g, h) \
68 (_VA_ELEM8(v, a, b, c, d, e, f, g) || _VA_ELEM2(v, h))
69#define _VA_ELEM10(v, a, b, c, d, e, f, g, h, i) \
70 (_VA_ELEM9(v, a, b, c, d, e, f, g, h) || _VA_ELEM2(v, i))
71#define _VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
72 (_VA_ELEM10(v, a, b, c, d, e, f, g, h, i) || _VA_ELEM2(v, j))
73#define _VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
74 (_VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || _VA_ELEM2(v, k))
75#define _VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
76 (_VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || _VA_ELEM2(v, l))
77#define _VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
78 (_VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || _VA_ELEM2(v, m))
79#define _VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
80 (_VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || _VA_ELEM2(v, n))
81#define _VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
82 (_VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || _VA_ELEM2(v, o))
83#define _VA_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
84 (_VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || _VA_ELEM2(v, p))
85/* clang-format on */
86
87/* reusable ELEM macro */
88#define ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_ELEM, __VA_ARGS__)
89
91
92/* -------------------------------------------------------------------- */
95
96/* Macro to convert a value to string in the preprocessor:
97 * - `STRINGIFY_ARG`: gives the argument as a string
98 * - `STRINGIFY_APPEND`: appends any argument 'b' onto the string argument 'a',
99 * used by `STRINGIFY` because some preprocessors warn about zero arguments.
100 * - `STRINGIFY`: gives the argument's value as a string. */
101
102#define STRINGIFY_ARG(x) "" #x
103#define STRINGIFY_APPEND(a, b) "" a #b
104#define STRINGIFY(x) STRINGIFY_APPEND("", x)
105
106/* generic strcmp macros */
107#if defined(_MSC_VER)
108# define strcasecmp _stricmp
109# define strncasecmp _strnicmp
110#endif
111
112#define STREQ(a, b) (strcmp(a, b) == 0)
113#define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
114#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
115#define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
116
117#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
118
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
#define N
#define T