1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/MacroArgs.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* 1.11 + * Implements various macros meant to ease the use of variadic macros. 1.12 + */ 1.13 + 1.14 +#ifndef mozilla_MacroArgs_h 1.15 +#define mozilla_MacroArgs_h 1.16 + 1.17 +/* 1.18 + * MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) counts the number of variadic 1.19 + * arguments and prefixes it with |aPrefix|. For example: 1.20 + * 1.21 + * MOZ_PASTE_PREFIX_AND_ARG_COUNT(, foo, 42) expands to 2 1.22 + * MOZ_PASTE_PREFIX_AND_ARG_COUNT(A, foo, 42, bar) expands to A3 1.23 + * 1.24 + * You must pass in between 1 and 50 (inclusive) variadic arguments, past 1.25 + * |aPrefix|. It is not legal to do 1.26 + * 1.27 + * MOZ_PASTE_PREFIX_AND_ARG_COUNT(prefix) 1.28 + * 1.29 + * (that is, pass in 0 variadic arguments). To ensure that a compile-time 1.30 + * error occurs when these constraints are violated, use the 1.31 + * MOZ_STATIC_ASSERT_VALID_ARG_COUNT macro with the same variaidc arguments 1.32 + * wherever this macro is used. 1.33 + * 1.34 + * Passing (__VA_ARGS__, <rest of arguments>) rather than simply calling 1.35 + * MOZ_MACROARGS_ARG_COUNT_HELPER2(__VA_ARGS__, <rest of arguments>) very 1.36 + * carefully tiptoes around a MSVC bug where it improperly expands __VA_ARGS__ 1.37 + * as a single token in argument lists. For details, see: 1.38 + * 1.39 + * http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement 1.40 + * http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644 1.41 + */ 1.42 +#define MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) \ 1.43 + MOZ_MACROARGS_ARG_COUNT_HELPER((__VA_ARGS__, \ 1.44 + aPrefix##50, aPrefix##49, aPrefix##48, aPrefix##47, aPrefix##46, \ 1.45 + aPrefix##45, aPrefix##44, aPrefix##43, aPrefix##42, aPrefix##41, \ 1.46 + aPrefix##40, aPrefix##39, aPrefix##38, aPrefix##37, aPrefix##36, \ 1.47 + aPrefix##35, aPrefix##34, aPrefix##33, aPrefix##32, aPrefix##31, \ 1.48 + aPrefix##30, aPrefix##29, aPrefix##28, aPrefix##27, aPrefix##26, \ 1.49 + aPrefix##25, aPrefix##24, aPrefix##23, aPrefix##22, aPrefix##21, \ 1.50 + aPrefix##20, aPrefix##19, aPrefix##18, aPrefix##17, aPrefix##16, \ 1.51 + aPrefix##15, aPrefix##14, aPrefix##13, aPrefix##12, aPrefix##11, \ 1.52 + aPrefix##10, aPrefix##9, aPrefix##8, aPrefix##7, aPrefix##6, \ 1.53 + aPrefix##5, aPrefix##4, aPrefix##3, aPrefix##2, aPrefix##1, aPrefix##0)) 1.54 + 1.55 +#define MOZ_MACROARGS_ARG_COUNT_HELPER(aArgs) \ 1.56 + MOZ_MACROARGS_ARG_COUNT_HELPER2 aArgs 1.57 + 1.58 +#define MOZ_MACROARGS_ARG_COUNT_HELPER2( \ 1.59 + a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, \ 1.60 + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \ 1.61 + a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \ 1.62 + a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \ 1.63 + a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \ 1.64 + a51, ...) a51 1.65 + 1.66 +/* 1.67 + * MOZ_STATIC_ASSERT_VALID_ARG_COUNT ensures that a compile-time error occurs 1.68 + * when the argument count constraints of MOZ_PASTE_PREFIX_AND_ARG_COUNT are 1.69 + * violated. Use this macro wherever MOZ_PASTE_PREFIX_AND_ARG_COUNT is used 1.70 + * and pass it the same variadic arguments. 1.71 + * 1.72 + * This macro employs a few dirty tricks to function. To detect the zero 1.73 + * argument case, |(__VA_ARGS__)| is stringified, sizeof-ed, and compared to 1.74 + * what it should be in the absence of arguments. 1.75 + * 1.76 + * Detecting too many arguments is a little trickier. With a valid argument 1.77 + * count and a prefix of 1, MOZ_PASTE_PREFIX_AND_ARG_COUNT expands to e.g. 14. 1.78 + * With a prefix of 0.0, it expands to e.g. 0.04. If there are too many 1.79 + * arguments, it expands to the first argument over the limit. If this 1.80 + * exceeding argument is a number, the assertion will fail as there is no 1.81 + * number than can simultaneously be both > 10 and < 0.1. If the exceeding 1.82 + * argument is not a number, a compile-time error will still occur because the 1.83 + * exceeding argument is compared to an int and a double. 1.84 + */ 1.85 +#define MOZ_MACROARGS_STRINGIFY_HELPER(x) #x 1.86 +#define MOZ_STATIC_ASSERT_VALID_ARG_COUNT(...) \ 1.87 + static_assert( \ 1.88 + sizeof(MOZ_MACROARGS_STRINGIFY_HELPER((__VA_ARGS__))) != sizeof("()") && \ 1.89 + (MOZ_PASTE_PREFIX_AND_ARG_COUNT(1, __VA_ARGS__)) > 10 && \ 1.90 + (MOZ_PASTE_PREFIX_AND_ARG_COUNT(0.0, __VA_ARGS__)) < 0.1, \ 1.91 + "MOZ_STATIC_ASSERT_VALID_ARG_COUNT requires 1 to 50 arguments") /* ; */ 1.92 + 1.93 +/* 1.94 + * MOZ_ARGS_AFTER_N expands to its arguments excluding the first |N| 1.95 + * arguments. For example: 1.96 + * 1.97 + * MOZ_ARGS_AFTER_2(a, b, c, d) expands to: c, d 1.98 + */ 1.99 +#define MOZ_ARGS_AFTER_1(a1, ...) __VA_ARGS__ 1.100 +#define MOZ_ARGS_AFTER_2(a1, a2, ...) __VA_ARGS__ 1.101 + 1.102 +/* 1.103 + * MOZ_ARG_N expands to its |N|th argument. 1.104 + */ 1.105 +#define MOZ_ARG_1(a1, ...) a1 1.106 +#define MOZ_ARG_2(a1, a2, ...) a2 1.107 + 1.108 +#endif /* mozilla_MacroArgs_h */