1 /* 2 * Copyright (C) 2012 William Swanson 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Except as contained in this notice, the names of the authors or 24 * their institutions shall not be used in advertising or otherwise to 25 * promote the sale, use or other dealings in this Software without 26 * prior written authorization from the authors. 27 */ 28 29 /* 30 * Implements the higher-order Map function in the C Pre-Processor. 31 * 32 * Explanations available in [1,2,3]. 33 * Original source from [1]. 34 * 35 * [1] https://github.com/swansontec/map-macro SHA:383c38e9 36 * [2] http://jhnet.co.uk/articles/cpp_magic#turning-recursion-into-an-iterator 37 * [3] https://en.wikipedia.org/wiki/Map_(higher-order_function) 38 */ 39 40 #ifndef EXT_DEBUG_CTF_CTF_MAP_H 41 #define EXT_DEBUG_CTF_CTF_MAP_H 42 43 #define EVAL0(...) __VA_ARGS__ 44 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__))) 45 #define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__))) 46 #define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__))) 47 #define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__))) 48 #define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__))) 49 50 #define MAP_END(...) 51 #define MAP_OUT 52 #define MAP_COMMA , 53 54 #define MAP_GET_END2() 0, MAP_END 55 #define MAP_GET_END1(...) MAP_GET_END2 56 #define MAP_GET_END(...) MAP_GET_END1 57 #define MAP_NEXT0(test, next, ...) next MAP_OUT 58 #define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0) 59 #define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next) 60 61 #define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__) 62 #define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__) 63 64 #define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0) 65 #define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next) 66 67 #define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__) 68 #define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__) 69 70 /** 71 * Applies the function macro `f` to each of the remaining parameters. 72 */ 73 #define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) 74 75 /** 76 * Applies the function macro `f` to each of the remaining parameters and 77 * inserts commas between the results. 78 */ 79 #define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) 80 81 #endif /* EXT_DEBUG_CTF_CTF_MAP_H */ 82