esp_macros.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. /*
  8. This header contains various general purpose helper macros used across ESP-IDF
  9. */
  10. #include <assert.h>
  11. #include "esp_assert.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * @brief Macro to select different versions of other macros based on whether VA_ARGS has an argument or no argument
  17. *
  18. * Some macros (such as in FreeRTOS) have two versions (one that accepts arguments and another that does not). The
  19. * following "CHOOSE_MACRO_VA_ARG" selector allows automatic selection between two different versions of a macro.
  20. *
  21. * "CHOOSE_MACRO_VA_ARG" make use of the fact that "##__VA_ARGS__," will eliminate the trailing comma if there are no
  22. * arguments, thus allows subsequent arguments in "CHOOSE_MACRO_VA_ARG" to be left shifted in the parameter list.
  23. *
  24. * Therefore, if we call the following:
  25. * - CHOOSE_MACRO_VA_ARG(MACRO_ARGS, MACRO_NO_ARGS, ##__VA_ARGS__)(__VA_ARGS__)
  26. *
  27. * The result will be:
  28. * - MACRO_ARGS(__VA_ARGS__) if __VA_ARGS__ was not empty
  29. * - MACRO_NO_ARGS() if __VA_ARGS__ was empty
  30. *
  31. * @note In the future, we want to switch to C++20. We also want to become compatible with clang. Hence, we provide two
  32. * versions of the following macros which are using variadic arguments. One is using the GNU extension ##__VA_ARGS__.
  33. * The other is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with standard C++20
  34. * enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to using ##__VA_ARGS__.
  35. */
  36. #if defined(__cplusplus) && (__cplusplus > 201703L)
  37. #define CHOOSE_MACRO_VA_ARG_INN_IMPL(...) __VA_OPT__(0)
  38. #define CHOOSE_MACRO_VA_ARG_INN(one, MACRO1, MACRO2, ...) MACRO1
  39. #define CHOOSE_MACRO_VA_ARG(MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, ...) CHOOSE_MACRO_VA_ARG_INN(CHOOSE_MACRO_VA_ARG_INN_IMPL(__VA_ARGS__) __VA_OPT__(,) MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, 0)
  40. #else
  41. #define CHOOSE_MACRO_VA_ARG_INN(one, two, MACRO1, MACRO2, ...) MACRO1
  42. #define CHOOSE_MACRO_VA_ARG(MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, ...) CHOOSE_MACRO_VA_ARG_INN(0, ##__VA_ARGS__, MACRO_WITH_ARGS, MACRO_WITH_NO_ARGS, 0)
  43. #endif
  44. /* test macros */
  45. #define foo_args(...) 1
  46. #define foo_no_args() 2
  47. #if defined(__cplusplus) && (__cplusplus > 201703L)
  48. #define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
  49. #else
  50. #define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args, ##__VA_ARGS__)(__VA_ARGS__)
  51. #endif
  52. ESP_STATIC_ASSERT(foo() == 2, "CHOOSE_MACRO_VA_ARG() result does not match for 0 arguments");
  53. ESP_STATIC_ASSERT(foo(42) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for 1 argument");
  54. #if defined(__cplusplus) && (__cplusplus > 201703L)
  55. ESP_STATIC_ASSERT(foo(42, 87) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for n arguments");
  56. #endif
  57. #undef foo
  58. #undef foo_args
  59. #undef foo_no_args
  60. #ifdef __cplusplus
  61. }
  62. #endif