1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #ifndef COMMON_MACRO_TRICK_H_ 19 #define COMMON_MACRO_TRICK_H_ 20 21 #pragma once 22 23 ///////////////// variadic macro //////////////////////// 24 25 // a little more complex version that works with GCC and visual studio 26 27 /// http://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion 28 #define COUNT_ARGS_IMPL2(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ 29 N, ...) \ 30 N 31 #define COUNT_ARGS_IMPL(args) COUNT_ARGS_IMPL2 args 32 #define COUNT_ARGS(...) \ 33 COUNT_ARGS_IMPL((__VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)) 34 35 #define MACRO_CHOOSE_HELPER2(base, count) base##count 36 #define MACRO_CHOOSE_HELPER1(base, count) MACRO_CHOOSE_HELPER2(base, count) 37 #define MACRO_CHOOSE_HELPER(base, count) MACRO_CHOOSE_HELPER1(base, count) 38 39 #define MACRO_GLUE(x, y) x y 40 #define VARARG(base, ...) MACRO_GLUE(MACRO_CHOOSE_HELPER(base, COUNT_ARGS(__VA_ARGS__)), (__VA_ARGS__)) 41 // usage 42 /* 43 * #define fun1(a) xxxx 44 * #define fun2(a, b) xxxx 45 * #define fun3(a, b, c) xxxx 46 * 47 * #define fun(...) VARARG(fun, __VA_ARGS__) 48 * 49 * int main(){ 50 * fun(1); // calls fun1(1) 51 * fun(1, 2); // calls fun2(1,2) 52 * fun(1, 2, 3); // calls fun3(1,2,3) 53 * } 54 * 55 */ 56 57 ///////////////// end of variadic macro ////////////////////// 58 59 #endif /* COMMON_MACRO_TRICK_H_ */ 60