From 93a970945e8ce32cc26b47d7bb196c9a01295242 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 16 Dec 2012 10:03:19 -0800 Subject: [PATCH] Improve API and make it work on older GCC. --- format.h | 24 ++++++++++++++++++++---- format_test.cc | 1 - 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/format.h b/format.h index 316b057a..2073895c 100644 --- a/format.h +++ b/format.h @@ -349,6 +349,16 @@ class ArgInserter { void ResetFormatter() const { formatter_ = 0; } + struct Proxy { + Formatter *formatter; + explicit Proxy(Formatter *f) : formatter(f) {} + }; + + static Formatter *Format(Proxy p) { + p.formatter->Format(); + return p.formatter; + } + public: ~ArgInserter() { if (formatter_) @@ -362,14 +372,20 @@ class ArgInserter { return *this; } + operator Proxy() { + Formatter *f = formatter_; + formatter_ = 0; + return Proxy(f); + } + // Performs formatting and returns a C string with the output. - friend const char *c_str(const ArgInserter &af) { - return af.Format()->c_str(); + friend const char *c_str(Proxy p) { + return Format(p)->c_str(); } // Performs formatting and returns a std::string with the output. - friend std::string str(const ArgInserter &af) { - return af.Format()->str(); + friend std::string str(Proxy p) { + return Format(p)->str(); } }; } diff --git a/format_test.cc b/format_test.cc index 4e7ad837..4b86bd3d 100644 --- a/format_test.cc +++ b/format_test.cc @@ -731,7 +731,6 @@ TEST(ActiveFormatterTest, ArgLifetime) { // been destroyed, but ArgInserter dtor hasn't been called yet. // But that's OK since the Arg's dtor takes care of this and // calls Format. - EXPECT_EQ("test", str(af)); } #endif