2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // @author: Andrei Alexandrescu
22 * Necessarily evil preprocessor-related amenities.
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
30 * FB_ONE_OR_NONE(hello, world) expands to hello and
31 * FB_ONE_OR_NONE(hello) expands to nothing. This macro is used to
32 * insert or eliminate text based on the presence of another argument.
34 #define FB_ONE_OR_NONE(a, ...) FB_VA_GLUE(FB_THIRD, (a, ## __VA_ARGS__, a))
35 #define FB_THIRD(a, b, ...) __VA_ARGS__
38 * Helper macro that extracts the first argument out of a list of any
39 * number of arguments.
41 #define FB_ARG_1(a, ...) a
44 * Helper macro that extracts the second argument out of a list of any
45 * number of arguments. If only one argument is given, it returns
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__))
54 #define FB_ARG_2_OR_1(...) FB_ARG_2_OR_1_IMPL(__VA_ARGS__, __VA_ARGS__)
56 // Support macro for the above
57 #define FB_ARG_2_OR_1_IMPL(a, b, ...) b
60 * Helper macro that provides a way to pass argument with commas in it to
61 * some other macro whose syntax doesn't allow using extra parentheses.
64 * #define MACRO(type, name) type name
65 * MACRO(FB_SINGLE_ARG(std::pair<size_t, size_t>), x);
68 #define FB_SINGLE_ARG(...) __VA_ARGS__
71 * Helper macro that just ignores its parameters.
73 #define FOLLY_IGNORE(...)
76 * Helper macro that just ignores its parameters and inserts a semicolon.
78 #define FOLLY_SEMICOLON(...) ;
81 * FB_ANONYMOUS_VARIABLE(str) introduces an identifier starting with
82 * str and ending with a number that varies with the line.
84 #ifndef FB_ANONYMOUS_VARIABLE
85 #define FB_CONCATENATE_IMPL(s1, s2) s1##s2
86 #define FB_CONCATENATE(s1, s2) FB_CONCATENATE_IMPL(s1, s2)
88 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __COUNTER__)
90 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __LINE__)
95 * Use FB_STRINGIZE(x) when you'd want to do what #x does inside
96 * another macro expansion.
98 #define FB_STRINGIZE(x) #x