Use ArgInfo instead of Arg where possible.

This commit is contained in:
Victor Zverovich 2014-04-22 08:58:54 -07:00
parent 5b8126f84d
commit 656a8378d1
2 changed files with 24 additions and 16 deletions

View File

@ -388,7 +388,7 @@ unsigned fmt::BasicFormatter<Char>::ParseUInt(const Char *&s) const {
} }
template <typename Char> template <typename Char>
inline const typename fmt::BasicFormatter<Char>::Arg inline const typename fmt::BasicFormatter<Char>::ArgInfo
&fmt::BasicFormatter<Char>::ParseArgIndex(const Char *&s) { &fmt::BasicFormatter<Char>::ParseArgIndex(const Char *&s) {
unsigned arg_index = 0; unsigned arg_index = 0;
if (*s < '0' || *s > '9') { if (*s < '0' || *s > '9') {
@ -409,11 +409,11 @@ inline const typename fmt::BasicFormatter<Char>::Arg
} }
if (arg_index >= args_.size()) if (arg_index >= args_.size())
ReportError(s, "argument index is out of range in format"); ReportError(s, "argument index is out of range in format");
return *args_[arg_index]; return args_[arg_index];
} }
template <typename Char> template <typename Char>
void fmt::BasicFormatter<Char>::CheckSign(const Char *&s, const Arg &arg) { void fmt::BasicFormatter<Char>::CheckSign(const Char *&s, const ArgInfo &arg) {
char sign = static_cast<char>(*s); char sign = static_cast<char>(*s);
if (arg.type > LAST_NUMERIC_TYPE) { if (arg.type > LAST_NUMERIC_TYPE) {
ReportError(s, ReportError(s,
@ -446,7 +446,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
num_open_braces_= 1; num_open_braces_= 1;
writer.buffer_.append(start, s - 1); writer.buffer_.append(start, s - 1);
const Arg &arg = ParseArgIndex(s); const ArgInfo &arg = ParseArgIndex(s);
FormatSpec spec; FormatSpec spec;
int precision = -1; int precision = -1;
@ -537,7 +537,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
} else if (*s == '{') { } else if (*s == '{') {
++s; ++s;
++num_open_braces_; ++num_open_braces_;
const Arg &precision_arg = ParseArgIndex(s); const ArgInfo &precision_arg = ParseArgIndex(s);
ULongLong value = 0; ULongLong value = 0;
switch (precision_arg.type) { switch (precision_arg.type) {
case INT: case INT:
@ -702,11 +702,11 @@ template void fmt::BasicFormatter<char>::ReportError(
template unsigned fmt::BasicFormatter<char>::ParseUInt(const char *&s) const; template unsigned fmt::BasicFormatter<char>::ParseUInt(const char *&s) const;
template const fmt::BasicFormatter<char>::Arg template const fmt::BasicFormatter<char>::ArgInfo
&fmt::BasicFormatter<char>::ParseArgIndex(const char *&s); &fmt::BasicFormatter<char>::ParseArgIndex(const char *&s);
template void fmt::BasicFormatter<char>::CheckSign( template void fmt::BasicFormatter<char>::CheckSign(
const char *&s, const Arg &arg); const char *&s, const ArgInfo &arg);
template void fmt::BasicFormatter<char>::DoFormat(); template void fmt::BasicFormatter<char>::DoFormat();
@ -732,11 +732,11 @@ template void fmt::BasicFormatter<wchar_t>::ReportError(
template unsigned fmt::BasicFormatter<wchar_t>::ParseUInt( template unsigned fmt::BasicFormatter<wchar_t>::ParseUInt(
const wchar_t *&s) const; const wchar_t *&s) const;
template const fmt::BasicFormatter<wchar_t>::Arg template const fmt::BasicFormatter<wchar_t>::ArgInfo
&fmt::BasicFormatter<wchar_t>::ParseArgIndex(const wchar_t *&s); &fmt::BasicFormatter<wchar_t>::ParseArgIndex(const wchar_t *&s);
template void fmt::BasicFormatter<wchar_t>::CheckSign( template void fmt::BasicFormatter<wchar_t>::CheckSign(
const wchar_t *&s, const Arg &arg); const wchar_t *&s, const ArgInfo &arg);
template void fmt::BasicFormatter<wchar_t>::DoFormat(); template void fmt::BasicFormatter<wchar_t>::DoFormat();

View File

@ -1031,7 +1031,15 @@ class BasicFormatter {
}; };
// A wrapper around a format argument used to ensure that the formatting // 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 { class Arg : public ArgInfo {
private: private:
// This method is private to disallow formatting of arbitrary pointers. // 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 // Format is called here to make sure that a referred object is
// still alive, for example: // 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 // Here an Arg object refers to a temporary std::string which is
// destroyed at the end of the statement. Since the string object is // destroyed at the end of the statement. Since the string object is
@ -1132,7 +1140,7 @@ class BasicFormatter {
}; };
enum { NUM_INLINE_ARGS = 10 }; enum { NUM_INLINE_ARGS = 10 };
internal::Array<const Arg*, NUM_INLINE_ARGS> args_; // Format arguments. internal::Array<ArgInfo, NUM_INLINE_ARGS> args_; // Format arguments.
const Char *format_; // Format string. const Char *format_; // Format string.
int num_open_braces_; int num_open_braces_;
@ -1153,9 +1161,9 @@ class BasicFormatter {
unsigned ParseUInt(const Char *&s) const; unsigned ParseUInt(const Char *&s) const;
// Parses argument index and returns an argument with this index. // 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, // Parses the format string and performs the actual formatting,
// writing the output to writer_. // writing the output to writer_.
@ -1194,7 +1202,7 @@ class BasicFormatter {
// TODO: don't copy arguments // TODO: don't copy arguments
args_.reserve(args.size()); args_.reserve(args.size());
for (const Arg &arg: args) for (const Arg &arg: args)
args_.push_back(&arg); args_.push_back(arg);
} }
#endif #endif
@ -1211,7 +1219,7 @@ class BasicFormatter {
// Feeds an argument to a formatter. // Feeds an argument to a formatter.
BasicFormatter &operator<<(const Arg &arg) { BasicFormatter &operator<<(const Arg &arg) {
arg.formatter = this; arg.formatter = this;
args_.push_back(&arg); args_.push_back(arg);
return *this; return *this;
} }