Blender V4.3
BLI_utildefines.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5/* Use a define instead of `#pragma once` because of `BLI_memory_utils.h` */
6#ifndef __BLI_UTILDEFINES_H__
7#define __BLI_UTILDEFINES_H__
8
13/* avoid many includes for now */
14#include "BLI_compiler_compat.h"
15#include "BLI_sys_types.h"
17
18/* We could remove in future. */
19#include "BLI_assert.h"
20
21/* include after _VA_NARGS macro */
23
24#ifdef __cplusplus
25# include <type_traits>
26# include <utility>
27
28extern "C" {
29#endif
30
31/* -------------------------------------------------------------------- */
35#ifndef __cplusplus
36# define MIN2(a, b) ((a) < (b) ? (a) : (b))
37# define MAX2(a, b) ((a) > (b) ? (a) : (b))
38#endif
39
40#define INIT_MINMAX(min, max) \
41 { \
42 (min)[0] = (min)[1] = (min)[2] = 1.0e30f; \
43 (max)[0] = (max)[1] = (max)[2] = -1.0e30f; \
44 } \
45 (void)0
46#define INIT_MINMAX2(min, max) \
47 { \
48 (min)[0] = (min)[1] = 1.0e30f; \
49 (max)[0] = (max)[1] = -1.0e30f; \
50 } \
51 (void)0
52
55/* -------------------------------------------------------------------- */
59#define SWAP(type, a, b) \
60 { \
61 type sw_ap; \
62 CHECK_TYPE(a, type); \
63 CHECK_TYPE(b, type); \
64 sw_ap = (a); \
65 (a) = (b); \
66 (b) = sw_ap; \
67 } \
68 (void)0
69
70/* shift around elements */
71#define SHIFT3(type, a, b, c) \
72 { \
73 type tmp; \
74 CHECK_TYPE(a, type); \
75 CHECK_TYPE(b, type); \
76 CHECK_TYPE(c, type); \
77 tmp = a; \
78 a = c; \
79 c = b; \
80 b = tmp; \
81 } \
82 (void)0
83
84#define SHIFT4(type, a, b, c, d) \
85 { \
86 type tmp; \
87 CHECK_TYPE(a, type); \
88 CHECK_TYPE(b, type); \
89 CHECK_TYPE(c, type); \
90 CHECK_TYPE(d, type); \
91 tmp = a; \
92 a = d; \
93 d = c; \
94 c = b; \
95 b = tmp; \
96 } \
97 (void)0
98
101/* -------------------------------------------------------------------- */
105/* Manual line breaks for readability. */
106/* clang-format off */
107
108/* ELEM#(v, ...): is the first arg equal any others? */
109/* internal helpers. */
110#define _VA_ELEM2(v, a) ((v) == (a))
111#define _VA_ELEM3(v, a, b) \
112 (_VA_ELEM2(v, a) || _VA_ELEM2(v, b))
113#define _VA_ELEM4(v, a, b, c) \
114 (_VA_ELEM3(v, a, b) || _VA_ELEM2(v, c))
115#define _VA_ELEM5(v, a, b, c, d) \
116 (_VA_ELEM4(v, a, b, c) || _VA_ELEM2(v, d))
117#define _VA_ELEM6(v, a, b, c, d, e) \
118 (_VA_ELEM5(v, a, b, c, d) || _VA_ELEM2(v, e))
119#define _VA_ELEM7(v, a, b, c, d, e, f) \
120 (_VA_ELEM6(v, a, b, c, d, e) || _VA_ELEM2(v, f))
121#define _VA_ELEM8(v, a, b, c, d, e, f, g) \
122 (_VA_ELEM7(v, a, b, c, d, e, f) || _VA_ELEM2(v, g))
123#define _VA_ELEM9(v, a, b, c, d, e, f, g, h) \
124 (_VA_ELEM8(v, a, b, c, d, e, f, g) || _VA_ELEM2(v, h))
125#define _VA_ELEM10(v, a, b, c, d, e, f, g, h, i) \
126 (_VA_ELEM9(v, a, b, c, d, e, f, g, h) || _VA_ELEM2(v, i))
127#define _VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
128 (_VA_ELEM10(v, a, b, c, d, e, f, g, h, i) || _VA_ELEM2(v, j))
129#define _VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
130 (_VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || _VA_ELEM2(v, k))
131#define _VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
132 (_VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || _VA_ELEM2(v, l))
133#define _VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
134 (_VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || _VA_ELEM2(v, m))
135#define _VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
136 (_VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || _VA_ELEM2(v, n))
137#define _VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
138 (_VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || _VA_ELEM2(v, o))
139#define _VA_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
140 (_VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || _VA_ELEM2(v, p))
141/* clang-format on */
142
143/* reusable ELEM macro */
144#define ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_ELEM, __VA_ARGS__)
145
148/* -------------------------------------------------------------------- */
152/* Float equality checks. */
153
154#define IS_EQ(a, b) \
155 (CHECK_TYPE_INLINE_NONCONST(a, double), \
156 CHECK_TYPE_INLINE_NONCONST(b, double), \
157 ((fabs((double)((a) - (b))) >= (double)FLT_EPSILON) ? false : true))
158
159#define IS_EQF(a, b) \
160 (CHECK_TYPE_INLINE_NONCONST(a, float), \
161 CHECK_TYPE_INLINE_NONCONST(b, float), \
162 ((fabsf((float)((a) - (b))) >= (float)FLT_EPSILON) ? false : true))
163
164#define IS_EQT(a, b, c) (((a) > (b)) ? ((((a) - (b)) <= (c))) : (((((b) - (a)) <= (c)))))
165#define IN_RANGE(a, b, c) (((b) < (c)) ? (((b) < (a) && (a) < (c))) : (((c) < (a) && (a) < (b))))
166#define IN_RANGE_INCL(a, b, c) \
167 (((b) < (c)) ? (((b) <= (a) && (a) <= (c))) : (((c) <= (a) && (a) <= (b))))
168
177#define DECIMAL_DIGITS_BOUND(t) (241 * sizeof(t) / 100 + 1)
178
179#ifdef __cplusplus
180inline constexpr int64_t is_power_of_2(const int64_t x)
181{
182 BLI_assert(x >= 0);
183 return (x & (x - 1)) == 0;
184}
185
186inline constexpr int64_t log2_floor(const int64_t x)
187{
188 BLI_assert(x >= 0);
189 return x <= 1 ? 0 : 1 + log2_floor(x >> 1);
190}
191
192inline constexpr int64_t log2_ceil(const int64_t x)
193{
194 BLI_assert(x >= 0);
195 return (is_power_of_2(int(x))) ? log2_floor(x) : log2_floor(x) + 1;
196}
197
198inline constexpr int64_t power_of_2_max(const int64_t x)
199{
200 BLI_assert(x >= 0);
201 return 1ll << log2_ceil(x);
202}
203#endif
204
207/* -------------------------------------------------------------------- */
211#define CLAMP(a, b, c) \
212 { \
213 if ((a) < (b)) { \
214 (a) = (b); \
215 } \
216 else if ((a) > (c)) { \
217 (a) = (c); \
218 } \
219 } \
220 (void)0
221
222#define CLAMP_MAX(a, c) \
223 { \
224 if ((a) > (c)) { \
225 (a) = (c); \
226 } \
227 } \
228 (void)0
229
230#define CLAMP_MIN(a, b) \
231 { \
232 if ((a) < (b)) { \
233 (a) = (b); \
234 } \
235 } \
236 (void)0
237
238#define CLAMP3(vec, b, c) \
239 { \
240 CLAMP((vec)[0], b, c); \
241 CLAMP((vec)[1], b, c); \
242 CLAMP((vec)[2], b, c); \
243 } \
244 (void)0
245
246#define CLAMP3_MIN(vec, b) \
247 { \
248 CLAMP_MIN((vec)[0], b); \
249 CLAMP_MIN((vec)[1], b); \
250 CLAMP_MIN((vec)[2], b); \
251 } \
252 (void)0
253
254#define CLAMP4_MIN(vec, b) \
255 { \
256 CLAMP_MIN((vec)[0], b); \
257 CLAMP_MIN((vec)[1], b); \
258 CLAMP_MIN((vec)[2], b); \
259 CLAMP_MIN((vec)[3], b); \
260 } \
261 (void)0
262
265/* -------------------------------------------------------------------- */
269/* unpack vector for args */
270#define UNPACK2(a) ((a)[0]), ((a)[1])
271#define UNPACK3(a) UNPACK2(a), ((a)[2])
272#define UNPACK4(a) UNPACK3(a), ((a)[3])
273/* pre may be '&', '*' or func, post may be '->member' */
274#define UNPACK2_EX(pre, a, post) (pre((a)[0]) post), (pre((a)[1]) post)
275#define UNPACK3_EX(pre, a, post) UNPACK2_EX(pre, a, post), (pre((a)[2]) post)
276#define UNPACK4_EX(pre, a, post) UNPACK3_EX(pre, a, post), (pre((a)[3]) post)
277
280/* -------------------------------------------------------------------- */
284#define ARRAY_HAS_ITEM(arr_item, arr_start, arr_len) \
285 (CHECK_TYPE_PAIR_INLINE(arr_start, arr_item), \
286 ((size_t)((arr_item) - (arr_start)) < (size_t)(arr_len)))
287
288/* assuming a static array */
289#ifndef __cplusplus
290# if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) && \
291 !defined(__INTEL_COMPILER)
292# define ARRAY_SIZE(arr) \
293 ((sizeof(struct { int isnt_array : ((const void *)&(arr) == &(arr)[0]); }) * 0) + \
294 (sizeof(arr) / sizeof(*(arr))))
295# else
296# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
297# endif
298#endif
299
300/* ARRAY_SET_ITEMS#(v, ...): set indices of array 'v' */
301/* internal helpers */
302#define _VA_ARRAY_SET_ITEMS2(v, a) ((v)[0] = (a))
303#define _VA_ARRAY_SET_ITEMS3(v, a, b) \
304 _VA_ARRAY_SET_ITEMS2(v, a); \
305 ((v)[1] = (b))
306#define _VA_ARRAY_SET_ITEMS4(v, a, b, c) \
307 _VA_ARRAY_SET_ITEMS3(v, a, b); \
308 ((v)[2] = (c))
309#define _VA_ARRAY_SET_ITEMS5(v, a, b, c, d) \
310 _VA_ARRAY_SET_ITEMS4(v, a, b, c); \
311 ((v)[3] = (d))
312#define _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e) \
313 _VA_ARRAY_SET_ITEMS5(v, a, b, c, d); \
314 ((v)[4] = (e))
315#define _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f) \
316 _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e); \
317 ((v)[5] = (f))
318#define _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g) \
319 _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f); \
320 ((v)[6] = (g))
321#define _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h) \
322 _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g); \
323 ((v)[7] = (h))
324#define _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i) \
325 _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h); \
326 ((v)[8] = (i))
327#define _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j) \
328 _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i); \
329 ((v)[9] = (j))
330#define _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k) \
331 _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j); \
332 ((v)[10] = (k))
333#define _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
334 _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k); \
335 ((v)[11] = (l))
336#define _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
337 _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l); \
338 ((v)[12] = (m))
339#define _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
340 _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m); \
341 ((v)[13] = (n))
342#define _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
343 _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n); \
344 ((v)[14] = (o))
345#define _VA_ARRAY_SET_ITEMS17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
346 _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); \
347 ((v)[15] = (p))
348
349/* reusable ARRAY_SET_ITEMS macro */
350#define ARRAY_SET_ITEMS(...) \
351 { \
352 VA_NARGS_CALL_OVERLOAD(_VA_ARRAY_SET_ITEMS, __VA_ARGS__); \
353 } \
354 (void)0
355
358/* -------------------------------------------------------------------- */
362#if defined(__GNUC__) || defined(__clang__)
363# define POINTER_OFFSET(v, ofs) ((typeof(v))((char *)(v) + (ofs)))
364#else
365# define POINTER_OFFSET(v, ofs) ((void *)((char *)(v) + (ofs)))
366#endif
367
368/* Warning-free macros for storing ints in pointers. Use these _only_
369 * for storing an int in a pointer, not a pointer in an int (64bit)! */
370#define POINTER_FROM_INT(i) ((void *)(intptr_t)(i))
371#define POINTER_AS_INT(i) ((void)0, ((int)(intptr_t)(i)))
372
373#define POINTER_FROM_UINT(i) ((void *)(uintptr_t)(i))
374#define POINTER_AS_UINT(i) ((void)0, ((unsigned int)(uintptr_t)(i)))
375
378/* -------------------------------------------------------------------- */
387#define OFFSETOF_STRUCT_AFTER(_struct, _member) \
388 ((size_t)(((const char *)&((_struct)->_member)) - ((const char *)(_struct))) + \
389 sizeof((_struct)->_member))
390
396#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member) \
397 { \
398 CHECK_TYPE_NONCONST(struct_dst); \
399 ((void)(struct_dst == struct_src), \
400 memcpy((char *)(struct_dst) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
401 (const char *)(struct_src) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
402 sizeof(*(struct_dst)) - OFFSETOF_STRUCT_AFTER(struct_dst, member))); \
403 } \
404 ((void)0)
405
406#define MEMSET_STRUCT_AFTER(struct_var, value, member) \
407 { \
408 CHECK_TYPE_NONCONST(struct_var); \
409 memset((char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
410 value, \
411 sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)); \
412 } \
413 ((void)0)
414
415/* defined
416 * in memory_utils.c for now. I do not know where we should put it actually... */
417#ifndef __BLI_MEMORY_UTILS_H__
421extern bool BLI_memory_is_zero(const void *arr, size_t arr_size);
422#endif
423
424#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member) \
425 (BLI_memory_is_zero((const char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
426 sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)))
427
430/* -------------------------------------------------------------------- */
434/* Macro to convert a value to string in the preprocessor:
435 * - `STRINGIFY_ARG`: gives the argument as a string
436 * - `STRINGIFY_APPEND`: appends any argument 'b' onto the string argument 'a',
437 * used by `STRINGIFY` because some preprocessors warn about zero arguments.
438 * - `STRINGIFY`: gives the argument's value as a string. */
439
440#define STRINGIFY_ARG(x) "" #x
441#define STRINGIFY_APPEND(a, b) "" a #b
442#define STRINGIFY(x) STRINGIFY_APPEND("", x)
443
444/* generic strcmp macros */
445#if defined(_MSC_VER)
446# define strcasecmp _stricmp
447# define strncasecmp _strnicmp
448#endif
449
450#define STREQ(a, b) (strcmp(a, b) == 0)
451#define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
452#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
453#define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
454
455#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
456
459/* -------------------------------------------------------------------- */
463#ifndef __cplusplus
464/* UNUSED macro, for function argument */
465# if defined(__GNUC__) || defined(__clang__)
466# define UNUSED(x) UNUSED_##x __attribute__((__unused__))
467# elif defined(_MSC_VER)
468/* NOTE: This suppresses the warning for the line, not the attribute. */
469# define UNUSED(x) UNUSED_##x __pragma(warning(suppress : 4100))
470# else
471# define UNUSED(x) UNUSED_##x
472# endif
473#endif
474
479#if defined(__GNUC__) || defined(__clang__)
480# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_##x
481#else
482# define UNUSED_FUNCTION(x) UNUSED_##x
483#endif
484
485#if defined(__GNUC__) || defined(__clang__)
486# define UNUSED_FUNCTION_WITH_RETURN_TYPE(rtype, x) __attribute__((__unused__)) rtype UNUSED_##x
487#else
488# define UNUSED_FUNCTION_WITH_RETURN_TYPE(rtype, x) rtype UNUSED_##x
489#endif
490
504#define _VA_UNUSED_VARS_1(a0) ((void)(a0))
505#define _VA_UNUSED_VARS_2(a0, b0) ((void)(a0), _VA_UNUSED_VARS_1(b0))
506#define _VA_UNUSED_VARS_3(a0, b0, c0) ((void)(a0), _VA_UNUSED_VARS_2(b0, c0))
507#define _VA_UNUSED_VARS_4(a0, b0, c0, d0) ((void)(a0), _VA_UNUSED_VARS_3(b0, c0, d0))
508#define _VA_UNUSED_VARS_5(a0, b0, c0, d0, e0) ((void)(a0), _VA_UNUSED_VARS_4(b0, c0, d0, e0))
509#define _VA_UNUSED_VARS_6(a0, b0, c0, d0, e0, f0) \
510 ((void)(a0), _VA_UNUSED_VARS_5(b0, c0, d0, e0, f0))
511#define _VA_UNUSED_VARS_7(a0, b0, c0, d0, e0, f0, g0) \
512 ((void)(a0), _VA_UNUSED_VARS_6(b0, c0, d0, e0, f0, g0))
513#define _VA_UNUSED_VARS_8(a0, b0, c0, d0, e0, f0, g0, h0) \
514 ((void)(a0), _VA_UNUSED_VARS_7(b0, c0, d0, e0, f0, g0, h0))
515#define _VA_UNUSED_VARS_9(a0, b0, c0, d0, e0, f0, g0, h0, i0) \
516 ((void)(a0), _VA_UNUSED_VARS_8(b0, c0, d0, e0, f0, g0, h0, i0))
517#define _VA_UNUSED_VARS_10(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0) \
518 ((void)(a0), _VA_UNUSED_VARS_9(b0, c0, d0, e0, f0, g0, h0, i0, j0))
519#define _VA_UNUSED_VARS_11(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0) \
520 ((void)(a0), _VA_UNUSED_VARS_10(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0))
521#define _VA_UNUSED_VARS_12(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0) \
522 ((void)(a0), _VA_UNUSED_VARS_11(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0))
523#define _VA_UNUSED_VARS_13(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0) \
524 ((void)(a0), _VA_UNUSED_VARS_12(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0))
525#define _VA_UNUSED_VARS_14(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0) \
526 ((void)(a0), _VA_UNUSED_VARS_13(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0))
527#define _VA_UNUSED_VARS_15(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0) \
528 ((void)(a0), _VA_UNUSED_VARS_14(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0))
529#define _VA_UNUSED_VARS_16(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0) \
530 ((void)(a0), _VA_UNUSED_VARS_15(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0))
531
532/* reusable ELEM macro */
533#define UNUSED_VARS(...) VA_NARGS_CALL_OVERLOAD(_VA_UNUSED_VARS_, __VA_ARGS__)
534
535/* for debug-only variables */
536#ifndef NDEBUG
537# define UNUSED_VARS_NDEBUG(...)
538#else
539# define UNUSED_VARS_NDEBUG UNUSED_VARS
540#endif
541
544/* -------------------------------------------------------------------- */
548/* hints for branch prediction, only use in code that runs a _lot_ where */
549#ifdef __GNUC__
550# define LIKELY(x) __builtin_expect(!!(x), 1)
551# define UNLIKELY(x) __builtin_expect(!!(x), 0)
552#else
553# define LIKELY(x) (x)
554# define UNLIKELY(x) (x)
555#endif
556
559/* -------------------------------------------------------------------- */
563/* Set flag from a single test */
564#define SET_FLAG_FROM_TEST(value, test, flag) \
565 { \
566 if (test) { \
567 (value) |= (flag); \
568 } \
569 else { \
570 (value) &= ~(flag); \
571 } \
572 } \
573 ((void)0)
574
577/* -------------------------------------------------------------------- */
581#ifdef __cplusplus
582
583/* Useful to port C code using enums to C++ where enums are strongly typed.
584 * To use after the enum declaration. */
585/* If any enumerator `C` is set to say `A|B`, then `C` would be the max enum value. */
586# define ENUM_OPERATORS(_enum_type, _max_enum_value) \
587 extern "C++" { \
588 inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \
589 { \
590 return (_enum_type)(uint64_t(a) | uint64_t(b)); \
591 } \
592 inline constexpr _enum_type operator&(_enum_type a, _enum_type b) \
593 { \
594 return (_enum_type)(uint64_t(a) & uint64_t(b)); \
595 } \
596 inline constexpr _enum_type operator~(_enum_type a) \
597 { \
598 return (_enum_type)(~uint64_t(a) & (2 * uint64_t(_max_enum_value) - 1)); \
599 } \
600 inline _enum_type &operator|=(_enum_type &a, _enum_type b) \
601 { \
602 return a = (_enum_type)(uint64_t(a) | uint64_t(b)); \
603 } \
604 inline _enum_type &operator&=(_enum_type &a, _enum_type b) \
605 { \
606 return a = (_enum_type)(uint64_t(a) & uint64_t(b)); \
607 } \
608 inline _enum_type &operator^=(_enum_type &a, _enum_type b) \
609 { \
610 return a = (_enum_type)(uint64_t(a) ^ uint64_t(b)); \
611 } \
612 } /* extern "C++" */
613
614#else
615/* Output nothing. */
616# define ENUM_OPERATORS(_type, _max)
617#endif
618
621/* -------------------------------------------------------------------- */
626#define AT __FILE__ ":" STRINGIFY(__LINE__)
627
629#define EXPR_NOP(expr) (void)(0 ? ((void)(expr), 1) : 0)
630
638#define BLI_ENABLE_IF(condition) typename std::enable_if_t<(condition)> * = nullptr
639
640#if defined(_MSC_VER) && !defined(__clang__)
641# define BLI_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
642#elif defined(__has_cpp_attribute)
643# if __has_cpp_attribute(no_unique_address)
644# define BLI_NO_UNIQUE_ADDRESS [[no_unique_address]]
645# else
646# define BLI_NO_UNIQUE_ADDRESS
647# endif
648#else
649# define BLI_NO_UNIQUE_ADDRESS [[no_unique_address]]
650#endif
651
654#ifdef __cplusplus
655}
656
657namespace blender::blenlib_internal {
658
659/* A replacement for std::is_bounded_array_v until we go C++20. */
660template<class T> struct IsBoundedArray : std::false_type {};
661template<class T, std::size_t N> struct IsBoundedArray<T[N]> : std::true_type {};
662
663} // namespace blender::blenlib_internal
664
671template<class T, size_t N> constexpr size_t ARRAY_SIZE(const T (&arg)[N]) noexcept
672{
673 (void)arg;
674 return N;
675}
676
687template<class T>
688constexpr std::enable_if_t<blender::blenlib_internal::IsBoundedArray<T>::value, size_t>
689BOUNDED_ARRAY_TYPE_SIZE() noexcept
690{
691 return sizeof(std::declval<T>()) / sizeof(std::declval<T>()[0]);
692}
693
694#endif
695
696#endif /* __BLI_UTILDEFINES_H__ */
#define BLI_assert(a)
Definition BLI_assert.h:50
bool BLI_memory_is_zero(const void *arr, size_t arr_size)
#define ARRAY_SIZE(arr)
#define N
__int64 int64_t
Definition stdint.h:89