Bug 1732032 [wpt PR 30868] - fix: canShare() return false if not allowed to use,...
[gecko.git] / mfbt / MacroForEach.h
blobc3067e3620048e7ef7626f3778aeeaac280dffae
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Implements a higher-order macro for iteratively calling another macro with
9 * fixed leading arguments, plus a trailing element picked from a second list
10 * of arguments.
13 #ifndef mozilla_MacroForEach_h
14 #define mozilla_MacroForEach_h
16 #include "mozilla/MacroArgs.h"
19 * MOZ_FOR_EACH(aMacro, aFixedArgs, aArgs) expands to N calls to the macro
20 * |aMacro| where N is equal the number of items in the list |aArgs|. The
21 * arguments for each |aMacro| call are composed of *all* arguments in the list
22 * |aFixedArgs| as well as a single argument in the list |aArgs|. For example:
24 * #define MACRO_A(x) x +
25 * int a = MOZ_FOR_EACH(MACRO_A, (), (1, 2, 3)) 0;
26 * // Expands to: MACRO_A(1) MACRO_A(2) MACRO_A(3) 0;
27 * // And further to: 1 + 2 + 3 + 0;
29 * #define MACRO_B(k, x) (k + x) +
30 * int b = MOZ_FOR_EACH(MACRO_B, (5,), (1, 2)) 0;
31 * // Expands to: MACRO_B(5, 1) MACRO_B(5, 2) 0;
33 * #define MACRO_C(k1, k2, x) (k1 + k2 + x) +
34 * int c = MOZ_FOR_EACH(MACRO_C, (5, 8,), (1, 2)) 0;
35 * // Expands to: MACRO_B(5, 8, 1) MACRO_B(5, 8, 2) 0;
37 * MOZ_FOR_EACH_SEPARATED(aMacro, aSeparator, aFixedArgs, aArgs) is identical
38 * to MOZ_FOR_EACH except that it inserts |aSeparator| between each call to
39 * the macro. |aSeparator| must be wrapped by parens. For example:
41 * #define MACRO_A(x) x
42 * int a = MOZ_FOR_EACH_SEPARATED(MACRO_A, (+), (), (1, 2, 3));
43 * // Expands to: MACRO_A(1) + MACRO_A(2) + MACRO_A(3);
44 * // And further to: 1 + 2 + 3
46 * #define MACRO_B(t, n) t n
47 * void test(MOZ_FOR_EACH_SEPARATED(MACRO_B, (,), (int,), (a, b)));
48 * // Expands to: void test(MACRO_B(int, a) , MACRO_B(int, b));
49 * // And further to: void test(int a , int b);
51 * If the |aFixedArgs| list is not empty, a trailing comma must be included.
53 * The |aArgs| list may be up to 50 items long.
55 #define MOZ_FOR_EACH_EXPAND_HELPER(...) __VA_ARGS__
56 #define MOZ_FOR_EACH_GLUE(a, b) a b
57 #define MOZ_FOR_EACH_SEPARATED(aMacro, aSeparator, aFixedArgs, aArgs) \
58 MOZ_FOR_EACH_GLUE(MOZ_PASTE_PREFIX_AND_ARG_COUNT( \
59 MOZ_FOR_EACH_, MOZ_FOR_EACH_EXPAND_HELPER aArgs), \
60 (aMacro, aSeparator, aFixedArgs, aArgs))
61 #define MOZ_FOR_EACH(aMacro, aFixedArgs, aArgs) \
62 MOZ_FOR_EACH_SEPARATED(aMacro, (), aFixedArgs, aArgs)
64 #define MOZ_FOR_EACH_HELPER_GLUE(a, b) a b
65 #define MOZ_FOR_EACH_HELPER(aMacro, aFixedArgs, aArgs) \
66 MOZ_FOR_EACH_HELPER_GLUE( \
67 aMacro, (MOZ_FOR_EACH_EXPAND_HELPER aFixedArgs MOZ_ARG_1 aArgs))
69 #define MOZ_FOR_EACH_0(m, s, fa, a)
70 #define MOZ_FOR_EACH_1(m, s, fa, a) MOZ_FOR_EACH_HELPER(m, fa, a)
71 #define MOZ_FOR_EACH_2(m, s, fa, a) \
72 MOZ_FOR_EACH_HELPER(m, fa, a) \
73 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_1(m, s, fa, (MOZ_ARGS_AFTER_1 a))
74 #define MOZ_FOR_EACH_3(m, s, fa, a) \
75 MOZ_FOR_EACH_HELPER(m, fa, a) \
76 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_2(m, s, fa, (MOZ_ARGS_AFTER_1 a))
77 #define MOZ_FOR_EACH_4(m, s, fa, a) \
78 MOZ_FOR_EACH_HELPER(m, fa, a) \
79 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_3(m, s, fa, (MOZ_ARGS_AFTER_1 a))
80 #define MOZ_FOR_EACH_5(m, s, fa, a) \
81 MOZ_FOR_EACH_HELPER(m, fa, a) \
82 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_4(m, s, fa, (MOZ_ARGS_AFTER_1 a))
83 #define MOZ_FOR_EACH_6(m, s, fa, a) \
84 MOZ_FOR_EACH_HELPER(m, fa, a) \
85 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_5(m, s, fa, (MOZ_ARGS_AFTER_1 a))
86 #define MOZ_FOR_EACH_7(m, s, fa, a) \
87 MOZ_FOR_EACH_HELPER(m, fa, a) \
88 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_6(m, s, fa, (MOZ_ARGS_AFTER_1 a))
89 #define MOZ_FOR_EACH_8(m, s, fa, a) \
90 MOZ_FOR_EACH_HELPER(m, fa, a) \
91 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_7(m, s, fa, (MOZ_ARGS_AFTER_1 a))
92 #define MOZ_FOR_EACH_9(m, s, fa, a) \
93 MOZ_FOR_EACH_HELPER(m, fa, a) \
94 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_8(m, s, fa, (MOZ_ARGS_AFTER_1 a))
95 #define MOZ_FOR_EACH_10(m, s, fa, a) \
96 MOZ_FOR_EACH_HELPER(m, fa, a) \
97 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_9(m, s, fa, (MOZ_ARGS_AFTER_1 a))
98 #define MOZ_FOR_EACH_11(m, s, fa, a) \
99 MOZ_FOR_EACH_HELPER(m, fa, a) \
100 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_10(m, s, fa, (MOZ_ARGS_AFTER_1 a))
101 #define MOZ_FOR_EACH_12(m, s, fa, a) \
102 MOZ_FOR_EACH_HELPER(m, fa, a) \
103 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_11(m, s, fa, (MOZ_ARGS_AFTER_1 a))
104 #define MOZ_FOR_EACH_13(m, s, fa, a) \
105 MOZ_FOR_EACH_HELPER(m, fa, a) \
106 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_12(m, s, fa, (MOZ_ARGS_AFTER_1 a))
107 #define MOZ_FOR_EACH_14(m, s, fa, a) \
108 MOZ_FOR_EACH_HELPER(m, fa, a) \
109 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_13(m, s, fa, (MOZ_ARGS_AFTER_1 a))
110 #define MOZ_FOR_EACH_15(m, s, fa, a) \
111 MOZ_FOR_EACH_HELPER(m, fa, a) \
112 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_14(m, s, fa, (MOZ_ARGS_AFTER_1 a))
113 #define MOZ_FOR_EACH_16(m, s, fa, a) \
114 MOZ_FOR_EACH_HELPER(m, fa, a) \
115 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_15(m, s, fa, (MOZ_ARGS_AFTER_1 a))
116 #define MOZ_FOR_EACH_17(m, s, fa, a) \
117 MOZ_FOR_EACH_HELPER(m, fa, a) \
118 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_16(m, s, fa, (MOZ_ARGS_AFTER_1 a))
119 #define MOZ_FOR_EACH_18(m, s, fa, a) \
120 MOZ_FOR_EACH_HELPER(m, fa, a) \
121 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_17(m, s, fa, (MOZ_ARGS_AFTER_1 a))
122 #define MOZ_FOR_EACH_19(m, s, fa, a) \
123 MOZ_FOR_EACH_HELPER(m, fa, a) \
124 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_18(m, s, fa, (MOZ_ARGS_AFTER_1 a))
125 #define MOZ_FOR_EACH_20(m, s, fa, a) \
126 MOZ_FOR_EACH_HELPER(m, fa, a) \
127 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_19(m, s, fa, (MOZ_ARGS_AFTER_1 a))
128 #define MOZ_FOR_EACH_21(m, s, fa, a) \
129 MOZ_FOR_EACH_HELPER(m, fa, a) \
130 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_20(m, s, fa, (MOZ_ARGS_AFTER_1 a))
131 #define MOZ_FOR_EACH_22(m, s, fa, a) \
132 MOZ_FOR_EACH_HELPER(m, fa, a) \
133 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_21(m, s, fa, (MOZ_ARGS_AFTER_1 a))
134 #define MOZ_FOR_EACH_23(m, s, fa, a) \
135 MOZ_FOR_EACH_HELPER(m, fa, a) \
136 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_22(m, s, fa, (MOZ_ARGS_AFTER_1 a))
137 #define MOZ_FOR_EACH_24(m, s, fa, a) \
138 MOZ_FOR_EACH_HELPER(m, fa, a) \
139 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_23(m, s, fa, (MOZ_ARGS_AFTER_1 a))
140 #define MOZ_FOR_EACH_25(m, s, fa, a) \
141 MOZ_FOR_EACH_HELPER(m, fa, a) \
142 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_24(m, s, fa, (MOZ_ARGS_AFTER_1 a))
143 #define MOZ_FOR_EACH_26(m, s, fa, a) \
144 MOZ_FOR_EACH_HELPER(m, fa, a) \
145 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_25(m, s, fa, (MOZ_ARGS_AFTER_1 a))
146 #define MOZ_FOR_EACH_27(m, s, fa, a) \
147 MOZ_FOR_EACH_HELPER(m, fa, a) \
148 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_26(m, s, fa, (MOZ_ARGS_AFTER_1 a))
149 #define MOZ_FOR_EACH_28(m, s, fa, a) \
150 MOZ_FOR_EACH_HELPER(m, fa, a) \
151 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_27(m, s, fa, (MOZ_ARGS_AFTER_1 a))
152 #define MOZ_FOR_EACH_29(m, s, fa, a) \
153 MOZ_FOR_EACH_HELPER(m, fa, a) \
154 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_28(m, s, fa, (MOZ_ARGS_AFTER_1 a))
155 #define MOZ_FOR_EACH_30(m, s, fa, a) \
156 MOZ_FOR_EACH_HELPER(m, fa, a) \
157 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_29(m, s, fa, (MOZ_ARGS_AFTER_1 a))
158 #define MOZ_FOR_EACH_31(m, s, fa, a) \
159 MOZ_FOR_EACH_HELPER(m, fa, a) \
160 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_30(m, s, fa, (MOZ_ARGS_AFTER_1 a))
161 #define MOZ_FOR_EACH_32(m, s, fa, a) \
162 MOZ_FOR_EACH_HELPER(m, fa, a) \
163 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_31(m, s, fa, (MOZ_ARGS_AFTER_1 a))
164 #define MOZ_FOR_EACH_33(m, s, fa, a) \
165 MOZ_FOR_EACH_HELPER(m, fa, a) \
166 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_32(m, s, fa, (MOZ_ARGS_AFTER_1 a))
167 #define MOZ_FOR_EACH_34(m, s, fa, a) \
168 MOZ_FOR_EACH_HELPER(m, fa, a) \
169 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_33(m, s, fa, (MOZ_ARGS_AFTER_1 a))
170 #define MOZ_FOR_EACH_35(m, s, fa, a) \
171 MOZ_FOR_EACH_HELPER(m, fa, a) \
172 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_34(m, s, fa, (MOZ_ARGS_AFTER_1 a))
173 #define MOZ_FOR_EACH_36(m, s, fa, a) \
174 MOZ_FOR_EACH_HELPER(m, fa, a) \
175 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_35(m, s, fa, (MOZ_ARGS_AFTER_1 a))
176 #define MOZ_FOR_EACH_37(m, s, fa, a) \
177 MOZ_FOR_EACH_HELPER(m, fa, a) \
178 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_36(m, s, fa, (MOZ_ARGS_AFTER_1 a))
179 #define MOZ_FOR_EACH_38(m, s, fa, a) \
180 MOZ_FOR_EACH_HELPER(m, fa, a) \
181 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_37(m, s, fa, (MOZ_ARGS_AFTER_1 a))
182 #define MOZ_FOR_EACH_39(m, s, fa, a) \
183 MOZ_FOR_EACH_HELPER(m, fa, a) \
184 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_38(m, s, fa, (MOZ_ARGS_AFTER_1 a))
185 #define MOZ_FOR_EACH_40(m, s, fa, a) \
186 MOZ_FOR_EACH_HELPER(m, fa, a) \
187 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_39(m, s, fa, (MOZ_ARGS_AFTER_1 a))
188 #define MOZ_FOR_EACH_41(m, s, fa, a) \
189 MOZ_FOR_EACH_HELPER(m, fa, a) \
190 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_40(m, s, fa, (MOZ_ARGS_AFTER_1 a))
191 #define MOZ_FOR_EACH_42(m, s, fa, a) \
192 MOZ_FOR_EACH_HELPER(m, fa, a) \
193 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_41(m, s, fa, (MOZ_ARGS_AFTER_1 a))
194 #define MOZ_FOR_EACH_43(m, s, fa, a) \
195 MOZ_FOR_EACH_HELPER(m, fa, a) \
196 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_42(m, s, fa, (MOZ_ARGS_AFTER_1 a))
197 #define MOZ_FOR_EACH_44(m, s, fa, a) \
198 MOZ_FOR_EACH_HELPER(m, fa, a) \
199 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_43(m, s, fa, (MOZ_ARGS_AFTER_1 a))
200 #define MOZ_FOR_EACH_45(m, s, fa, a) \
201 MOZ_FOR_EACH_HELPER(m, fa, a) \
202 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_44(m, s, fa, (MOZ_ARGS_AFTER_1 a))
203 #define MOZ_FOR_EACH_46(m, s, fa, a) \
204 MOZ_FOR_EACH_HELPER(m, fa, a) \
205 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_45(m, s, fa, (MOZ_ARGS_AFTER_1 a))
206 #define MOZ_FOR_EACH_47(m, s, fa, a) \
207 MOZ_FOR_EACH_HELPER(m, fa, a) \
208 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_46(m, s, fa, (MOZ_ARGS_AFTER_1 a))
209 #define MOZ_FOR_EACH_48(m, s, fa, a) \
210 MOZ_FOR_EACH_HELPER(m, fa, a) \
211 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_47(m, s, fa, (MOZ_ARGS_AFTER_1 a))
212 #define MOZ_FOR_EACH_49(m, s, fa, a) \
213 MOZ_FOR_EACH_HELPER(m, fa, a) \
214 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_48(m, s, fa, (MOZ_ARGS_AFTER_1 a))
215 #define MOZ_FOR_EACH_50(m, s, fa, a) \
216 MOZ_FOR_EACH_HELPER(m, fa, a) \
217 MOZ_FOR_EACH_EXPAND_HELPER s MOZ_FOR_EACH_49(m, s, fa, (MOZ_ARGS_AFTER_1 a))
219 #endif /* mozilla_MacroForEach_h */