Loading...
Searching...
No Matches
forEach.hpp
Go to the documentation of this file.
1
5/*
6 * Copyright (C) 2012 William Swanson
7 *
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy,
12 * modify, merge, publish, distribute, sublicense, and/or sell copies
13 * of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Except as contained in this notice, the names of the authors or
28 * their institutions shall not be used in advertising or otherwise to
29 * promote the sale, use or other dealings in this Software without
30 * prior written authorization from the authors.
31 */
32
33// modification: kniz
34
35#pragma once
36
39
40#define _BY_EACH_GET_END2() 0, BY_CONSUME_ARGS
41#define _BY_EACH_GET_END1(...) _BY_EACH_GET_END2
42#define _BY_EACH_GET_END(...) _BY_EACH_GET_END1
43#define _BY_EACH_NEXT0(test, next, ...) next BY_VOID()
44#define _BY_EACH_NEXT1(test, next) _BY_EACH_NEXT0(test, next, 0)
45#define _BY_EACH_NEXT(test, next) _BY_EACH_NEXT1(_BY_EACH_GET_END test, next)
46
47#define _BY_EACH_LIST_NEXT1(test, next) _BY_EACH_NEXT0(test, BY_COMMA next, 0)
48#define _BY_EACH_LIST_NEXT(test, next) _BY_EACH_LIST_NEXT1(_BY_EACH_GET_END test, next)
49
50// Applies the function macro `f` to each of the remaining parameters.
51#define _BY_EACH0(f, x, peek, ...) f(x) _BY_EACH_NEXT(peek, _BY_EACH1)(f, peek, __VA_ARGS__)
52#define _BY_EACH1(f, x, peek, ...) f(x) _BY_EACH_NEXT(peek, _BY_EACH0)(f, peek, __VA_ARGS__)
53#define BY_EACH(f, ...) BY_EVAL(_BY_EACH1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
54
55// EACH macro for various parametered function:
56// usage:
57// #define X(x, y) cout << (x+y);
58// BY_EACH_TUPLE(X, (1,2), (2,3)) // please be careful to wrap with a paranthesis each
59// set of parameters.
60//
61// output:
62// 37
63#define _BY_EACH_TUPLE0(f, x, peek, ...) f x _BY_EACH_NEXT(peek, _BY_EACH_TUPLE1)(f, peek, __VA_ARGS__)
64#define _BY_EACH_TUPLE1(f, x, peek, ...) f x _BY_EACH_NEXT(peek, _BY_EACH_TUPLE0)(f, peek, __VA_ARGS__)
65#define BY_EACH_TUPLE(f, ...) BY_EVAL(_BY_EACH_TUPLE1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
66
67// EACH macro for expanding:
68// usage:
69// #define X(x, y) cout << (x+y);
70// int b = 5;
71// BY_EACH_EXPAND(X, b, 1,2,3) // ==> cout << (b+1); cout << (b+2); cout << (b+3);
72//
73// output:
74// 678
75#define _BY_EACH_EXPAND0(f, s, x, peek, ...) f(s, x) _BY_EACH_NEXT(peek, _BY_EACH_EXPAND1)(f, s, peek, __VA_ARGS__)
76#define _BY_EACH_EXPAND1(f, s, x, peek, ...) f(s, x) _BY_EACH_NEXT(peek, _BY_EACH_EXPAND0)(f, s, peek, __VA_ARGS__)
77#define BY_EACH_EXPAND(f, ...) BY_EVAL(_BY_EACH_EXPAND1(f, s, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
78
79// Applies the function macro `f` to each of the remaining parameters and inserts commas between
80// the results.
81#define _BY_EACH_LIST0(f, x, peek, ...) f(x) _BY_EACH_LIST_NEXT(peek, _BY_EACH_LIST1)(f, peek, __VA_ARGS__)
82#define _BY_EACH_LIST1(f, x, peek, ...) f(x) _BY_EACH_LIST_NEXT(peek, _BY_EACH_LIST0)(f, peek, __VA_ARGS__)
83#define BY_EACH_LIST(f, ...) BY_EVAL(_BY_EACH_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))