diff --git a/README.rst b/README.rst index fc1b9377..0813aa86 100644 --- a/README.rst +++ b/README.rst @@ -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 ReportError(const char *format) { - return fmt::ActiveFormatter(format); + fmt::TempFormatter ReportError(const char *format) { + return fmt::TempFormatter(format); } ReportError("File not found: {0}") << path; diff --git a/format.h b/format.h index 2073895c..c9c8832b 100644 --- a/format.h +++ b/format.h @@ -303,9 +303,6 @@ class Formatter { std::string str() const { return std::string(&buffer_[0], buffer_.size()); } }; -template -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 -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 Format(const char *format) { - return ActiveFormatter(format); +inline TempFormatter Format(const char *format) { + return TempFormatter(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 Print(const char *format) { - return ActiveFormatter(format); +inline TempFormatter Print(const char *format) { + return TempFormatter(format); } } diff --git a/format_test.cc b/format_test.cc index 23c97278..ea6dd21e 100644 --- a/format_test.cc +++ b/format_test.cc @@ -703,20 +703,20 @@ struct CountCalls { } }; -TEST(ActiveFormatterTest, Action) { +TEST(TempFormatterTest, Action) { int num_calls = 0; { - fmt::ActiveFormatter af("test", CountCalls(num_calls)); + fmt::TempFormatter 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 af("{0", CountCalls(num_calls)), + fmt::TempFormatter 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 &af = fmt::Format("{0}"); - const_cast&>(af) << std::string("test"); - // String object passed as an argument to ActiveFormatter has + const fmt::TempFormatter &af = fmt::Format("{0}"); + const_cast&>(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 ReportError(const char *format) { - return fmt::ActiveFormatter(format); +fmt::TempFormatter ReportError(const char *format) { + return fmt::TempFormatter(format); } -TEST(ActiveFormatterTest, Example) { +TEST(TempFormatterTest, Example) { std::string path = "somefile"; ReportError("File not found: {0}") << path; }