Debian Codegraph @

src/atlas/library/detail/BlackMagic.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 // This file contains preprocessor black magic. It contains macros that
14 // can return the number of arguments passed
15
16 //-----------------------------------------------------------------------------------------------------------
17 // Public
18
19 /// Returns the number of passed arguments
20 #define __ATLAS_NARG(...)
21
22 /// Splice a and b together
23 #define __ATLAS_SPLICE(a, b)
24
25 #define __ATLAS_STRINGIFY(a) a
26
27 #define __ATLAS_TYPE(Type, ...)
28 #define __ATLAS_TYPE_SCOPE(Type, ...)
29
30 //-----------------------------------------------------------------------------------------------------------
31 // Details
32
33 // Undefine these, to be redefined further down.
34 #undef __ATLAS_NARG
35 #undef __ATLAS_SPLICE
36 #undef __ATLAS_TYPE
37 #undef __ATLAS_TYPE_SCOPE
38
39 #define __ATLAS_REVERSE 5, 4, 3, 2, 1, 0
40 #define __ATLAS_ARGN(_1, _2, _3, _4, _5, N, ...) N
41 #define __ATLAS_NARG__(dummy, ...) __ATLAS_ARGN(__VA_ARGS__)
42 #define __ATLAS_NARG_(...) __ATLAS_NARG__(dummy, ##__VA_ARGS__, __ATLAS_REVERSE)
43 #define __ATLAS_SPLICE(a, b) __ATLAS_SPLICE_1(a, b)
44 #define __ATLAS_SPLICE_1(a, b) __ATLAS_SPLICE_2(a, b)
45 #define __ATLAS_SPLICE_2(a, b) a##b
46
47 #define __ATLAS_ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
48 #define __ATLAS_HAS_COMMA(...) __ATLAS_ARG16(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
49 #define __ATLAS_TRIGGER_PARENTHESIS(...) ,
50 #define __ATLAS_ISEMPTY(...) \
51 __ATLAS_ISEMPTY_( /* test if there is just one argument, eventually an empty \
52 one */ \
53 __ATLAS_HAS_COMMA(__VA_ARGS__), /* test if \
54 _TRIGGER_PARENTHESIS_ \
55 together with the \
56 argument adds a comma */ \
57 __ATLAS_HAS_COMMA( \
58 __ATLAS_TRIGGER_PARENTHESIS __VA_ARGS__), /* test if the argument together with \
59 a parenthesis adds a comma */ \
60 __ATLAS_HAS_COMMA(__VA_ARGS__(/*empty*/)), /* test if placing it between \
61 __ATLAS_TRIGGER_PARENTHESIS and the \
62 parenthesis adds a comma */ \
63 __ATLAS_HAS_COMMA(__ATLAS_TRIGGER_PARENTHESIS __VA_ARGS__(/*empty*/)))
64
65 #define __ATLAS_PASTE5(_0, _1, _2, _3, _4) _0##_1##_2##_3##_4
66 #define __ATLAS_ISEMPTY_(_0, _1, _2, _3) __ATLAS_HAS_COMMA(__ATLAS_PASTE5(__ATLAS_IS_EMPTY_CASE_, _0, _1, _2, _3))
67 #define __ATLAS_IS_EMPTY_CASE_0001 ,
68
69 #define __ATLAS_NARG(...) __ATLAS_SPLICE(__ATLAS_CALL_NARG_, __ATLAS_ISEMPTY(__VA_ARGS__))(__VA_ARGS__)
70 #define __ATLAS_CALL_NARG_1(...) 0
71 #define __ATLAS_CALL_NARG_0 __ATLAS_NARG_
72
73 #define __ATLAS_COMMA_ARGS(...) __ATLAS_SPLICE(__ATLAS_COMMA_ARGS_, __ATLAS_ISEMPTY(__VA_ARGS__))(__VA_ARGS__)
74 #define __ATLAS_COMMA_ARGS_1(...)
75 #define __ATLAS_COMMA_ARGS_0(...) , __VA_ARGS__
76
77 #define __ATLAS_ARGS_OR_DUMMY(...) \
78 __ATLAS_SPLICE(__ATLAS_ARGS_OR_DUMMY_, __ATLAS_ISEMPTY(__VA_ARGS__)) \
79 (__VA_ARGS__)
80 #define __ATLAS_ARGS_OR_DUMMY_0(...) __VA_ARGS__
81 #define __ATLAS_ARGS_OR_DUMMY_1(...) 0
82
83 #define __ATLAS_TYPE(Type, ...) \
84 __ATLAS_SPLICE(__ATLAS_TYPE_, __ATLAS_ISEMPTY(__VA_ARGS__)) \
85 (Type, __ATLAS_ARGS_OR_DUMMY(__VA_ARGS__))
86 #define __ATLAS_TYPE_1(Type, dummy) Type __ATLAS_SPLICE(__variable_, __LINE__)
87 #define __ATLAS_TYPE_0(Type, ...) Type __ATLAS_SPLICE(__variable_, __LINE__)(__VA_ARGS__)
88
89 #define __ATLAS_TYPE_SCOPE(Type, ...) \
90 __ATLAS_SPLICE(__ATLAS_TYPE_SCOPE_, __ATLAS_ISEMPTY(__VA_ARGS__)) \
91 (Type, __ATLAS_ARGS_OR_DUMMY(__VA_ARGS__))
92 #define __ATLAS_TYPE_SCOPE_1(Type, ...) \
93 for (bool __ATLAS_SPLICE(__done_, __LINE__) = false; __ATLAS_SPLICE(__done_, __LINE__) != true;) \
94 for (Type __ATLAS_SPLICE(__variable_, __LINE__); __ATLAS_SPLICE(__done_, __LINE__) != true; \
95 __ATLAS_SPLICE(__done_, __LINE__) = true)
96 #define __ATLAS_TYPE_SCOPE_0(Type, ...) \
97 for (bool __ATLAS_SPLICE(__done_, __LINE__) = false; __ATLAS_SPLICE(__done_, __LINE__) != true;) \
98 for (Type __ATLAS_SPLICE(__variable_, __LINE__)(__VA_ARGS__); __ATLAS_SPLICE(__done_, __LINE__) != true; \
99 __ATLAS_SPLICE(__done_, __LINE__) = true)