1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_MACROS_REMOVE_PARENS_H_ 6 #define BASE_MACROS_REMOVE_PARENS_H_ 7 8 #include "base/macros/if.h" 9 #include "base/macros/is_empty.h" 10 11 // A macro that removes at most one outer set of parentheses from its arguments. 12 // If the arguments are not surrounded by parentheses, this expands to the 13 // arguments unchanged. For example: 14 // `BASE_REMOVE_PARENS()` -> `` 15 // `BASE_REMOVE_PARENS(foo)` -> `foo` 16 // `BASE_REMOVE_PARENS(foo(1))` -> `foo(1)` 17 // `BASE_REMOVE_PARENS((foo))` -> `foo` 18 // `BASE_REMOVE_PARENS((foo(1)))` -> `foo(1)` 19 // `BASE_REMOVE_PARENS((foo)[1])` -> `(foo)[1]` 20 // `BASE_REMOVE_PARENS(((foo)))` -> `(foo)` 21 // `BASE_REMOVE_PARENS(foo, bar, baz)` -> `foo, bar, baz` 22 // `BASE_REMOVE_PARENS(foo, (bar), baz)` -> `foo, (bar), baz` 23 #define BASE_REMOVE_PARENS(...) \ 24 BASE_IF(BASE_INTERNAL_IS_PARENTHESIZED(__VA_ARGS__), BASE_INTERNAL_ECHO, \ 25 BASE_INTERNAL_EMPTY()) \ 26 __VA_ARGS__ 27 28 #define BASE_INTERNAL_IS_PARENTHESIZED(...) \ 29 BASE_IS_EMPTY(BASE_INTERNAL_EAT __VA_ARGS__) 30 #define BASE_INTERNAL_EAT(...) 31 #define BASE_INTERNAL_ECHO(...) __VA_ARGS__ 32 #define BASE_INTERNAL_EMPTY() 33 34 #endif // BASE_MACROS_REMOVE_PARENS_H_ 35