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