mfbt/MacroArgs.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * Implements various macros meant to ease the use of variadic macros.
michael@0 9 */
michael@0 10
michael@0 11 #ifndef mozilla_MacroArgs_h
michael@0 12 #define mozilla_MacroArgs_h
michael@0 13
michael@0 14 /*
michael@0 15 * MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) counts the number of variadic
michael@0 16 * arguments and prefixes it with |aPrefix|. For example:
michael@0 17 *
michael@0 18 * MOZ_PASTE_PREFIX_AND_ARG_COUNT(, foo, 42) expands to 2
michael@0 19 * MOZ_PASTE_PREFIX_AND_ARG_COUNT(A, foo, 42, bar) expands to A3
michael@0 20 *
michael@0 21 * You must pass in between 1 and 50 (inclusive) variadic arguments, past
michael@0 22 * |aPrefix|. It is not legal to do
michael@0 23 *
michael@0 24 * MOZ_PASTE_PREFIX_AND_ARG_COUNT(prefix)
michael@0 25 *
michael@0 26 * (that is, pass in 0 variadic arguments). To ensure that a compile-time
michael@0 27 * error occurs when these constraints are violated, use the
michael@0 28 * MOZ_STATIC_ASSERT_VALID_ARG_COUNT macro with the same variaidc arguments
michael@0 29 * wherever this macro is used.
michael@0 30 *
michael@0 31 * Passing (__VA_ARGS__, <rest of arguments>) rather than simply calling
michael@0 32 * MOZ_MACROARGS_ARG_COUNT_HELPER2(__VA_ARGS__, <rest of arguments>) very
michael@0 33 * carefully tiptoes around a MSVC bug where it improperly expands __VA_ARGS__
michael@0 34 * as a single token in argument lists. For details, see:
michael@0 35 *
michael@0 36 * http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
michael@0 37 * http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
michael@0 38 */
michael@0 39 #define MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) \
michael@0 40 MOZ_MACROARGS_ARG_COUNT_HELPER((__VA_ARGS__, \
michael@0 41 aPrefix##50, aPrefix##49, aPrefix##48, aPrefix##47, aPrefix##46, \
michael@0 42 aPrefix##45, aPrefix##44, aPrefix##43, aPrefix##42, aPrefix##41, \
michael@0 43 aPrefix##40, aPrefix##39, aPrefix##38, aPrefix##37, aPrefix##36, \
michael@0 44 aPrefix##35, aPrefix##34, aPrefix##33, aPrefix##32, aPrefix##31, \
michael@0 45 aPrefix##30, aPrefix##29, aPrefix##28, aPrefix##27, aPrefix##26, \
michael@0 46 aPrefix##25, aPrefix##24, aPrefix##23, aPrefix##22, aPrefix##21, \
michael@0 47 aPrefix##20, aPrefix##19, aPrefix##18, aPrefix##17, aPrefix##16, \
michael@0 48 aPrefix##15, aPrefix##14, aPrefix##13, aPrefix##12, aPrefix##11, \
michael@0 49 aPrefix##10, aPrefix##9, aPrefix##8, aPrefix##7, aPrefix##6, \
michael@0 50 aPrefix##5, aPrefix##4, aPrefix##3, aPrefix##2, aPrefix##1, aPrefix##0))
michael@0 51
michael@0 52 #define MOZ_MACROARGS_ARG_COUNT_HELPER(aArgs) \
michael@0 53 MOZ_MACROARGS_ARG_COUNT_HELPER2 aArgs
michael@0 54
michael@0 55 #define MOZ_MACROARGS_ARG_COUNT_HELPER2( \
michael@0 56 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, \
michael@0 57 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \
michael@0 58 a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \
michael@0 59 a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \
michael@0 60 a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \
michael@0 61 a51, ...) a51
michael@0 62
michael@0 63 /*
michael@0 64 * MOZ_STATIC_ASSERT_VALID_ARG_COUNT ensures that a compile-time error occurs
michael@0 65 * when the argument count constraints of MOZ_PASTE_PREFIX_AND_ARG_COUNT are
michael@0 66 * violated. Use this macro wherever MOZ_PASTE_PREFIX_AND_ARG_COUNT is used
michael@0 67 * and pass it the same variadic arguments.
michael@0 68 *
michael@0 69 * This macro employs a few dirty tricks to function. To detect the zero
michael@0 70 * argument case, |(__VA_ARGS__)| is stringified, sizeof-ed, and compared to
michael@0 71 * what it should be in the absence of arguments.
michael@0 72 *
michael@0 73 * Detecting too many arguments is a little trickier. With a valid argument
michael@0 74 * count and a prefix of 1, MOZ_PASTE_PREFIX_AND_ARG_COUNT expands to e.g. 14.
michael@0 75 * With a prefix of 0.0, it expands to e.g. 0.04. If there are too many
michael@0 76 * arguments, it expands to the first argument over the limit. If this
michael@0 77 * exceeding argument is a number, the assertion will fail as there is no
michael@0 78 * number than can simultaneously be both > 10 and < 0.1. If the exceeding
michael@0 79 * argument is not a number, a compile-time error will still occur because the
michael@0 80 * exceeding argument is compared to an int and a double.
michael@0 81 */
michael@0 82 #define MOZ_MACROARGS_STRINGIFY_HELPER(x) #x
michael@0 83 #define MOZ_STATIC_ASSERT_VALID_ARG_COUNT(...) \
michael@0 84 static_assert( \
michael@0 85 sizeof(MOZ_MACROARGS_STRINGIFY_HELPER((__VA_ARGS__))) != sizeof("()") && \
michael@0 86 (MOZ_PASTE_PREFIX_AND_ARG_COUNT(1, __VA_ARGS__)) > 10 && \
michael@0 87 (MOZ_PASTE_PREFIX_AND_ARG_COUNT(0.0, __VA_ARGS__)) < 0.1, \
michael@0 88 "MOZ_STATIC_ASSERT_VALID_ARG_COUNT requires 1 to 50 arguments") /* ; */
michael@0 89
michael@0 90 /*
michael@0 91 * MOZ_ARGS_AFTER_N expands to its arguments excluding the first |N|
michael@0 92 * arguments. For example:
michael@0 93 *
michael@0 94 * MOZ_ARGS_AFTER_2(a, b, c, d) expands to: c, d
michael@0 95 */
michael@0 96 #define MOZ_ARGS_AFTER_1(a1, ...) __VA_ARGS__
michael@0 97 #define MOZ_ARGS_AFTER_2(a1, a2, ...) __VA_ARGS__
michael@0 98
michael@0 99 /*
michael@0 100 * MOZ_ARG_N expands to its |N|th argument.
michael@0 101 */
michael@0 102 #define MOZ_ARG_1(a1, ...) a1
michael@0 103 #define MOZ_ARG_2(a1, a2, ...) a2
michael@0 104
michael@0 105 #endif /* mozilla_MacroArgs_h */

mercurial