esp_macros.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Macro to select different versions of other macros based on whether VA_ARGS has an argument or no argument
  15. *
  16. * Some macros (such as in FreeRTOS) have two versions (one that accepts arguments and another that does not). The
  17. * following "CHOOSE_MACRO_VA_ARG" selector allows automatic selection between two different versions of a macro.
  18. *
  19. * "CHOOSE_MACRO_VA_ARG" make use of the fact that "##__VA_ARGS__," will eliminate the trailing comma if there are no
  20. * arguments, thus allows subsequent arguments in "CHOOSE_MACRO_VA_ARG" to be left shifted in the parameter list.
  21. *
  22. * Therefore, if we call the following:
  23. * - CHOOSE_MACRO_VA_ARG(MACRO_ARGS, MACRO_NO_ARGS, ##__VA_ARGS__)(__VA_ARGS__)
  24. *
  25. * The result will be:
  26. * - MACRO_ARGS(__VA_ARGS__) if __VA_ARGS__ was not empty
  27. * - MACRO_NO_ARGS() if __VA_ARGS__ was empty
  28. *
  29. * @note In the future, we want to switch to C++20. We also want to become compatible with clang. Hence, we provide two
  30. * versions of the following macros which are using variadic arguments. One is using the GNU extension ##__VA_ARGS__.
  31. * The other is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with standard C++20
  32. * enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to using ##__VA_ARGS__.
  33. */
  34. #if defined(__cplusplus) && (__cplusplus > 201703L)
  35. #define CHOOSE_MACRO_VA_ARG_INN_IMPL(...) __VA_OPT__(0)
  36. #define CHOOSE_MACRO_VA_ARG_INN(one, MACRO1, MACRO2, ...) MACRO1
  37. #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)
  38. #else
  39. #define CHOOSE_MACRO_VA_ARG_INN(one, two, MACRO1, MACRO2, ...) MACRO1
  40. #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)
  41. #endif
  42. /* test macros */
  43. #define foo_args(...) 1
  44. #define foo_no_args() 2
  45. #if defined(__cplusplus) && (__cplusplus > 201703L)
  46. #define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__)
  47. #else
  48. #define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args, ##__VA_ARGS__)(__VA_ARGS__)
  49. #endif
  50. #if defined(__cplusplus)
  51. #define MY_STATIC_ASSERT static_assert
  52. #else
  53. #define MY_STATIC_ASSERT _Static_assert
  54. #endif
  55. MY_STATIC_ASSERT(foo() == 2, "CHOOSE_MACRO_VA_ARG() result does not match for 0 arguments");
  56. MY_STATIC_ASSERT(foo(42) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for 1 argument");
  57. #if defined(__cplusplus) && (__cplusplus > 201703L)
  58. static_assert(foo(42, 87) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for n arguments");
  59. #endif
  60. #undef MY_STATIC_ASSERT
  61. #undef foo
  62. #undef foo_args
  63. #undef foo_no_args
  64. #ifdef __cplusplus
  65. }
  66. #endif