ATLAS Offline Software
MacroHelpers.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 //
4 // Dear emacs, this is -*- c++ -*-
5 //
6 
7 // cppcheck-suppress-file preprocessorErrorDirective; does not understand __VA_ARGS__
8 
9 #ifndef CALORECGPU_MACROHELPERS_H
10 #define CALORECGPU_MACROHELPERS_H
11 
16 #define CRGPU_EMPTY()
17 
20 #define CRGPU_DEFER(...) __VA_ARGS__ CRGPU_EMPTY()
21 
25 #define CRGPU_IGNORE(...)
26 
29 #define CRGPU_CONCAT(X, Y) CRGPU_CONCAT_HELPER(X, Y)
30 
33 #define CRGPU_CONCAT_HELPER(X,...) X ## __VA_ARGS__
34 
35 
38 #define CRGPU_STRINGIFY(X) CRGPU_STRINGIFY_HELPER(X)
39 
42 #define CRGPU_STRINGIFY_HELPER(...) #__VA_ARGS__
43 
44 
47 #define CRGPU_GET_FIRST(X, ...) X
48 
51 #define CRGPU_GET_SECOND(X, ...) CRGPU_GET_FIRST(__VA_ARGS__, X)
52 
55 #define CRGPU_TEST_CRGPU_END ,1
56 
58 #define CRGPU_TEST_ ,1
59 
62 #define CRGPU_CHECK_FOR_END(X) CRGPU_CHECK_FOR_END_CHECK(CRGPU_TEST_ ## X, 0)
63 
66 #define CRGPU_CHECK_FOR_END_CHECK(...) CRGPU_GET_SECOND(__VA_ARGS__,)
67 
70 #define CRGPU_RECURSIVE_MACRO(...) CR_GPU_MACRO_RECURSE1(CR_GPU_MACRO_RECURSE1(CR_GPU_MACRO_RECURSE1(CR_GPU_MACRO_RECURSE1(__VA_ARGS__))))
71 
74 #define CR_GPU_MACRO_RECURSE1(...) CR_GPU_MACRO_RECURSE2(CR_GPU_MACRO_RECURSE2(CR_GPU_MACRO_RECURSE2(CR_GPU_MACRO_RECURSE2(__VA_ARGS__))))
75 
78 #define CR_GPU_MACRO_RECURSE2(...) CR_GPU_MACRO_RECURSE3(CR_GPU_MACRO_RECURSE3(CR_GPU_MACRO_RECURSE3(CR_GPU_MACRO_RECURSE3(__VA_ARGS__))))
79 
82 #define CR_GPU_MACRO_RECURSE3(...) CR_GPU_MACRO_RECURSE4(CR_GPU_MACRO_RECURSE4(CR_GPU_MACRO_RECURSE4(CR_GPU_MACRO_RECURSE4(__VA_ARGS__))))
83 
86 #define CR_GPU_MACRO_RECURSE4(...) CR_GPU_MACRO_RECURSE_BASE(CR_GPU_MACRO_RECURSE_BASE(CR_GPU_MACRO_RECURSE_BASE(CR_GPU_MACRO_RECURSE_BASE(__VA_ARGS__))))
87 
90 #define CR_GPU_MACRO_RECURSE_BASE(...) __VA_ARGS__
91 
96 #define CRGPU_MACRO_EXPANSION(MACRO, EXTRA_ARG, ... ) \
97  CRGPU_MACRO_EXPANSION_IMPL(MACRO, EXTRA_ARG, __VA_ARGS__, CRGPU_END, CRGPU_END)
98 
99 #define CRGPU_MACRO_EXPANSION_IMPL(MACRO, EXTRA_ARG, ONE, NEXT, ... ) \
100  CRGPU_DEFER(CRGPU_CONCAT(CRGPU_MACRO_INSTANTIATION_IMPL_, CRGPU_CHECK_FOR_END(ONE))) (MACRO) (ONE, EXTRA_ARG, (NEXT, __VA_ARGS__) ) \
101  CRGPU_DEFER(CRGPU_CONCAT(CRGPU_MACRO_EXPANSION_IMPL_, CRGPU_CHECK_FOR_END(NEXT))) () (MACRO, EXTRA_ARG, NEXT, __VA_ARGS__ )
102 
105 #define CRGPU_MACRO_EXPANSION_IMPL_0 CRGPU_MACRO_EXPANSION_IMPL_DO
106 
109 #define CRGPU_MACRO_EXPANSION_IMPL_DO() CRGPU_MACRO_EXPANSION_IMPL
110 
113 #define CRGPU_MACRO_EXPANSION_IMPL_1 CRGPU_MACRO_EXPANSION_IMPL_DONT
114 
117 #define CRGPU_MACRO_EXPANSION_IMPL_DONT() CRGPU_IGNORE
118 
121 #define CRGPU_MACRO_INSTANTIATION_IMPL_0 CRGPU_MACRO_INSTANTIATION_DO
122 
125 #define CRGPU_MACRO_INSTANTIATION_IMPL_1 CRGPU_MACRO_INSTANTIATION_DONT
126 
129 #define CRGPU_MACRO_INSTANTIATION_DO(...) __VA_ARGS__
130 
133 #define CRGPU_MACRO_INSTANTIATION_DONT(...) CRGPU_IGNORE
134 
137 #define CRGPU_STRING_TO_ENUM_OP(TYPE, VAR_PREFIX_PAIR, IGNORE) \
138  else if (CRGPU_GET_FIRST VAR_PREFIX_PAIR == CRGPU_STRINGIFY(TYPE)) \
139  { return CRGPU_GET_SECOND VAR_PREFIX_PAIR::TYPE; }
140 
141 
148 #define CRGPU_CHEAP_STRING_TO_ENUM(VAR, PREFIX, ONE, ...) \
149  if (false) { return PREFIX::ONE; } CRGPU_MACRO_EXPANSION(CRGPU_STRING_TO_ENUM_OP, (VAR, PREFIX), ONE, __VA_ARGS__)
150 
151 
152 //Implements this in any C++ compiler that supports variadic macros
153 //(MSVC had some problems in the past, not sure how it is now...)
154 
155 
156 //To call this, wrap in CRGPU_RECURSIVE_MACRO( ... ) to ensure correct expansion.
157 //If there are too many items and the final macros come unexpanded,
158 //thus leading to "weird" compilation errors, maybe wrap it in two or three...
159 //Any question, reach me at nuno.dos.santos.fernandes@cern.ch
160 
161 
162 #endif //CALORECGPU_MACROHELPERS_H