Implement formatting of objects with (s)printf.

This commit is contained in:
vitaut 2015-09-08 08:36:20 -07:00
parent 59155abbf3
commit 79d8f59906
4 changed files with 23 additions and 13 deletions

View File

@ -523,6 +523,12 @@ class PrintfArgFormatter :
}
*out = static_cast<Char>(value);
}
void visit_custom(Arg::CustomValue c) {
BasicFormatter<Char> formatter(ArgList(), this->writer());
const char *format = "}";
c.format(&formatter, c.value, &format);
}
};
} // namespace internal
} // namespace fmt

View File

@ -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<BasicWriter<char> >::value);

View File

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

View File

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