cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
Utility Math & Helpers

Quality-of-life macros for strings, pointers, and math. More...

Macros

#define STRINGIFY_DETAIL(x)
 Internal implementation of STRINGIFY. Do not use directly.
#define STRINGIFY(x)
 Safely converts a macro argument into a string literal.
#define GLUE_DETAIL(x, y)
 Internal implementation of GLUE. Do not use directly.
#define GLUE(x, y)
 Safely concatenates two tokens together into a single token.
#define INT_FROM_PTR(ptr)
 Safely casts a pointer to an integer of pointer size.
#define PTR_FROM_INT(type, val)
 Safely casts a pointer-sized integer back to a specific pointer type.
#define ARRAY_SIZE(arr)
 Calculates the number of elements in a statically allocated array.
#define MIN(a, b)
 Returns the minimum of two values (Standard C fallback).
#define MAX(a, b)
 Returns the maximum of two values (Standard C fallback).
#define CLAMP(val, min, max)
 Clamps a value between a minimum and a maximum (Standard C fallback).
#define SWAP(type, a, b)
 Generic variable swap.
#define Boolify(x)
 Safely converts any scalar value to a strict 1 or 0 boolean.
#define AsciiID4(a, b, c, d)
 Packs four 8-bit ASCII characters into a single 32-bit integer (FourCC).
#define ExpandAsciiID(x)
 Expands a 32-bit FourCC integer for use in a printf format string (e.g. "%.*s").

Detailed Description

Quality-of-life macros for strings, pointers, and math.

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE ( arr)
Value:
(sizeof(arr) / sizeof((arr)[0]))

Calculates the number of elements in a statically allocated array.

Warning
Do not pass a decayed pointer to this macro. It only works on real arrays.
Parameters
arrThe static array.
Returns
The element count as a size_t.

Definition at line 252 of file base_macros.h.

◆ AsciiID4

#define AsciiID4 ( a,
b,
c,
d )
Value:
(((d) << 24) | ((c) << 16) | ((b) << 8) | (a))

Packs four 8-bit ASCII characters into a single 32-bit integer (FourCC).

Parameters
a1st character.
b2nd character.
c3rd character.
d4th character.

Definition at line 330 of file base_macros.h.

◆ Boolify

#define Boolify ( x)
Value:
((x) != 0)

Safely converts any scalar value to a strict 1 or 0 boolean.

Parameters
xThe value to evaluate.

Definition at line 321 of file base_macros.h.

◆ CLAMP

#define CLAMP ( val,
min,
max )
Value:
(((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val))

Clamps a value between a minimum and a maximum (Standard C fallback).

Definition at line 301 of file base_macros.h.

◆ ExpandAsciiID

#define ExpandAsciiID ( x)
Value:
(int)(sizeof(x)), (char *)(&(x))

Expands a 32-bit FourCC integer for use in a printf format string (e.g. "%.*s").

Parameters
xThe integer to expand.

Definition at line 336 of file base_macros.h.

◆ GLUE

#define GLUE ( x,
y )
Value:
#define GLUE_DETAIL(x, y)
Internal implementation of GLUE. Do not use directly.

Safely concatenates two tokens together into a single token.

Parameters
xThe first token.
yThe second token.

Definition at line 229 of file base_macros.h.

◆ GLUE_DETAIL

#define GLUE_DETAIL ( x,
y )
Value:
x##y

Internal implementation of GLUE. Do not use directly.

Definition at line 223 of file base_macros.h.

◆ INT_FROM_PTR

#define INT_FROM_PTR ( ptr)
Value:
((uintptr_t)(ptr))

Safely casts a pointer to an integer of pointer size.

Parameters
ptrThe pointer to cast.
Returns
An unsigned integer representing the memory address.

Definition at line 236 of file base_macros.h.

◆ MAX

#define MAX ( a,
b )
Value:
(((a) > (b)) ? (a) : (b))

Returns the maximum of two values (Standard C fallback).

Definition at line 299 of file base_macros.h.

◆ MIN

#define MIN ( a,
b )
Value:
(((a) < (b)) ? (a) : (b))

Returns the minimum of two values (Standard C fallback).

Definition at line 297 of file base_macros.h.

◆ PTR_FROM_INT

#define PTR_FROM_INT ( type,
val )
Value:
((type *)(uintptr_t)(val))

Safely casts a pointer-sized integer back to a specific pointer type.

Parameters
typeThe type of pointer to return (e.g., int, struct Node).
valThe integer memory address.
Returns
A casted pointer.

Definition at line 244 of file base_macros.h.

◆ STRINGIFY

#define STRINGIFY ( x)
Value:
#define STRINGIFY_DETAIL(x)
Internal implementation of STRINGIFY. Do not use directly.

Safely converts a macro argument into a string literal.

Parameters
xThe token to stringify. If it is a macro, it will be expanded first.
Returns
A string literal of the evaluated token.

Definition at line 220 of file base_macros.h.

◆ STRINGIFY_DETAIL

#define STRINGIFY_DETAIL ( x)
Value:
#x

Internal implementation of STRINGIFY. Do not use directly.

Definition at line 214 of file base_macros.h.

◆ SWAP

#define SWAP ( type,
a,
b )
Value:
do { \
type _tmp = (a); \
(a) = (b); \
(b) = _tmp; \
} while (0)

Generic variable swap.

Parameters
typeThe data type of the variables being swapped (e.g., int, float).
aThe first variable.
bThe second variable.

Definition at line 310 of file base_macros.h.