proxygen
Preprocessor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present 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 #pragma once
20 
25 // MSVC's preprocessor is a pain, so we have to
26 // forcefully expand the VA args in some places.
27 #define FB_VA_GLUE(a, b) a b
28 
34 #define FB_ONE_OR_NONE(a, ...) FB_VA_GLUE(FB_THIRD, (a, ##__VA_ARGS__, a))
35 #define FB_THIRD(a, b, ...) __VA_ARGS__
36 
41 #define FB_ARG_1(a, ...) a
42 
48 #ifdef _MSC_VER
49 // GCC refuses to expand this correctly if this macro itself was
50 // called with FB_VA_GLUE :(
51 #define FB_ARG_2_OR_1(...) \
52  FB_VA_GLUE(FB_ARG_2_OR_1_IMPL, (__VA_ARGS__, __VA_ARGS__))
53 #else
54 #define FB_ARG_2_OR_1(...) FB_ARG_2_OR_1_IMPL(__VA_ARGS__, __VA_ARGS__)
55 #endif
56 // Support macro for the above
57 #define FB_ARG_2_OR_1_IMPL(a, b, ...) b
58 
68 #define FB_SINGLE_ARG(...) __VA_ARGS__
69 
70 #define FOLLY_PP_DETAIL_APPEND_VA_ARG(...) , ##__VA_ARGS__
71 
75 #define FOLLY_IGNORE(...)
76 
80 #define FOLLY_SEMICOLON(...) ;
81 
86 #ifndef FB_ANONYMOUS_VARIABLE
87 #define FB_CONCATENATE_IMPL(s1, s2) s1##s2
88 #define FB_CONCATENATE(s1, s2) FB_CONCATENATE_IMPL(s1, s2)
89 #ifdef __COUNTER__
90 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __COUNTER__)
91 #else
92 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __LINE__)
93 #endif
94 #endif
95 
100 #define FB_STRINGIZE(x) #x
101 
102 #define FOLLY_PP_DETAIL_NARGS_1(dummy, _7, _6, _5, _4, _3, _2, _1, _0, ...) _0
103 #define FOLLY_PP_DETAIL_NARGS(...) \
104  FOLLY_PP_DETAIL_NARGS_1(dummy, ##__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0)
105 
106 #define FOLLY_PP_DETAIL_FOR_EACH_REC_0(fn, ...)
107 #define FOLLY_PP_DETAIL_FOR_EACH_REC_1(fn, a, ...) \
108  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_0(fn, __VA_ARGS__)
109 #define FOLLY_PP_DETAIL_FOR_EACH_REC_2(fn, a, ...) \
110  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_1(fn, __VA_ARGS__)
111 #define FOLLY_PP_DETAIL_FOR_EACH_REC_3(fn, a, ...) \
112  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_2(fn, __VA_ARGS__)
113 #define FOLLY_PP_DETAIL_FOR_EACH_REC_4(fn, a, ...) \
114  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_3(fn, __VA_ARGS__)
115 #define FOLLY_PP_DETAIL_FOR_EACH_REC_5(fn, a, ...) \
116  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_4(fn, __VA_ARGS__)
117 #define FOLLY_PP_DETAIL_FOR_EACH_REC_6(fn, a, ...) \
118  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_5(fn, __VA_ARGS__)
119 #define FOLLY_PP_DETAIL_FOR_EACH_REC_7(fn, a, ...) \
120  fn(a) FOLLY_PP_DETAIL_FOR_EACH_REC_6(fn, __VA_ARGS__)
121 
122 #define FOLLY_PP_DETAIL_FOR_EACH_2(fn, n, ...) \
123  FOLLY_PP_DETAIL_FOR_EACH_REC_##n(fn, __VA_ARGS__)
124 #define FOLLY_PP_DETAIL_FOR_EACH_1(fn, n, ...) \
125  FOLLY_PP_DETAIL_FOR_EACH_2(fn, n, __VA_ARGS__)
126 
147 #define FOLLY_PP_FOR_EACH(fn, ...) \
148  FOLLY_PP_DETAIL_FOR_EACH_1( \
149  fn, FOLLY_PP_DETAIL_NARGS(__VA_ARGS__), __VA_ARGS__)