MacroArgs.h 4.7 KB

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