Commit 059934fd authored by Victor Zverovich's avatar Victor Zverovich

Rename Active Formatter to TempFormatter.

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