1 /* 2 * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #pragma once 8 9 /* 10 This header contains various general purpose helper macros used across ESP-IDF 11 */ 12 #include <assert.h> 13 #include "esp_assert.h" 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 /** 20 * @brief Macro to select different versions of other macros based on whether VA_ARGS has an argument or no argument 21 * 22 * Some macros (such as in FreeRTOS) have two versions (one that accepts arguments and another that does not). The 23 * following "CHOOSE_MACRO_VA_ARG" selector allows automatic selection between two different versions of a macro. 24 * 25 * "CHOOSE_MACRO_VA_ARG" make use of the fact that "##__VA_ARGS__," will eliminate the trailing comma if there are no 26 * arguments, thus allows subsequent arguments in "CHOOSE_MACRO_VA_ARG" to be left shifted in the parameter list. 27 * 28 * Therefore, if we call the following: 29 * - CHOOSE_MACRO_VA_ARG(MACRO_ARGS, MACRO_NO_ARGS, ##__VA_ARGS__)(__VA_ARGS__) 30 * 31 * The result will be: 32 * - MACRO_ARGS(__VA_ARGS__) if __VA_ARGS__ was not empty 33 * - MACRO_NO_ARGS() if __VA_ARGS__ was empty 34 * 35 * @note In the future, we want to become compatible with clang. Hence, we provide two 36 * versions of the following macros which are using variadic arguments. One is using the GNU extension ##__VA_ARGS__. 37 * The other is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with standard C++20 38 * enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to using ##__VA_ARGS__. 39 */ 40 #if defined(__cplusplus) && (__cplusplus > 201703L) 41 #define CHOOSE_MACRO_VA_ARG_INN_IMPL(...) __VA_OPT__(0) 42 #define CHOOSE_MACRO_VA_ARG_INN(one, MACRO1, MACRO2, ...) MACRO1 43 #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) 44 #else 45 #define CHOOSE_MACRO_VA_ARG_INN(one, two, MACRO1, MACRO2, ...) MACRO1 46 #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) 47 #endif 48 49 /* Count number of arguments of __VA_ARGS__ 50 * - reference https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s 51 * - __GET_NTH_ARG__() takes args >= N (64) but only expand to Nth one (64th) 52 * - __RSEQ_N__() is reverse sequential to N to add padding to have Nth 53 * position is the same as the number of arguments 54 * - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma) 55 */ 56 #ifndef __VA_NARG__ 57 # define __VA_NARG__(...) __NARG__(_0, ##__VA_ARGS__, __RSEQ_N__()) 58 59 # define __NARG__(...) __GET_NTH_ARG__(__VA_ARGS__) 60 # define __GET_NTH_ARG__( \ 61 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \ 62 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \ 63 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \ 64 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ 65 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \ 66 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \ 67 _61,_62,_63,N,...) N 68 # define __RSEQ_N__() \ 69 62,61,60, \ 70 59,58,57,56,55,54,53,52,51,50, \ 71 49,48,47,46,45,44,43,42,41,40, \ 72 39,38,37,36,35,34,33,32,31,30, \ 73 29,28,27,26,25,24,23,22,21,20, \ 74 19,18,17,16,15,14,13,12,11,10, \ 75 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 76 #endif 77 78 #ifndef ESP_UNUSED 79 #define ESP_UNUSED(x) ((void)(x)) 80 #endif 81 82 /* test macros */ 83 #define foo_args(...) 1 84 #define foo_no_args() 2 85 #if defined(__cplusplus) && (__cplusplus > 201703L) 86 #define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args __VA_OPT__(,) __VA_ARGS__)(__VA_ARGS__) 87 #else 88 #define foo(...) CHOOSE_MACRO_VA_ARG(foo_args, foo_no_args, ##__VA_ARGS__)(__VA_ARGS__) 89 #endif 90 91 ESP_STATIC_ASSERT(foo() == 2, "CHOOSE_MACRO_VA_ARG() result does not match for 0 arguments"); 92 ESP_STATIC_ASSERT(foo(42) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for 1 argument"); 93 #if defined(__cplusplus) && (__cplusplus > 201703L) 94 ESP_STATIC_ASSERT(foo(42, 87) == 1, "CHOOSE_MACRO_VA_ARG() result does not match for n arguments"); 95 #endif 96 97 #undef foo 98 #undef foo_args 99 #undef foo_no_args 100 101 #ifdef __cplusplus 102 } 103 #endif 104