Harden failure signal handler in the face of memory corruptions
[folly.git] / folly / Preprocessor.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // @author: Andrei Alexandrescu
18
19 #ifndef FOLLY_PREPROCESSOR_
20 #define FOLLY_PREPROCESSOR_
21
22 /**
23  * Necessarily evil preprocessor-related amenities.
24  */
25
26 /**
27  * FB_ONE_OR_NONE(hello, world) expands to hello and
28  * FB_ONE_OR_NONE(hello) expands to nothing. This macro is used to
29  * insert or eliminate text based on the presence of another argument.
30  */
31 #define FB_ONE_OR_NONE(a, ...) FB_THIRD(a, ## __VA_ARGS__, a)
32 #define FB_THIRD(a, b, ...) __VA_ARGS__
33
34 /**
35  * Helper macro that extracts the first argument out of a list of any
36  * number of arguments.
37  */
38 #define FB_ARG_1(a, ...) a
39
40 /**
41  * Helper macro that extracts the second argument out of a list of any
42  * number of arguments. If only one argument is given, it returns
43  * that.
44  */
45 #define FB_ARG_2_OR_1(...) FB_ARG_2_OR_1_IMPL(__VA_ARGS__, __VA_ARGS__)
46 // Support macro for the above
47 #define FB_ARG_2_OR_1_IMPL(a, b, ...) b
48
49 /**
50  * Helper macro that provides a way to pass argument with commas in it to
51  * some other macro whose syntax doesn't allow using extra parentheses.
52  * Example:
53  *
54  *   #define MACRO(type, name) type name
55  *   MACRO(FB_SINGLE_ARG(std::pair<size_t, size_t>), x);
56  *
57  */
58 #define FB_SINGLE_ARG(...) __VA_ARGS__
59
60 /**
61  * FB_ANONYMOUS_VARIABLE(str) introduces an identifier starting with
62  * str and ending with a number that varies with the line.
63  */
64 #ifndef FB_ANONYMOUS_VARIABLE
65 #define FB_CONCATENATE_IMPL(s1, s2) s1##s2
66 #define FB_CONCATENATE(s1, s2) FB_CONCATENATE_IMPL(s1, s2)
67 #ifdef __COUNTER__
68 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __COUNTER__)
69 #else
70 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __LINE__)
71 #endif
72 #endif
73
74 /**
75  * Use FB_STRINGIZE(x) when you'd want to do what #x does inside
76  * another macro expansion.
77  */
78 #define FB_STRINGIZE(x) #x
79
80 #endif // FOLLY_PREPROCESSOR_