From 656a8378d1f63a417c53f68f36908cc9a39d19f7 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 22 Apr 2014 08:58:54 -0700 Subject: [PATCH] Use ArgInfo instead of Arg where possible. --- format.cc | 18 +++++++++--------- format.h | 22 +++++++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/format.cc b/format.cc index 96fc0eb9..20ab9bb0 100644 --- a/format.cc +++ b/format.cc @@ -388,7 +388,7 @@ unsigned fmt::BasicFormatter::ParseUInt(const Char *&s) const { } template -inline const typename fmt::BasicFormatter::Arg +inline const typename fmt::BasicFormatter::ArgInfo &fmt::BasicFormatter::ParseArgIndex(const Char *&s) { unsigned arg_index = 0; if (*s < '0' || *s > '9') { @@ -409,11 +409,11 @@ inline const typename fmt::BasicFormatter::Arg } if (arg_index >= args_.size()) ReportError(s, "argument index is out of range in format"); - return *args_[arg_index]; + return args_[arg_index]; } template -void fmt::BasicFormatter::CheckSign(const Char *&s, const Arg &arg) { +void fmt::BasicFormatter::CheckSign(const Char *&s, const ArgInfo &arg) { char sign = static_cast(*s); if (arg.type > LAST_NUMERIC_TYPE) { ReportError(s, @@ -446,7 +446,7 @@ void fmt::BasicFormatter::DoFormat() { num_open_braces_= 1; writer.buffer_.append(start, s - 1); - const Arg &arg = ParseArgIndex(s); + const ArgInfo &arg = ParseArgIndex(s); FormatSpec spec; int precision = -1; @@ -537,7 +537,7 @@ void fmt::BasicFormatter::DoFormat() { } else if (*s == '{') { ++s; ++num_open_braces_; - const Arg &precision_arg = ParseArgIndex(s); + const ArgInfo &precision_arg = ParseArgIndex(s); ULongLong value = 0; switch (precision_arg.type) { case INT: @@ -702,11 +702,11 @@ template void fmt::BasicFormatter::ReportError( template unsigned fmt::BasicFormatter::ParseUInt(const char *&s) const; -template const fmt::BasicFormatter::Arg +template const fmt::BasicFormatter::ArgInfo &fmt::BasicFormatter::ParseArgIndex(const char *&s); template void fmt::BasicFormatter::CheckSign( - const char *&s, const Arg &arg); + const char *&s, const ArgInfo &arg); template void fmt::BasicFormatter::DoFormat(); @@ -732,11 +732,11 @@ template void fmt::BasicFormatter::ReportError( template unsigned fmt::BasicFormatter::ParseUInt( const wchar_t *&s) const; -template const fmt::BasicFormatter::Arg +template const fmt::BasicFormatter::ArgInfo &fmt::BasicFormatter::ParseArgIndex(const wchar_t *&s); template void fmt::BasicFormatter::CheckSign( - const wchar_t *&s, const Arg &arg); + const wchar_t *&s, const ArgInfo &arg); template void fmt::BasicFormatter::DoFormat(); diff --git a/format.h b/format.h index bc94df2b..e837ba4b 100644 --- a/format.h +++ b/format.h @@ -1031,7 +1031,15 @@ class BasicFormatter { }; // A wrapper around a format argument used to ensure that the formatting - // is performed before the argument is destroyed. + // is performed before the argument is destroyed. It is private so that + // its objects are only created by automatic conversions and not by users. + // Example: + // + // Format("{}") << std::string("test"); + // + // Here an Arg object that wraps a temporary string is automatically + // created. It triggers formatting when destroyed which makes sure that + // the temporary string is still alive at the time of the formatting. class Arg : public ArgInfo { private: // This method is private to disallow formatting of arbitrary pointers. @@ -1118,7 +1126,7 @@ class BasicFormatter { // Format is called here to make sure that a referred object is // still alive, for example: // - // Print("{0}") << std::string("test"); + // Print("{}") << std::string("test"); // // Here an Arg object refers to a temporary std::string which is // destroyed at the end of the statement. Since the string object is @@ -1132,7 +1140,7 @@ class BasicFormatter { }; enum { NUM_INLINE_ARGS = 10 }; - internal::Array args_; // Format arguments. + internal::Array args_; // Format arguments. const Char *format_; // Format string. int num_open_braces_; @@ -1153,9 +1161,9 @@ class BasicFormatter { unsigned ParseUInt(const Char *&s) const; // Parses argument index and returns an argument with this index. - const Arg &ParseArgIndex(const Char *&s); + const ArgInfo &ParseArgIndex(const Char *&s); - void CheckSign(const Char *&s, const Arg &arg); + void CheckSign(const Char *&s, const ArgInfo &arg); // Parses the format string and performs the actual formatting, // writing the output to writer_. @@ -1194,7 +1202,7 @@ class BasicFormatter { // TODO: don't copy arguments args_.reserve(args.size()); for (const Arg &arg: args) - args_.push_back(&arg); + args_.push_back(arg); } #endif @@ -1211,7 +1219,7 @@ class BasicFormatter { // Feeds an argument to a formatter. BasicFormatter &operator<<(const Arg &arg) { arg.formatter = this; - args_.push_back(&arg); + args_.push_back(arg); return *this; }