From 79d8f5990696d09180446f87d57190f6e04a3007 Mon Sep 17 00:00:00 2001 From: vitaut Date: Tue, 8 Sep 2015 08:36:20 -0700 Subject: [PATCH] Implement formatting of objects with (s)printf. --- format.cc | 6 ++++++ test/format-test.cc | 13 ------------- test/printf-test.cc | 4 ++++ test/util.h | 13 +++++++++++++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/format.cc b/format.cc index 6c0905b0..62b54d6d 100644 --- a/format.cc +++ b/format.cc @@ -523,6 +523,12 @@ class PrintfArgFormatter : } *out = static_cast(value); } + + void visit_custom(Arg::CustomValue c) { + BasicFormatter formatter(ArgList(), this->writer()); + const char *format = "}"; + c.format(&formatter, c.value, &format); + } }; } // namespace internal } // namespace fmt diff --git a/test/format-test.cc b/test/format-test.cc index 35d96b45..18498524 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -148,19 +148,6 @@ TEST(CStringRefTest, Ctor) { EXPECT_STREQ("defg", CStringRef(std::string("defg")).c_str()); } -class TestString { - private: - std::string value_; - - public: - explicit TestString(const char *value = "") : value_(value) {} - - friend std::ostream &operator<<(std::ostream &os, const TestString &s) { - os << s.value_; - return os; - } -}; - #if FMT_USE_TYPE_TRAITS TEST(WriterTest, NotCopyConstructible) { EXPECT_FALSE(std::is_copy_constructible >::value); diff --git a/test/printf-test.cc b/test/printf-test.cc index ec29bbe1..e942d6fe 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -426,6 +426,10 @@ TEST(PrintfTest, Pointer) { EXPECT_PRINTF(fmt::format("{}", p), "%p", p); } +TEST(PrintfTest, Custom) { + EXPECT_PRINTF("abc", "%s", TestString("abc")); +} + TEST(PrintfTest, Location) { // TODO: test %n } diff --git a/test/util.h b/test/util.h index a0d485eb..0b41fbbb 100644 --- a/test/util.h +++ b/test/util.h @@ -67,3 +67,16 @@ inline FILE *safe_fopen(const char *filename, const char *mode) { return std::fopen(filename, mode); #endif } + +class TestString { + private: + std::string value_; + + public: + explicit TestString(const char *value = "") : value_(value) {} + + friend std::ostream &operator<<(std::ostream &os, const TestString &s) { + os << s.value_; + return os; + } +};