Improve API and make it work on older GCC.

This commit is contained in:
Victor Zverovich 2012-12-16 10:03:19 -08:00
parent fcf476bb02
commit 93a970945e
2 changed files with 20 additions and 5 deletions

View File

@ -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();
}
};
}

View File

@ -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