2 * Copyright 2012 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
19 #ifndef FOLLY_PREPROCESSOR_
20 #define FOLLY_PREPROCESSOR_
23 * Necessarily evil preprocessor-related amenities.
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.
31 #define FB_ONE_OR_NONE(a, ...) FB_THIRD(a, ## __VA_ARGS__, a)
32 #define FB_THIRD(a, b, ...) __VA_ARGS__
35 * Helper macro that extracts the first argument out of a list of any
36 * number of arguments.
38 #define FB_ARG_1(a, ...) a
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
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
50 * FB_ANONYMOUS_VARIABLE(str) introduces an identifier starting with
51 * str and ending with a number that varies with the line.
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)
57 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __COUNTER__)
59 #define FB_ANONYMOUS_VARIABLE(str) FB_CONCATENATE(str, __LINE__)
64 * Use FB_STRINGIZE(name) when you'd want to do what #name does inside
65 * another macro expansion.
67 #define FB_STRINGIZE(name) #name
70 #endif // FOLLY_PREPROCESSOR_