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; }
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user