mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-10 21:20:07 +00:00
Improve API and make it work on older GCC.
This commit is contained in:
parent
fcf476bb02
commit
93a970945e
24
format.h
24
format.h
@ -349,6 +349,16 @@ class ArgInserter {
|
|||||||
|
|
||||||
void ResetFormatter() const { formatter_ = 0; }
|
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:
|
public:
|
||||||
~ArgInserter() {
|
~ArgInserter() {
|
||||||
if (formatter_)
|
if (formatter_)
|
||||||
@ -362,14 +372,20 @@ class ArgInserter {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator Proxy() {
|
||||||
|
Formatter *f = formatter_;
|
||||||
|
formatter_ = 0;
|
||||||
|
return Proxy(f);
|
||||||
|
}
|
||||||
|
|
||||||
// Performs formatting and returns a C string with the output.
|
// Performs formatting and returns a C string with the output.
|
||||||
friend const char *c_str(const ArgInserter &af) {
|
friend const char *c_str(Proxy p) {
|
||||||
return af.Format()->c_str();
|
return Format(p)->c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs formatting and returns a std::string with the output.
|
// Performs formatting and returns a std::string with the output.
|
||||||
friend std::string str(const ArgInserter &af) {
|
friend std::string str(Proxy p) {
|
||||||
return af.Format()->str();
|
return Format(p)->str();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -731,7 +731,6 @@ TEST(ActiveFormatterTest, ArgLifetime) {
|
|||||||
// been destroyed, but ArgInserter dtor hasn't been called yet.
|
// been destroyed, but ArgInserter dtor hasn't been called yet.
|
||||||
// But that's OK since the Arg's dtor takes care of this and
|
// But that's OK since the Arg's dtor takes care of this and
|
||||||
// calls Format.
|
// calls Format.
|
||||||
EXPECT_EQ("test", str(af));
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user