From 3b18ecb24a1c919ee4c3df95896aaf8f4a6be28a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 28 Jun 2014 11:30:15 -0700 Subject: [PATCH] FMT_FOR_EACH_NARG -> FMT_NARG. --- format.h | 13 ++++++++----- test/macro-test.cc | 13 +++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/format.h b/format.h index 76fdbc93..8966793c 100644 --- a/format.h +++ b/format.h @@ -2178,14 +2178,17 @@ inline void FormatDec(char *&buffer, T value) { #define FMT_CONCATENATE2(arg1, arg2) arg1##arg2 #define FMT_EXPAND(args) args -#define FMT_FOR_EACH_NARG(...) FMT_FOR_EACH_NARG_(__VA_ARGS__, FMT_FOR_EACH_RSEQ_N()) -#define FMT_FOR_EACH_NARG_(...) FMT_EXPAND(FMT_FOR_EACH_ARG_N(__VA_ARGS__)) -#define FMT_FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N -#define FMT_FOR_EACH_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 + +// Returns the number of arguments. +// Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s. +#define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N()) +#define FMT_NARG_(...) FMT_ARG_N(__VA_ARGS__) +#define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N +#define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 #define FMT_FOR_EACH_(N, func, ...) FMT_CONCATENATE(FMT_FOR_EACH, N)(func, __VA_ARGS__) #define FMT_FOR_EACH(f, ...) \ - FMT_EXPAND(FMT_FOR_EACH_(FMT_FOR_EACH_NARG(__VA_ARGS__), f, __VA_ARGS__)) + FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__)) #define FMT_ADD_ARG_NAME(type, index) type arg##index #define FMT_GET_ARG_NAME(type, index) arg##index diff --git a/test/macro-test.cc b/test/macro-test.cc index 57725213..f7b4e91c 100644 --- a/test/macro-test.cc +++ b/test/macro-test.cc @@ -49,6 +49,19 @@ TEST(UtilTest, ForEach) { } } +TEST(UtilTest, NArg) { + EXPECT_EQ(1, FMT_NARG(a)); + EXPECT_EQ(2, FMT_NARG(a, b)); + EXPECT_EQ(3, FMT_NARG(a, b, c)); + EXPECT_EQ(4, FMT_NARG(a, b, c, d)); + EXPECT_EQ(5, FMT_NARG(a, b, c, d, e)); + EXPECT_EQ(6, FMT_NARG(a, b, c, d, e, f)); + EXPECT_EQ(7, FMT_NARG(a, b, c, d, e, f, g)); + EXPECT_EQ(8, FMT_NARG(a, b, c, d, e, f, g, h)); + EXPECT_EQ(9, FMT_NARG(a, b, c, d, e, f, g, h, i)); + EXPECT_EQ(10, FMT_NARG(a, b, c, d, e, f, g, h, i, j)); +} + int result; #define MAKE_TEST(func) \