EzC
ezc_macro.h
Go to the documentation of this file.
1 /* ezc_macro.h
2  *
3  * Copyright (c) 2018 Kirk Lange <github.com/kirklange>
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  ******************************************************************************
22  *
23  * Copyright (C) 2012 William Swanson <github.com/swansontec/map-macro>
24  *
25  * Permission is hereby granted, free of charge, to any person
26  * obtaining a copy of this software and associated documentation
27  * files (the "Software"), to deal in the Software without
28  * restriction, including without limitation the rights to use, copy,
29  * modify, merge, publish, distribute, sublicense, and/or sell copies
30  * of the Software, and to permit persons to whom the Software is
31  * furnished to do so, subject to the following conditions:
32  *
33  * The above copyright notice and this permission notice shall be
34  * included in all copies or substantial portions of the Software.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
40  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
41  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
42  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43  *
44  * Except as contained in this notice, the names of the authors or
45  * their institutions shall not be used in advertising or otherwise to
46  * promote the sale, use or other dealings in this Software without
47  * prior written authorization from the authors.
48  */
49 
50 #ifndef EZC_MACRO_H
51 #define EZC_MACRO_H
52 
62 #define SST_EVAL0(...) __VA_ARGS__
63 #define SST_EVAL1(...) SST_EVAL0(SST_EVAL0(SST_EVAL0(__VA_ARGS__)))
64 #define SST_EVAL2(...) SST_EVAL1(SST_EVAL1(SST_EVAL1(__VA_ARGS__)))
65 #define SST_EVAL3(...) SST_EVAL2(SST_EVAL2(SST_EVAL2(__VA_ARGS__)))
66 #define SST_EVAL4(...) SST_EVAL3(SST_EVAL3(SST_EVAL3(__VA_ARGS__)))
67 #define SST_EVAL(...) SST_EVAL4(SST_EVAL4(SST_EVAL4(__VA_ARGS__)))
68 
69 #define SST_MAP_END(...)
70 #define SST_MAP_OUT
71 #define SST_MAP_COMMA ,
72 
73 #define SST_MAP_GET_END2() 0, SST_MAP_END
74 #define SST_MAP_GET_END1(...) SST_MAP_GET_END2
75 #define SST_MAP_GET_END(...) SST_MAP_GET_END1
76 #define SST_MAP_NEXT0(test, next, ...) next SST_MAP_OUT
77 #define SST_MAP_NEXT1(test, next) SST_MAP_NEXT0(test, next, 0)
78 #define SST_MAP_NEXT(test, next) SST_MAP_NEXT1(SST_MAP_GET_END test, next)
79 
80 #define SST_MAP0(f, x, peek, ...) \
81  f(x) SST_MAP_NEXT(peek, SST_MAP1)(f, peek, __VA_ARGS__)
82 #define SST_MAP1(f, x, peek, ...) \
83  f(x) SST_MAP_NEXT(peek, SST_MAP0)(f, peek, __VA_ARGS__)
84 
85 #define SST_MAP_LIST_NEXT1(test, next) \
86  SST_MAP_NEXT0(test, SST_MAP_COMMA next, 0)
87 #define SST_MAP_LIST_NEXT(test, next) \
88  SST_MAP_LIST_NEXT1(SST_MAP_GET_END test, next)
89 
90 #define SST_MAP_LIST0(f, x, peek, ...) \
91  f(x) SST_MAP_LIST_NEXT(peek, SST_MAP_LIST1)(f, peek, __VA_ARGS__)
92 #define SST_MAP_LIST1(f, x, peek, ...) \
93  f(x) SST_MAP_LIST_NEXT(peek, SST_MAP_LIST0)(f, peek, __VA_ARGS__)
94 
95 
96 
103 #define SST_MAP(f, ...) \
104  SST_EVAL(SST_MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
105 
106 
107 
116 #define SST_MAP_LIST(f, ...) \
117  SST_EVAL(SST_MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
118 
119 
120 
121 #define EZC_TO_ZERO(ptr) ((ptr) = 0)
122 
123 #define EZC_DO_FREE(ptr) (free((ptr)))
124 
125 
126 
127 #endif /* EZC_MACRO_H */