1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef IORAP_COMMON_MACROS_H_ 18 #define IORAP_COMMON_MACROS_H_ 19 20 // Expands to the # of arguments passed to the macro. 21 // For example `IORAP_PP_NARG(a,b,c)` -> 3 22 // For example `IORAP_PP_NARG(x)` -> 1 23 // 24 // The # of arguments must be >0 and <64. 25 #define IORAP_PP_NARG(...) \ 26 IORAP_PP_NARG_IMPL(__VA_ARGS__, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,) // NOLINT 27 28 // Implementation explanation: Equivalent of: 29 // lst = __VA_ARGS__ + 64..1 30 // return lst[64] 31 // The variadic arguments logically shift-off the hardcoded 64..1 list, revealing 32 // the # of arguments that were shifted. 33 #define IORAP_PP_NARG_IMPL(e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, e62, e63, N, ...) N 34 35 #define IORAP_PP_CONCAT(lhs, rhs) \ 36 IORAP_PP_CONCAT_IMPL(lhs, rhs) 37 38 #define IORAP_PP_CONCAT_IMPL(lhs, rhs) \ 39 lhs ## rhs 40 41 // Expands to call FN around every argument passed it. 42 // MAP(f, a1, a2, .., an) -> f(a1) f(a2) f(a3) ... f(an) 43 #define IORAP_PP_MAP(FN, ...) IORAP_PP_MAP_IMPL(IORAP_PP_NARG(__VA_ARGS__), /*sep=blank*/, FN, __VA_ARGS__) 44 // Expands to call FN around every argument passed it. 45 // Every non-first/non-last function call also has a separator prefixed before it. 46 // MAP_SEP(f, sep, a1, a2, .., an) -> f(a1) sep() f(a2) sep() f(a3) sep() ... sep() f(an-1) f(an) 47 #define IORAP_PP_MAP_SEP(FN, sep, ...) IORAP_PP_MAP_IMPL(IORAP_PP_NARG(__VA_ARGS__), FN, sep, __VA_ARGS__) 48 #define IORAP_PP_MAP_IMPL(N, FN, sep, ...) IORAP_PP_CONCAT(IORAP_PP_MAP_IMPL_, N)(FN, sep, __VA_ARGS__) 49 #define IORAP_PP_MAP_IMPL_1(FN, sep, a1) FN(a1) 50 #define IORAP_PP_MAP_IMPL_2(FN, sep, a1, a2) FN(a1) sep() FN(a2) 51 #define IORAP_PP_MAP_IMPL_3(FN, sep, a1, a2, a3) FN(a1) sep() FN(a2) sep() FN(a3) 52 #define IORAP_PP_MAP_IMPL_4(FN, sep, a1, a2, a3, a4) FN(a1) sep() FN(a2) sep() FN(a3) sep() FN(a4) 53 54 // Deferred comma. Useful with the above MAP function when you need a comma separator. 55 #define IORAP_PP_COMMA() , 56 // All arguments are ignored. 57 #define IORAP_PP_NOP(...) 58 59 #endif // IORAP_COMMON_MACROS_H_