mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 03:17:53 +00:00
Rename Active Formatter to TempFormatter.
This commit is contained in:
parent
e0f85c4431
commit
059934fd10
@ -60,7 +60,7 @@ An object of any user-defined type for which there is an overloaded
|
||||
std::string s = str(fmt::Format("The date is {0}") << Date(2012, 12, 9));
|
||||
// s == "The date is 2012-12-9"
|
||||
|
||||
You can use ``fmt::ActiveFormatter`` to create your own functions
|
||||
You can use ``fmt::TempFormatter`` to create your own functions
|
||||
similar to ``fmt::Format`` and ``fmt::Print`` with an arbitrary action
|
||||
performed when formatting is complete:
|
||||
|
||||
@ -73,8 +73,8 @@ performed when formatting is complete:
|
||||
};
|
||||
|
||||
// Formats an error message and prints it to std::cerr.
|
||||
fmt::ActiveFormatter<PrintError> ReportError(const char *format) {
|
||||
return fmt::ActiveFormatter<PrintError>(format);
|
||||
fmt::TempFormatter<PrintError> ReportError(const char *format) {
|
||||
return fmt::TempFormatter<PrintError>(format);
|
||||
}
|
||||
|
||||
ReportError("File not found: {0}") << path;
|
||||
|
27
format.h
27
format.h
@ -303,9 +303,6 @@ class Formatter {
|
||||
std::string str() const { return std::string(&buffer_[0], buffer_.size()); }
|
||||
};
|
||||
|
||||
template <typename Action>
|
||||
class ActiveFormatter;
|
||||
|
||||
namespace internal {
|
||||
|
||||
// This is a transient object that normally exists only as a temporary
|
||||
@ -410,19 +407,19 @@ inline internal::ArgInserter Formatter::operator()(const char *format) {
|
||||
}
|
||||
|
||||
// A formatter with an action performed when formatting is complete.
|
||||
// This is a transient object that normally exists only as a temporary
|
||||
// returned by one of the formatting functions.
|
||||
// Objects of this class normally exist only as temporaries returned
|
||||
// by one of the formatting functions, thus the name.
|
||||
template <typename Action>
|
||||
class ActiveFormatter : public internal::ArgInserter {
|
||||
class TempFormatter : public internal::ArgInserter {
|
||||
private:
|
||||
Formatter formatter_;
|
||||
Action action_;
|
||||
|
||||
// Forbid copying other than from a temporary. Do not implement.
|
||||
ActiveFormatter(ActiveFormatter &);
|
||||
TempFormatter(TempFormatter &);
|
||||
|
||||
// Do not implement.
|
||||
ActiveFormatter& operator=(const ActiveFormatter &);
|
||||
TempFormatter& operator=(const TempFormatter &);
|
||||
|
||||
struct Proxy {
|
||||
const char *format;
|
||||
@ -436,17 +433,17 @@ class ActiveFormatter : public internal::ArgInserter {
|
||||
// Action should be an unary function object that takes a const
|
||||
// reference to Formatter as an argument. See Ignore and Write
|
||||
// for examples of action classes.
|
||||
explicit ActiveFormatter(const char *format, Action a = Action())
|
||||
explicit TempFormatter(const char *format, Action a = Action())
|
||||
: action_(a) {
|
||||
Init(formatter_, format);
|
||||
}
|
||||
|
||||
ActiveFormatter(const Proxy &p)
|
||||
TempFormatter(const Proxy &p)
|
||||
: ArgInserter(0), action_(p.action) {
|
||||
Init(formatter_, p.format);
|
||||
}
|
||||
|
||||
~ActiveFormatter() {
|
||||
~TempFormatter() {
|
||||
if (formatter())
|
||||
action_(*Format());
|
||||
}
|
||||
@ -466,8 +463,8 @@ struct Ignore {
|
||||
// Formats a string.
|
||||
// Example:
|
||||
// std::string s = str(Format("Elapsed time: {0:.2f} seconds") << 1.23);
|
||||
inline ActiveFormatter<Ignore> Format(const char *format) {
|
||||
return ActiveFormatter<Ignore>(format);
|
||||
inline TempFormatter<Ignore> Format(const char *format) {
|
||||
return TempFormatter<Ignore>(format);
|
||||
}
|
||||
|
||||
// A formatting action that writes formatted output to stdout.
|
||||
@ -480,8 +477,8 @@ struct Write {
|
||||
// Formats a string and prints it to stdout.
|
||||
// Example:
|
||||
// Print("Elapsed time: {0:.2f} seconds") << 1.23;
|
||||
inline ActiveFormatter<Write> Print(const char *format) {
|
||||
return ActiveFormatter<Write>(format);
|
||||
inline TempFormatter<Write> Print(const char *format) {
|
||||
return TempFormatter<Write>(format);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -703,20 +703,20 @@ struct CountCalls {
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ActiveFormatterTest, Action) {
|
||||
TEST(TempFormatterTest, Action) {
|
||||
int num_calls = 0;
|
||||
{
|
||||
fmt::ActiveFormatter<CountCalls> af("test", CountCalls(num_calls));
|
||||
fmt::TempFormatter<CountCalls> af("test", CountCalls(num_calls));
|
||||
EXPECT_EQ(0, num_calls);
|
||||
}
|
||||
EXPECT_EQ(1, num_calls);
|
||||
}
|
||||
|
||||
TEST(ActiveFormatterTest, ActionNotCalledOnError) {
|
||||
TEST(TempFormatterTest, ActionNotCalledOnError) {
|
||||
int num_calls = 0;
|
||||
{
|
||||
EXPECT_THROW(
|
||||
fmt::ActiveFormatter<CountCalls> af("{0", CountCalls(num_calls)),
|
||||
fmt::TempFormatter<CountCalls> af("{0", CountCalls(num_calls)),
|
||||
FormatError);
|
||||
}
|
||||
EXPECT_EQ(0, num_calls);
|
||||
@ -726,12 +726,12 @@ TEST(ActiveFormatterTest, ActionNotCalledOnError) {
|
||||
// require an accessible copy constructor when binding a temporary to
|
||||
// a const reference.
|
||||
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
|
||||
TEST(ActiveFormatterTest, ArgLifetime) {
|
||||
TEST(TempFormatterTest, ArgLifetime) {
|
||||
// The following code is for testing purposes only. It is a definite abuse
|
||||
// of the API and shouldn't be used in real applications.
|
||||
const fmt::ActiveFormatter<fmt::Ignore> &af = fmt::Format("{0}");
|
||||
const_cast<fmt::ActiveFormatter<fmt::Ignore>&>(af) << std::string("test");
|
||||
// String object passed as an argument to ActiveFormatter has
|
||||
const fmt::TempFormatter<fmt::Ignore> &af = fmt::Format("{0}");
|
||||
const_cast<fmt::TempFormatter<fmt::Ignore>&>(af) << std::string("test");
|
||||
// String object passed as an argument to TempFormatter has
|
||||
// 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.
|
||||
@ -744,11 +744,11 @@ struct PrintError {
|
||||
}
|
||||
};
|
||||
|
||||
fmt::ActiveFormatter<PrintError> ReportError(const char *format) {
|
||||
return fmt::ActiveFormatter<PrintError>(format);
|
||||
fmt::TempFormatter<PrintError> ReportError(const char *format) {
|
||||
return fmt::TempFormatter<PrintError>(format);
|
||||
}
|
||||
|
||||
TEST(ActiveFormatterTest, Example) {
|
||||
TEST(TempFormatterTest, Example) {
|
||||
std::string path = "somefile";
|
||||
ReportError("File not found: {0}") << path;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user