gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
util_macros.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
18 
19 #ifndef __util_macros_hpp__
20 #define __util_macros_hpp__
21 
23 #include "gambit/Utils/cats.hpp"
24 #include "gambit/Utils/stringify.hpp" // stringification macro
25 #include "gambit/Utils/local_info.hpp" // Local information macro.
26 #include "gambit/Utils/export_symbols.hpp" // macro for controlling symbol visibility
27 
28 #include <boost/preprocessor/seq/size.hpp>
29 #include <boost/preprocessor/tuple/elem.hpp>
30 #include <boost/preprocessor/control/if.hpp>
31 #include <boost/preprocessor/comparison/equal.hpp>
32 #include <boost/preprocessor/arithmetic/sub.hpp>
33 #include <boost/preprocessor/punctuation/comma.hpp>
34 #include <boost/preprocessor/punctuation/paren.hpp>
35 
37 #define FAIL(x) static_assert(false,"GAMBIT precompiler error: " x);
38 
40 #define APPEND_TOKEN(s,data,elem) CAT(elem,data)
41 
45 #define DUMMY
46 #define DUMMYARG(...)
47 
50 #define FIRST_ARG(A1,...) A1
51 
54 #define REMFIRST(...) REMFIRST_I(__VA_ARGS__)
55 #define REMFIRST_I(A1,...) (__VA_ARGS__)
56 
61 #define APPLY(macro, args) APPLY_I(macro, args)
62 #define APPLY_I(macro, args) macro args
63 #define STRIP_PARENS(x) EVAL_PAR((STRIP_PARENS_I x), x)
64 #define STRIP_PARENS_I(...) 1,1
65 #define EVAL_PAR(test, x) EVAL_PAR_I(test, x)
66 #define EVAL_PAR_I(test, x) MAYBE_STRIP_PARENS(TEST_ARITY test, x)
67 #define TEST_ARITY(...) APPLY(TEST_ARITY_I, (__VA_ARGS__, 2, 1, 0))
68 #define TEST_ARITY_I(a,b,c,...) c
69 #define MAYBE_STRIP_PARENS(cond, x) MAYBE_STRIP_PARENS_I(cond, x)
70 #define MAYBE_STRIP_PARENS_I(cond, x) CAT(MAYBE_STRIP_PARENS_, cond)(x)
71 #define MAYBE_STRIP_PARENS_1(x) x
72 #define MAYBE_STRIP_PARENS_2(x) APPLY(MAYBE_STRIP_PARENS_2_I, x)
73 #define MAYBE_STRIP_PARENS_2_I(...) __VA_ARGS__
74 
79 #define HAS_PARENS(x) EVAL_HASP((HAS_PARENS_I x), x)
80 #define HAS_PARENS_I(...) 1,1
81 #define EVAL_HASP(test, x) EVAL_HASP_I(test, x)
82 #define EVAL_HASP_I(test, x) CHECK_PARENS(TEST_ARITY test, x)
83 #define CHECK_PARENS(cond, x) CHECK_PARENS_I(cond, x)
84 #define CHECK_PARENS_I(cond, x) CAT(CHECK_PARENS_, cond)(x)
85 #define CHECK_PARENS_1(x) 0
86 #define CHECK_PARENS_2(x) 1
87 
92 #define _ARG128(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \
93 _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, \
94 _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, \
95 _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, \
96 _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, \
97 _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, \
98 _96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, \
99 _110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, _123, _124, _125, _126, _127, ...) _127
100 
101 #define HAS_COMMA(...) _ARG128(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
103 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
104 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
105 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
106 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
107 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
108 
113 #define ISEMPTY(ARG) ISEMPTY_I(_129ONES STRIP_PARENS(ARG) ())
114 #define ISEMPTY_I(A) ISEMPTY_II(A,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
115  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
116  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
117  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
118 #define ISEMPTY_II(...) _ARG128(__VA_ARGS__)
119 #define _129ONES() 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, \
120  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,\
121  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,\
122  1,1,1,1,1,1,1,1,1,1,1,1
123 
129 #define INSERT_NONEMPTY(ARG) CAT(INSERT_NONEMPTY_I,ISEMPTY(ARG))(ARG)
130 #define INSERT_NONEMPTY_I0(ARG) ,STRIP_PARENS(ARG)
131 #define INSERT_NONEMPTY_I1(...) DUMMYARG(__VA_ARGS__)
132 
137 #define PAIR_ELEMENT0(TPLE) PAIR_ELEMENT0_I TPLE
138 #define PAIR_ELEMENT0_I(A,B) A
139 #define PAIR_ELEMENT1(TPLE) PAIR_ELEMENT1_I TPLE
140 #define PAIR_ELEMENT1_I(A,B) B
141 
151 #define void_void 1)(1
152 #define int_int 1)(1
153 #define float_float 1)(1
154 #define double_double 1)(1
155 #define bool_bool 1)(1
156 #define char_char 1)(1
157 #define string_string 1)(1
158 #define ModelParameters_ModelParameters 1)(1
159 #define IS_TYPE(COMPTYPE,TYPE) BOOST_PP_EQUAL(BOOST_PP_SEQ_SIZE((CAT_3(COMPTYPE,_,TYPE))),2)
160 
171 #define IS_EQUAL(A,B) IS_TYPE(A,B)
172 #define IF_EQUAL(A,B,C) BOOST_PP_IIF(IS_EQUAL(A,B),C,)
173 #define IF_NOT_EQUAL(A,B,C) BOOST_PP_IIF(IS_EQUAL(A,B), ,C)
174 #define IF_ELSE_EQUAL(A,B,C,D) BOOST_PP_IIF(IS_EQUAL(A,B),C,D)
175 
181 #define EMPTY_TOKEN_TESTER 1)(1
182 #define IS_EMPTY(A) IS_EQUAL(CAT(A,EMPTY),TOKEN_TESTER)
183 #define IF_EMPTY(A,B) BOOST_PP_IIF(IS_EMPTY(A),B,)
184 #define IF_NOT_EMPTY(A,B) BOOST_PP_IIF(IS_EMPTY(A), ,B)
185 #define IF_ELSE_EMPTY(A,B,C) BOOST_PP_IIF(IS_EMPTY(A),B,C)
186 
199 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, N, ...) N
200 #define VA_NARGS(...) VA_NARGS_IMPL(X,__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
201 #define VARARG_IMPL2(base, count, ...) CAT_3(base,_,count)(__VA_ARGS__)
202 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
203 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
204 
208 #define VA_NARGS_NESTED_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, N, ...) N
209 #define VA_NARGS_NESTED(...) VA_NARGS_NESTED_IMPL(X,__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
210 #define VARARG_NESTED_IMPL2(base, count, ...) CAT_3(base,_,count)(__VA_ARGS__)
211 #define VARARG_NESTED_IMPL(base, count, ...) VARARG_NESTED_IMPL2(base, count, __VA_ARGS__)
212 #define VARARG_NESTED(base, ...) VARARG_NESTED_IMPL(base, VA_NARGS_NESTED(__VA_ARGS__), __VA_ARGS__)
213 
217 #define VA_NARGS_AB_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, N, ...) N
218 #define VA_NARGS_AB(...) VA_NARGS_AB_IMPL(X,__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
219 #define VARARG_AB_IMPL2(base, A, B, count, ...) CAT_3(base,_,count)(A, B, __VA_ARGS__)
220 #define VARARG_AB_IMPL(base, A, B, count, ...) VARARG_AB_IMPL2(base, A, B, count, __VA_ARGS__)
221 #define VARARG_AB(base, A, B, ...) VARARG_AB_IMPL(base, A, B, VA_NARGS_AB(__VA_ARGS__), __VA_ARGS__)
222 
226 #define VARARG_SWITCH_ON_GT_ONE_TESTER_1 1)(1
227 #define VA_NARGS_SWITCH_ON_GT_ONE_IMPL(_1, _2, _3, N, ...) IF_ELSE_EQUAL(VARARG_SWITCH_ON_GT_ONE_TESTER,N,N,MORE)
228 #define VA_NARGS_SWITCH_ON_GT_ONE(...) VA_NARGS_SWITCH_ON_GT_ONE_IMPL(X,__VA_ARGS__, 2, 1, 0)
229 #define VARARG_SWITCH_ON_GT_ONE_IMPL2(base, count, ...) CAT_3(base,_,count)(__VA_ARGS__)
230 #define VARARG_SWITCH_ON_GT_ONE_IMPL(base, count, ...) VARARG_SWITCH_ON_GT_ONE_IMPL2(base, count, __VA_ARGS__)
231 #define VARARG_SWITCH_ON_GT_ONE(base, ...) VARARG_SWITCH_ON_GT_ONE_IMPL(base, VA_NARGS_SWITCH_ON_GT_ONE(__VA_ARGS__), __VA_ARGS__)
232 
236 #define VARARG_SWITCH_ON_GT_ONE_A_TESTER_1 1)(1
237 #define VA_NARGS_SWITCH_ON_GT_ONE_A_IMPL(_1, _2, _3, N, ...) IF_ELSE_EQUAL(VARARG_SWITCH_ON_GT_ONE_A_TESTER,N,N,MORE)
238 #define VA_NARGS_SWITCH_ON_GT_ONE_A(...) VA_NARGS_SWITCH_ON_GT_ONE_A_IMPL(X,__VA_ARGS__, 2, 1, 0)
239 #define VARARG_SWITCH_ON_GT_ONE_A_IMPL2(base, A, count, ...) CAT_3(base,_,count)(A, __VA_ARGS__)
240 #define VARARG_SWITCH_ON_GT_ONE_A_IMPL(base, A, count, ...) VARARG_SWITCH_ON_GT_ONE_A_IMPL2(base, A, count, __VA_ARGS__)
241 #define VARARG_SWITCH_ON_GT_ONE_A(base, A, ...) VARARG_SWITCH_ON_GT_ONE_A_IMPL(base, A, VA_NARGS_SWITCH_ON_GT_ONE_A(__VA_ARGS__), __VA_ARGS__)
242 
246 #define VARARG_SWITCH_ON_GT_ONE_ABC_TESTER_1 1)(1
247 #define VA_NARGS_SWITCH_ON_GT_ONE_ABC_IMPL(_1, _2, _3, N, ...) IF_ELSE_EQUAL(VARARG_SWITCH_ON_GT_ONE_ABC_TESTER,N,N,MORE)
248 #define VA_NARGS_SWITCH_ON_GT_ONE_ABC(...) VA_NARGS_SWITCH_ON_GT_ONE_ABC_IMPL(X,__VA_ARGS__, 2, 1, 0)
249 #define VARARG_SWITCH_ON_GT_ONE_ABC_IMPL2(base, A, B, C, count, ...) CAT_3(base,_,count)(A, B, C, __VA_ARGS__)
250 #define VARARG_SWITCH_ON_GT_ONE_ABC_IMPL(base, A, B, C, count, ...) VARARG_SWITCH_ON_GT_ONE_ABC_IMPL2(base, A, B, C, count, __VA_ARGS__)
251 #define VARARG_SWITCH_ON_GT_ONE_ABC(base, A, B, C, ...) VARARG_SWITCH_ON_GT_ONE_ABC_IMPL(base, A, B, C, VA_NARGS_SWITCH_ON_GT_ONE_ABC(__VA_ARGS__), __VA_ARGS__)
252 
256 #define VARARG_SWITCH_ON_GT_TWO_ABC_TESTER_2 1)(1
257 #define VARARG_SWITCH_ON_GT_TWO_ABC_TESTER_1 1)(1
258 #define VA_NARGS_SWITCH_ON_GT_TWO_ABC_IMPL(_1, _2, _3, _4, N, ...) IF_ELSE_EQUAL(VARARG_SWITCH_ON_GT_TWO_ABC_TESTER,\
259  BOOST_PP_TUPLE_ELEM(0,(STRIP_PARENS(N))),N,MORE)
260 #define VA_NARGS_SWITCH_ON_GT_TWO_ABC(...) VA_NARGS_SWITCH_ON_GT_TWO_ABC_IMPL(X,__VA_ARGS__, 3, 2, 1, 0)
261 #define VARARG_SWITCH_ON_GT_TWO_ABC_IMPL2(base, A, B, C, count, ...) CAT_3(base,_,count)(A, B, C, __VA_ARGS__)
262 #define VARARG_SWITCH_ON_GT_TWO_ABC_IMPL(base, A, B, C, count, ...) VARARG_SWITCH_ON_GT_TWO_ABC_IMPL2(base, A, B, C, count, __VA_ARGS__)
263 #define VARARG_SWITCH_ON_GT_TWO_ABC(base, A, B, C, ...) VARARG_SWITCH_ON_GT_TWO_ABC_IMPL(base, A, B, C, VA_NARGS_SWITCH_ON_GT_TWO_ABC(__VA_ARGS__), __VA_ARGS__)
264 
283 #define CHECK_N(x, n, ...) n
284 #define CHECK(...) CHECK_N(__VA_ARGS__, 0, 0)
285 #define PROBE(x) x, 1
286 #define DEFINED_PROBE(NAME) DEFINED_PROBE_PROXY( DEFINED_##NAME ) // concatenate DEFINED_ prefix with function name
287 #define DEFINED_PROBE_PROXY(...) DEFINED_PROBE_PRIMITIVE(__VA_ARGS__) // expand arguments
288 #define DEFINED_PROBE_PRIMITIVE(x) DEFINED_PROBE_COMBINE_ x // collapse again
289 #define DEFINED_PROBE_COMBINE_(...) PROBE(X)
290 #define DEFINED(NAME) CHECK(DEFINED_PROBE(NAME))
293 
298 
300 #define IF_DEFINED(NAME,ACTION) BOOST_PP_IF(DEFINED(NAME), ACTION, )
301 #define IF_NOT_DEFINED(NAME,ACTION) BOOST_PP_IF(DEFINED(NAME), , ACTION)
303 #define IF_ELSE_DEFINED(NAME,IF,ELSE) BOOST_PP_IF(DEFINED(NAME), IF, ELSE)
305 
309 #define TOKEN_DEFINED(A) BOOST_PP_NOT(DEFINED(A))
310 #define IF_TOKEN_DEFINED(A,B) BOOST_PP_IIF(DEFINED(A),BOOST_PP_EMPTY(),B)
311 #define IF_TOKEN_UNDEFINED(A,B) BOOST_PP_IIF(DEFINED(A),B,BOOST_PP_EMPTY())
312 #define IF_ELSE_TOKEN_DEFINED(A,B,C) BOOST_PP_IIF(DEFINED(A),C,B)
313 #define DEFINED_MODULE ()
314 #define DEFINED_CAPABILITY ()
315 #define DEFINED_FUNCTION ()
316 #define DEFINED_CONDITIONAL_DEPENDENCY ()
317 
321 #define POP_LAST(...) CAT(POP_LAST_, BOOST_PP_VARIADIC_SIZE __VA_ARGS__) __VA_ARGS__
322 #define POP_LAST_1(x1)
323 #define POP_LAST_2(x1, x2) x1
324 #define POP_LAST_3(x1, x2, x3) x1, x2
325 #define POP_LAST_4(x1, x2, x3, x4) x1, x2, x3
326 #define POP_LAST_5(x1, x2, x3, x4, x5) x1, x2, x3, x4
327 #define POP_LAST_6(x1, x2, x3, x4, x5, x6) x1, x2, x3, x4, x5
328 #define POP_LAST_7(x1, x2, x3, x4, x5, x6, x7) x1, x2, x3, x4, x5, x6
329 #define POP_LAST_8(x1, x2, x3, x4, x5, x6, x7, x8) x1, x2, x3, x4, x5, x6, x7
330 #define POP_LAST_9(x1, x2, x3, x4, x5, x6, x7, x8, x9) x1, x2, x3, x4, x5, x6, x7, x8
331 #define POP_LAST_10(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) x1, x2, x3, x4, x5, x6, x7, x8, x9
332 
336 #define etc_etc 1)(1
337 #define LAST_ARG_VARIADIC(T) IS_ELLIPSIS(BOOST_PP_TUPLE_ELEM(BOOST_PP_SUB(BOOST_PP_VARIADIC_SIZE T,1),T))
338 #define IS_ELLIPSIS(ELEM) IS_EQUAL(etc,ELEM)
339 #define STRIP_VARIADIC_ARG(T) BOOST_PP_IIF(LAST_ARG_VARIADIC(T), (POP_LAST(T)), T)
340 #define CONVERT_VARIADIC_ARG(T) BOOST_PP_IIF(LAST_ARG_VARIADIC(T), (POP_LAST(T), ...), T)
341 
345 #define IF_ELSEIF_9(VAR1, _1, VAR2, _2, VAR3, _3, VAR4, _4, _5) \
346  BOOST_PP_IF(VAR1, _1, \
347  BOOST_PP_IF(VAR2, _2, \
348  BOOST_PP_IF(VAR3, _3, \
349  BOOST_PP_IF(VAR4, _4, _5))))
350 #define IF_ELSEIF_7(VAR1, _1, VAR2, _2, VAR3, _3, _4) \
351  BOOST_PP_IF(VAR1, _1, \
352  BOOST_PP_IF(VAR2, _2, \
353  BOOST_PP_IF(VAR3, _3, _4)))
354 #define IF_ELSEIF_5(VAR1, _1, VAR2, _2, _3) \
355  BOOST_PP_IF(VAR1, _1, \
356  BOOST_PP_IF(VAR2, _2, _3))
357 #define IF_ELSEIF(...) VARARG_NESTED(IF_ELSEIF, __VA_ARGS__)
358 
366 #define BRACED_INIT_LIST(...) { __VA_ARGS__ }
367 #define MAKE_STATIC_VECTOR(TYPE,NAME,TUPLE) \
368  static const TYPE NAME##_array[] = BRACED_INIT_LIST TUPLE; \
369  static const std::vector<TYPE> NAME(NAME##_array, Utils::endA( NAME##_array ));
370 #define MAKE_STATIC_SET(TYPE,NAME,TUPLE) \
371  static const TYPE NAME##_array[] = BRACED_INIT_LIST(TUPLE); \
372  static const std::set<TYPE> NAME(NAME##_array, Utils::endA( NAME##_array ));
373 
374 #endif //defined __util_macros_hpp__
LOCAL_INFO macro.
Copies of boost headers that are required but that may be too recent to be present on the user&#39;s mach...
Stringification macro.
Helper macro for controlling symbol visibility in shared libraries.
Concatenation macros.