From ce9285aae91136ff872c3ae1efd9183df7aad142 Mon Sep 17 00:00:00 2001 From: "LUDA-PC\\ludek.vodicka" Date: Sun, 22 Oct 2017 16:27:09 +0200 Subject: [PATCH 1/3] FMT_VARIADIC_CONST - Support for const variadic methods --- fmt/format.h | 52 +++++++++++++++++++++++++-------------------- test/format-test.cc | 18 ++++++++++++++++ 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index abe2b9c527ff..8ad531d510d8 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -3634,10 +3634,10 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; #define FMT_GET_ARG_NAME(type, index) arg##index #if FMT_USE_VARIADIC_TEMPLATES -# define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \ +# define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \ template \ ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ - const Args & ... args) { \ + const Args & ... args) Const { \ typedef fmt::internal::ArgArray ArgArray; \ typename ArgArray::Type array{ \ ArgArray::template make >(args)...}; \ @@ -3647,35 +3647,35 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; #else // Defines a wrapper for a function taking __VA_ARGS__ arguments // and n additional arguments of arbitrary types. -# define FMT_WRAP(Char, ReturnType, func, call, n, ...) \ +# define FMT_WRAP(Const,Char, ReturnType, func, call, n, ...) \ template \ inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ - FMT_GEN(n, FMT_MAKE_ARG)) { \ + FMT_GEN(n, FMT_MAKE_ARG)) Const { \ fmt::internal::ArgArray::Type arr; \ FMT_GEN(n, FMT_ASSIGN_##Char); \ call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \ fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \ } -# define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \ - inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \ +# define FMT_VARIADIC_(Const,Char, ReturnType, func, call, ...) \ + inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) Const { \ call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \ } \ - FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \ - FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__) + FMT_WRAP(Const, Char, ReturnType, func, call, 1, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 2, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 3, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 4, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 5, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 6, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 7, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 8, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 9, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 10, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 11, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 12, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 13, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 14, __VA_ARGS__) \ + FMT_WRAP(Const, Char, ReturnType, func, call, 15, __VA_ARGS__) #endif // FMT_USE_VARIADIC_TEMPLATES /** @@ -3706,10 +3706,16 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; \endrst */ #define FMT_VARIADIC(ReturnType, func, ...) \ - FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__) + FMT_VARIADIC_(,char, ReturnType, func, return func, __VA_ARGS__) + +#define FMT_VARIADIC_CONST(ReturnType, func, ...) \ + FMT_VARIADIC_(const, char, ReturnType, func, return func, __VA_ARGS__) #define FMT_VARIADIC_W(ReturnType, func, ...) \ - FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__) + FMT_VARIADIC_(,wchar_t, ReturnType, func, return func, __VA_ARGS__) + +#define FMT_VARIADIC_CONST_W(ReturnType, func, ...) \ + FMT_VARIADIC_(const, wchar_t, ReturnType, func, return func, __VA_ARGS__) #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id) diff --git a/test/format-test.cc b/test/format-test.cc index 7160972ed5e0..03b1f326f014 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1630,6 +1630,24 @@ TEST(FormatTest, FormatMessageExample) { format_message(42, "{} happened", "something")); } +class test_class +{ +public: + std::string format_message(int id, const char *format,const fmt::ArgList &args) const { + MemoryWriter w; + w.write("[{}] ", id); + w.write(format, args); + return w.str(); + } + FMT_VARIADIC_CONST(std::string, format_message, int, const char *) +}; + +TEST(FormatTest, ConstFormatMessage) { + test_class c; + EXPECT_EQ("[42] something happened", + c.format_message(42, "{} happened", "something")); +} + #if FMT_USE_VARIADIC_TEMPLATES template void print_error(const char *file, int line, const char *format, From 8b6f3d732c4a0cc9b8e23b6272e62989e63180d6 Mon Sep 17 00:00:00 2001 From: "LUDA-PC\\ludek.vodicka" Date: Sun, 22 Oct 2017 16:46:06 +0200 Subject: [PATCH 2/3] updated code formatting --- fmt/format.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index 8ad531d510d8..7748bfac8370 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -3647,7 +3647,7 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; #else // Defines a wrapper for a function taking __VA_ARGS__ arguments // and n additional arguments of arbitrary types. -# define FMT_WRAP(Const,Char, ReturnType, func, call, n, ...) \ +# define FMT_WRAP(Const, Char, ReturnType, func, call, n, ...) \ template \ inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ FMT_GEN(n, FMT_MAKE_ARG)) Const { \ @@ -3657,7 +3657,7 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \ } -# define FMT_VARIADIC_(Const,Char, ReturnType, func, call, ...) \ +# define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \ inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) Const { \ call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \ } \ @@ -3706,13 +3706,13 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; \endrst */ #define FMT_VARIADIC(ReturnType, func, ...) \ - FMT_VARIADIC_(,char, ReturnType, func, return func, __VA_ARGS__) + FMT_VARIADIC_(, char, ReturnType, func, return func, __VA_ARGS__) #define FMT_VARIADIC_CONST(ReturnType, func, ...) \ FMT_VARIADIC_(const, char, ReturnType, func, return func, __VA_ARGS__) #define FMT_VARIADIC_W(ReturnType, func, ...) \ - FMT_VARIADIC_(,wchar_t, ReturnType, func, return func, __VA_ARGS__) + FMT_VARIADIC_(, wchar_t, ReturnType, func, return func, __VA_ARGS__) #define FMT_VARIADIC_CONST_W(ReturnType, func, ...) \ FMT_VARIADIC_(const, wchar_t, ReturnType, func, return func, __VA_ARGS__) From a3a49a48e6a127c62c6b35f8753927a26c0ace4d Mon Sep 17 00:00:00 2001 From: "LUDA-PC\\ludek.vodicka" Date: Sun, 22 Oct 2017 17:01:26 +0200 Subject: [PATCH 3/3] updated tabs to spaces --- test/format-test.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/format-test.cc b/test/format-test.cc index 03b1f326f014..f512ef489178 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1633,19 +1633,19 @@ TEST(FormatTest, FormatMessageExample) { class test_class { public: - std::string format_message(int id, const char *format,const fmt::ArgList &args) const { - MemoryWriter w; - w.write("[{}] ", id); - w.write(format, args); - return w.str(); - } - FMT_VARIADIC_CONST(std::string, format_message, int, const char *) + std::string format_message(int id, const char *format,const fmt::ArgList &args) const { + MemoryWriter w; + w.write("[{}] ", id); + w.write(format, args); + return w.str(); + } + FMT_VARIADIC_CONST(std::string, format_message, int, const char *) }; TEST(FormatTest, ConstFormatMessage) { - test_class c; - EXPECT_EQ("[42] something happened", - c.format_message(42, "{} happened", "something")); + test_class c; + EXPECT_EQ("[42] something happened", + c.format_message(42, "{} happened", "something")); } #if FMT_USE_VARIADIC_TEMPLATES