ic-macros  0.1.5
enum_cast.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, Intercreate, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
8 #include "ic_macros/ic_assert.h"
10 #include "zephyr/util.h"
11 
21 #define IC_CAST_ENUM(type, min, max, expr) \
22  ({ \
23  IC_BUILD_ASSERT(__typeof__(expr) == type || IC_IS_INT(expr) || IC_IS_UINT(expr)); \
24  IC_CAST_NUMBER(type, min, max, expr); \
25  })
26 
36 #define IC_Z_CAST_ENUM(type, min, max, expr) \
37  ({ \
38  __auto_type const _IC_Z_CAST_NUMBER_expr_ = IC_REQUIRE_NOT_CONSTANT(expr); \
39  IC_BUILD_ASSERT(!IC_IS_FP(_IC_Z_CAST_NUMBER_expr_)); \
40  IC_ASSERT(IC_IN_RANGE(_IC_Z_CAST_NUMBER_expr_, min, max)); \
41  (type) _IC_Z_CAST_NUMBER_expr_; \
42  })