MacroArgs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /*
  7. * Implements various macros meant to ease the use of variadic macros.
  8. */
  9. #ifndef mozilla_MacroArgs_h
  10. #define mozilla_MacroArgs_h
  11. // Concatenates pre-processor tokens in a way that can be used with __LINE__.
  12. #define MOZ_CONCAT2(x, y) x ## y
  13. #define MOZ_CONCAT(x, y) MOZ_CONCAT2(x, y)
  14. /*
  15. * MOZ_ARG_COUNT(...) counts the number of variadic arguments.
  16. * You must pass in between 0 and 50 (inclusive) variadic arguments.
  17. * For example:
  18. *
  19. * MOZ_ARG_COUNT() expands to 0
  20. * MOZ_ARG_COUNT(a) expands to 1
  21. * MOZ_ARG_COUNT(a, b) expands to 2
  22. *
  23. * Implementation notes:
  24. * The `##__VA_ARGS__` form is a GCC extension that removes the comma if
  25. * __VA_ARGS__ is empty. It is supported by Clang too. MSVC ignores ##,
  26. * and its default behavior is already to strip the comma when __VA_ARGS__
  27. * is empty.
  28. *
  29. * So MOZ_MACROARGS_ARG_COUNT_HELPER() expands to
  30. * (_, 50, 49, ...)
  31. * MOZ_MACROARGS_ARG_COUNT_HELPER(a) expands to
  32. * (_, a, 50, 49, ...)
  33. * etc.
  34. */
  35. #define MOZ_ARG_COUNT(...) \
  36. MOZ_MACROARGS_ARG_COUNT_HELPER2(MOZ_MACROARGS_ARG_COUNT_HELPER(__VA_ARGS__))
  37. #define MOZ_MACROARGS_ARG_COUNT_HELPER(...) (_, ##__VA_ARGS__, \
  38. 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
  39. 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
  40. 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
  41. 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
  42. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  43. #define MOZ_MACROARGS_ARG_COUNT_HELPER2(aArgs) \
  44. MOZ_MACROARGS_ARG_COUNT_HELPER3 aArgs
  45. #define MOZ_MACROARGS_ARG_COUNT_HELPER3(a0, \
  46. a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, \
  47. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \
  48. a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \
  49. a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \
  50. a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \
  51. a51, ...) a51
  52. /*
  53. * MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) counts the number of variadic
  54. * arguments and prefixes it with |aPrefix|. For example:
  55. *
  56. * MOZ_PASTE_PREFIX_AND_ARG_COUNT(, foo, 42) expands to 2
  57. * MOZ_PASTE_PREFIX_AND_ARG_COUNT(A, foo, 42, bar) expands to A3
  58. * MOZ_PASTE_PREFIX_AND_ARG_COUNT(A) expands to A0
  59. * MOZ_PASTE_PREFIX_AND_ARG_COUNT() expands to 0, but MSVC warns there
  60. * aren't enough arguments given.
  61. *
  62. * You must pass in between 0 and 50 (inclusive) variadic arguments, past
  63. * |aPrefix|.
  64. */
  65. #define MOZ_PASTE_PREFIX_AND_ARG_COUNT_GLUE(a, b) a b
  66. #define MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) \
  67. MOZ_PASTE_PREFIX_AND_ARG_COUNT_GLUE( \
  68. MOZ_CONCAT, (aPrefix, MOZ_ARG_COUNT(__VA_ARGS__)))
  69. /*
  70. * MOZ_ARGS_AFTER_N expands to its arguments excluding the first |N|
  71. * arguments. For example:
  72. *
  73. * MOZ_ARGS_AFTER_2(a, b, c, d) expands to: c, d
  74. */
  75. #define MOZ_ARGS_AFTER_1(a1, ...) __VA_ARGS__
  76. #define MOZ_ARGS_AFTER_2(a1, a2, ...) __VA_ARGS__
  77. /*
  78. * MOZ_ARG_N expands to its |N|th argument.
  79. */
  80. #define MOZ_ARG_1(a1, ...) a1
  81. #define MOZ_ARG_2(a1, a2, ...) a2
  82. #endif /* mozilla_MacroArgs_h */