|
189 | 189 | } while (0) |
190 | 190 | #endif // __cplusplus && CONFIG_COMPILER_CXX_EXCEPTIONS |
191 | 191 |
|
| 192 | +/** |
| 193 | + * @brief Check if the value is within the range [min, max]; |
| 194 | + * |
| 195 | + * @param x Value to check |
| 196 | + * @param min Minimum acceptable value |
| 197 | + * @param max Maximum acceptable value |
| 198 | + */ |
| 199 | +#define ESP_UTILS_CHECK_VALUE(x, min, max) do { \ |
| 200 | + __typeof__(x) _x = (x); \ |
| 201 | + __typeof__(min) _min = (min); \ |
| 202 | + __typeof__(max) _max = (max); \ |
| 203 | + if ((_x < _min) || (_x > _max)) { \ |
| 204 | + } \ |
| 205 | + } while(0) |
| 206 | + |
192 | 207 | #else |
193 | 208 |
|
194 | 209 | #ifndef unlikely |
|
392 | 407 | } while (0) |
393 | 408 | #endif // __cplusplus && CONFIG_COMPILER_CXX_EXCEPTIONS |
394 | 409 |
|
| 410 | +/** |
| 411 | + * @brief Check if the value is within the range [min, max]; if not, log an error |
| 412 | + * |
| 413 | + * @param x Value to check |
| 414 | + * @param min Minimum acceptable value |
| 415 | + * @param max Maximum acceptable value |
| 416 | + */ |
| 417 | +#define ESP_UTILS_CHECK_VALUE(x, min, max) do { \ |
| 418 | + __typeof__(x) _x = (x); \ |
| 419 | + __typeof__(min) _min = (min); \ |
| 420 | + __typeof__(max) _max = (max); \ |
| 421 | + if ((_x < _min) || (_x > _max)) { \ |
| 422 | + ESP_UTILS_LOGE("Invalid value: %d, should be in range [%d, %d]", _x, _min, _max); \ |
| 423 | + } \ |
| 424 | + } while(0) |
| 425 | + |
395 | 426 | #elif ESP_UTILS_CONF_CHECK_HANDLE_METHOD == ESP_UTILS_CHECK_HANDLE_WITH_ASSERT |
396 | 427 |
|
397 | 428 | #define ESP_UTILS_CHECK_NULL_RETURN(x, ...) assert((x) != NULL) |
|
456 | 487 | } while (0) |
457 | 488 | #endif // __cplusplus && CONFIG_COMPILER_CXX_EXCEPTIONS |
458 | 489 |
|
| 490 | +#define ESP_UTILS_CHECK_VALUE(x, min, max) do { \ |
| 491 | + __typeof__(x) _x = (x); \ |
| 492 | + __typeof__(min) _min = (min); \ |
| 493 | + __typeof__(max) _max = (max); \ |
| 494 | + assert((_x >= _min) && (_x <= _max)); \ |
| 495 | + } while(0) |
| 496 | + |
459 | 497 | #endif // ESP_UTILS_CONF_CHECK_HANDLE_METHOD |
460 | 498 | #endif // ESP_UTILS_CONF_CHECK_HANDLE_METHOD |
461 | 499 |
|
| 500 | +/** |
| 501 | + * @brief Check if the value is within the range [min, max]; if not, log an error and return the specified value. |
| 502 | + * |
| 503 | + * @param x Value to check |
| 504 | + * @param min Minimum acceptable value |
| 505 | + * @param max Maximum acceptable value |
| 506 | + * @param ret Value to return if the value is out of range |
| 507 | + * @param fmt Format string for the error message |
| 508 | + * @param ... Additional arguments for the format string |
| 509 | + */ |
| 510 | +#define ESP_UTILS_CHECK_VALUE_RETURN(x, min, max, ret, fmt, ...) do { \ |
| 511 | + __typeof__(x) __x = (x); \ |
| 512 | + __typeof__(min) __min = (min); \ |
| 513 | + __typeof__(max) __max = (max); \ |
| 514 | + ESP_UTILS_CHECK_VALUE(__x, __min, __max); \ |
| 515 | + ESP_UTILS_CHECK_FALSE_RETURN((__x >= __min) && (__x <= __max), ret, fmt, ##__VA_ARGS__); \ |
| 516 | + } while(0) |
| 517 | + |
| 518 | +/** |
| 519 | + * @brief Check if the value is within the range [min, max]; if not, log an error and goto the specified label. |
| 520 | + * |
| 521 | + * @param x Value to check |
| 522 | + * @param min Minimum acceptable value |
| 523 | + * @param max Maximum acceptable value |
| 524 | + * @param goto_tag Label to jump to if the value is out of range |
| 525 | + * @param fmt Format string for the error message |
| 526 | + * @param ... Additional arguments for the format string |
| 527 | + */ |
| 528 | +#define ESP_UTILS_CHECK_VALUE_GOTO(x, min, max, goto_tag, fmt, ...) do { \ |
| 529 | + __typeof__(x) __x = (x); \ |
| 530 | + __typeof__(min) __min = (min); \ |
| 531 | + __typeof__(max) __max = (max); \ |
| 532 | + ESP_UTILS_CHECK_VALUE(__x, __min, __max); \ |
| 533 | + ESP_UTILS_CHECK_FALSE_GOTO((__x >= __min) && (__x <= __max), goto_tag, fmt, ##__VA_ARGS__); \ |
| 534 | + } while(0) |
| 535 | + |
| 536 | +/** |
| 537 | + * @brief Check if the value is within the range [min, max]; if not, log an error and return without a value. |
| 538 | + * |
| 539 | + * @param x Value to check |
| 540 | + * @param min Minimum acceptable value |
| 541 | + * @param max Maximum acceptable value |
| 542 | + * @param fmt Format string for the error message |
| 543 | + * @param ... Additional arguments for the format string |
| 544 | + */ |
| 545 | +#define ESP_UTILS_CHECK_VALUE_EXIT(x, min, max, fmt, ...) do { \ |
| 546 | + __typeof__(x) __x = (x); \ |
| 547 | + __typeof__(min) __min = (min); \ |
| 548 | + __typeof__(max) __max = (max); \ |
| 549 | + ESP_UTILS_CHECK_VALUE(__x, __min, __max); \ |
| 550 | + ESP_UTILS_CHECK_FALSE_EXIT((__x >= __min) && (__x <= __max), fmt, ##__VA_ARGS__); \ |
| 551 | + } while(0) |
| 552 | + |
462 | 553 | #ifdef __cplusplus |
463 | 554 | #ifndef ESP_UTILS_CHECK_EXCEPTION_RETURN |
464 | 555 | #define ESP_UTILS_CHECK_EXCEPTION_RETURN(x, ret, fmt, ...) ((void)(x)) |
|
0 commit comments