libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_macro_sequences.h
Go to the documentation of this file.
1/*! \file
2 * \brief Macro primitives for metaprogramming
3 * \ingroup preprocessor
4 * \sa gimbal_macro_composition.h
5 *
6 * \author William Swanson
7 * \author Falco Girgis
8 */
9
10#ifndef GIMBAL_MACRO_SEQUENCES_H
11#define GIMBAL_MACRO_SEQUENCES_H
12
13/*
14 * Copyright (C) 2012 William Swanson
15 *
16 * Permission is hereby granted, free of charge, to any person
17 * obtaining a copy of this software and associated documentation
18 * files (the "Software"), to deal in the Software without
19 * restriction, including without limitation the rights to use, copy,
20 * modify, merge, publish, distribute, sublicense, and/or sell copies
21 * of the Software, and to permit persons to whom the Software is
22 * furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
31 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 * Except as contained in this notice, the names of the authors or
36 * their institutions shall not be used in advertising or otherwise to
37 * promote the sale, use or other dealings in this Software without
38 * prior written authorization from the authors.
39 */
40
41#define GBL_EVAL0(...) __VA_ARGS__
42#define GBL_EVAL1(...) GBL_EVAL0(GBL_EVAL0(GBL_EVAL0(__VA_ARGS__)))
43#define GBL_EVAL2(...) GBL_EVAL1(GBL_EVAL1(GBL_EVAL1(__VA_ARGS__)))
44#define GBL_EVAL3(...) GBL_EVAL2(GBL_EVAL2(GBL_EVAL2(__VA_ARGS__)))
45#define GBL_EVAL4(...) GBL_EVAL3(GBL_EVAL3(GBL_EVAL3(__VA_ARGS__)))
46#define GBL_EVAL(...) GBL_EVAL4(GBL_EVAL4(GBL_EVAL4(__VA_ARGS__)))
47
48#define GBL_MAP_END(...)
49#define GBL_MAP_OUT
50#define GBL_MAP_COMMA ,
51
52#define GBL_MAP_GET_END2() 0, GBL_MAP_END
53#define GBL_MAP_GET_END1(...) GBL_MAP_GET_END2
54#define GBL_MAP_GET_END(...) GBL_MAP_GET_END1
55#define GBL_MAP_NEXT0(test, next, ...) next GBL_MAP_OUT
56#define GBL_MAP_NEXT1(test, next) GBL_MAP_NEXT0(test, next, 0)
57#define GBL_MAP_NEXT(test, next) GBL_MAP_NEXT1(GBL_MAP_GET_END test, next)
58
59#define GBL_MAP0(f, x, peek, ...) f(x) GBL_MAP_NEXT(peek, GBL_MAP1)(f, peek, __VA_ARGS__)
60#define GBL_MAP1(f, x, peek, ...) f(x) GBL_MAP_NEXT(peek, GBL_MAP0)(f, peek, __VA_ARGS__)
61
62/**
63 * Applies the function macro `f` to each of the remaining parameters.
64 */
65#define GBL_MAP(f, ...) GBL_EVAL(GBL_MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
66
67
68/**
69 * Applies the function macro `f` to each of the remaining parameters and
70 * inserts commas between the results.
71 */
72
73#define GBL_MAP_LIST_NEXT1(test, next) GBL_MAP_NEXT0(test, GBL_MAP_COMMA next, 0)
74#define GBL_MAP_LIST_NEXT(test, next) GBL_MAP_LIST_NEXT1(GBL_MAP_GET_END test, next)
75
76#define GBL_MAP_LIST0(f, x, peek, ...) f(x) GBL_MAP_LIST_NEXT(peek, GBL_MAP_LIST1)(f, peek, __VA_ARGS__)
77#define GBL_MAP_LIST1(f, x, peek, ...) f(x) GBL_MAP_LIST_NEXT(peek, GBL_MAP_LIST0)(f, peek, __VA_ARGS__)
78#define GBL_MAP_LIST(f, ...) GBL_EVAL(GBL_MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
79
80#define GBL_MAP_TUPLES0(f, x, peek, ...) f x GBL_MAP_NEXT(peek, GBL_MAP_TUPLES1)(f, peek, __VA_ARGS__)
81#define GBL_MAP_TUPLES1(f, x, peek, ...) f x GBL_MAP_NEXT(peek, GBL_MAP_TUPLES0)(f, peek, __VA_ARGS__)
82#define GBL_MAP_TUPLES(f, ...) GBL_EVAL(GBL_MAP_TUPLES1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
83
84#endif // GIMBAL_MACRO_SEQUENCES_H
#define GBL_MAP_TUPLES0(f, x, peek,...)
#define GBL_MAP_LIST0(f, x, peek,...)
#define GBL_MAP1(f, x, peek,...)
#define GBL_EVAL3(...)
#define GBL_MAP_GET_END2()
#define GBL_MAP_GET_END1(...)
#define GBL_MAP0(f, x, peek,...)
#define GBL_MAP_LIST_NEXT1(test, next)
Applies the function macro f to each of the remaining parameters and inserts commas between the resul...
#define GBL_MAP_GET_END(...)
#define GBL_MAP_END(...)
#define GBL_MAP_OUT
#define GBL_MAP_NEXT(test, next)
#define GBL_EVAL2(...)
#define GBL_MAP_TUPLES1(f, x, peek,...)
#define GBL_MAP_NEXT0(test, next,...)
#define GBL_EVAL0(...)
#define GBL_EVAL4(...)
#define GBL_MAP_LIST_NEXT(test, next)
#define GBL_EVAL1(...)
#define GBL_MAP_COMMA
#define GBL_MAP_LIST1(f, x, peek,...)
#define GBL_EVAL(...)
#define GBL_MAP_NEXT1(test, next)