folly copyright 2015 -> copyright 2016
[folly.git] / folly / Preprocessor.h
1 /*
2  * Copyright 2016 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 #ifdef _MSC_VER
32
33 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
34 #define VA_NARGS(...) VA_NARGS_IMPL(X,##__VA_ARGS__, 4, 3, 2, 1, 0)
35 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
36 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
37 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
38
39 #define FB_ONE_OR_NONE0() /* */
40 #define FB_ONE_OR_NONE1(x) /* */
41 #define FB_ONE_OR_NONE2(x, y) x
42 #define FB_ONE_OR_NONE3(x, y, z) x
43 #define FB_ONE_OR_NONE(...) VARARG(FB_ONE_OR_NONE, __VA_ARGS__)
44
45 #else
46 #define FB_ONE_OR_NONE(a, ...) FB_THIRD(a, ## __VA_ARGS__, a)
47 #define FB_THIRD(a, b, ...) __VA_ARGS__
48 #endif
49
50 /**
51  * Helper macro that extracts the first argument out of a list of any
52  * number of arguments.
53  */
54 #define FB_ARG_1(a, ...) a
55
56 /**
57  * Helper macro that extracts the second argument out of a list of any
58  * number of arguments. If only one argument is given, it returns
59  * that.
60  */
61 #define FB_ARG_2_OR_1(...) FB_ARG_2_OR_1_IMPL(__VA_ARGS__, __VA_ARGS__)
62 // Support macro for the above
63 #define FB_ARG_2_OR_1_IMPL(a, b, ...) b
64
65 /**
66  * Helper macro that provides a way to pass argument with commas in it to
67  * some other macro whose syntax doesn't allow using extra parentheses.
68  * Example:
69  *
70  *   #define MACRO(type, name) type name
71  *   MACRO(FB_SINGLE_ARG(std::pair<size_t, size_t>), x);
72  *
73  */
74 #define FB_SINGLE_ARG(...) __VA_ARGS__
75
76 /**
77  * FB_ANONYMOUS_VARIABLE(str) introduces an identifier starting with
78  * str and ending with a number that varies with the line.
79  */
80 #ifndef FB_ANONYMOUS_VARIABLE
81 #define FB_CONCATENATE_IMPL(s1, s2) s1##s2
82 #define FB_CONCATENATE(s1, s2) FB_CONCATENATE_IMPL(s1, s2)
83 #ifdef __COUNTER__
84 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __COUNTER__)
85 #else
86 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __LINE__)
87 #endif
88 #endif
89
90 /**
91  * Use FB_STRINGIZE(x) when you'd want to do what #x does inside
92  * another macro expansion.
93  */
94 #define FB_STRINGIZE(x) #x
95
96 #endif // FOLLY_PREPROCESSOR_