ic-macros  0.1.5
enum_cast.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define IC_CAST_ENUM(type, min, max, expr)
 Compile-time constant safe cast from an integer type to an enum. More...
 
#define IC_Z_CAST_ENUM(type, min, max, expr)
 Safe cast from from an integer type to an enum. More...
 

Macro Definition Documentation

◆ IC_CAST_ENUM

#define IC_CAST_ENUM (   type,
  min,
  max,
  expr 
)
Value:
({ \
IC_BUILD_ASSERT(__typeof__(expr) == type || IC_IS_INT(expr) || IC_IS_UINT(expr)); \
IC_CAST_NUMBER(type, min, max, expr); \
})
#define IC_IS_INT(expr)
Compile-time check if expr is a signed integer.
Definition: assert_type.h:117
#define IC_IS_UINT(expr)
Compile-time constant check if expr is an unsigned integer.
Definition: assert_type.h:67

Compile-time constant safe cast from an integer type to an enum.

Note
Use IC_Z_CAST_ENUM for a runtime variant of this macro.
Parameters
typeThe enum type to cast to.
minThe minimum value of type, e.g. 0 or MY_ENUM_MIN.
maxThe maximum value of type, e.g. MY_ENUM_MAX.
exprThe expression to cast.
Returns
The expr casted to type: (type) (expr)

Definition at line 21 of file enum_cast.h.

◆ IC_Z_CAST_ENUM

#define IC_Z_CAST_ENUM (   type,
  min,
  max,
  expr 
)
Value:
({ \
__auto_type const _IC_Z_CAST_NUMBER_expr_ = IC_REQUIRE_NOT_CONSTANT(expr); \
IC_BUILD_ASSERT(!IC_IS_FP(_IC_Z_CAST_NUMBER_expr_)); \
IC_ASSERT(IC_IN_RANGE(_IC_Z_CAST_NUMBER_expr_, min, max)); \
(type) _IC_Z_CAST_NUMBER_expr_; \
})
#define IC_REQUIRE_NOT_CONSTANT(expr)
Require that the expression is not a compile-time constant before returning it unchanged.
#define IC_IS_FP(expr)
Compile-time constant check if expr is a floating point number.
Definition: assert_type.h:143
#define IC_IN_RANGE(val, min, max)
Checks if a value is within range.
Definition: util.h:563

Safe cast from from an integer type to an enum.

Note
The expression will be evaluated once.
Parameters
typeThe enum type to cast to.
minThe minimum value of type, e.g. 0 or MY_ENUM_MIN.
maxThe maximum value of type, e.g. MY_ENUM_MAX.
exprThe expression to cast.
Returns
The expr casted to type: (type) (expr)

Definition at line 36 of file enum_cast.h.