Copyright 2012 -> 2013
[folly.git] / folly / Preprocessor.h
1 /*
2  * Copyright 2013 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  * FB_ANONYMOUS_VARIABLE(str) introduces an identifier starting with
51  * str and ending with a number that varies with the line.
52  */
53 #ifndef FB_ANONYMOUS_VARIABLE
54 #define FB_CONCATENATE_IMPL(s1, s2) s1##s2
55 #define FB_CONCATENATE(s1, s2) FB_CONCATENATE_IMPL(s1, s2)
56 #ifdef __COUNTER__
57 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __COUNTER__)
58 #else
59 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __LINE__)
60 #endif
61 #endif
62
63 /**
64  * Use FB_STRINGIZE(name) when you'd want to do what #name does inside
65  * another macro expansion.
66  */
67 #define FB_STRINGIZE(name) #name
68
69
70 #endif // FOLLY_PREPROCESSOR_