diff --git a/format.h b/format.h index 5439435b..7a5ab559 100644 --- a/format.h +++ b/format.h @@ -712,9 +712,9 @@ struct NonZero<0> { enum { VALUE = 1 }; }; -// The value of a formatting argument. It is a POD type to allow storage in +// A formatting argument. It is a POD type to allow storage in // internal::MemoryBuffer. -struct Value { +struct Arg { template struct StringValue { const Char *value; @@ -755,8 +755,6 @@ struct Value { Type type; }; -typedef struct Value Arg; // TODO: remove - template struct None {}; @@ -807,9 +805,9 @@ struct EnableIf {}; template struct EnableIf { typedef T type; }; -// Makes a Value object from any type. +// Makes an Arg object from any type. template -class MakeValue : public Value { +class MakeValue : public Arg { // TODO: rename to MakeArg private: // The following two methods are private to disallow formatting of // arbitrary pointers. If you want to output a pointer cast it to @@ -1035,7 +1033,7 @@ class ArgVisitor { case Arg::CHAR: return FMT_DISPATCH(visit_char(arg.int_value)); case Arg::CSTRING: { - Value::StringValue str = arg.string; + Arg::StringValue str = arg.string; str.size = 0; return FMT_DISPATCH(visit_string(str)); } @@ -1066,7 +1064,7 @@ class ArgList { // To reduce compiled code size per formatting function call, types of first // MAX_PACKED_ARGS arguments are passed in the types_ field. uint64_t types_; - const internal::Value *values_; + const internal::Arg *args_; internal::Arg::Type type(unsigned index) const { unsigned shift = index * 4; @@ -1080,31 +1078,29 @@ class ArgList { enum { MAX_PACKED_ARGS = 16 }; ArgList() : types_(0) {} - ArgList(ULongLong types, const internal::Value *values) - : types_(types), values_(values) {} + ArgList(ULongLong types, const internal::Arg *args) + : types_(types), args_(args) {} /** Returns the argument at specified index. */ internal::Arg operator[](unsigned index) const { using internal::Arg; Arg arg; - if (index >= MAX_PACKED_ARGS) { - if (type(MAX_PACKED_ARGS - 1) == Arg::NONE) { - arg.type = Arg::NONE; - return arg; - } - for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) { - if (values_[i].type == Arg::NONE) - return values_[i]; - } - return values_[index]; + if (index < MAX_PACKED_ARGS) { + Arg::Type arg_type = type(index); + if (arg_type != Arg::NONE) + arg = args_[index]; + arg.type = arg_type; + return arg; } - Arg::Type arg_type = type(index); - if (arg_type != Arg::NONE) { - internal::Value &value = arg; - value = values_[index]; + if (type(MAX_PACKED_ARGS - 1) == Arg::NONE) { + arg.type = Arg::NONE; + return arg; } - arg.type = arg_type; - return arg; + for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) { + if (args_[i].type == Arg::NONE) + return args_[i]; + } + return args_[index]; } }; @@ -1463,11 +1459,11 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { # define FMT_VARIADIC_VOID(func, arg_type) \ template \ void func(arg_type arg1, const Args & ... args) { \ - const fmt::internal::Value values[ \ + const fmt::internal::Arg array[ \ fmt::internal::NonZero::VALUE] = { \ fmt::internal::MakeValue(args)... \ }; \ - func(arg1, ArgList(fmt::internal::make_type(args...), values)); \ + func(arg1, ArgList(fmt::internal::make_type(args...), array)); \ } // Defines a variadic constructor. @@ -1475,11 +1471,11 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { template \ ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \ using fmt::internal::MakeValue; \ - const fmt::internal::Value values[ \ + const fmt::internal::Arg array[ \ fmt::internal::NonZero::VALUE] = { \ MakeValue(args)... \ }; \ - func(arg0, arg1, ArgList(fmt::internal::make_type(args...), values)); \ + func(arg0, arg1, ArgList(fmt::internal::make_type(args...), array)); \ } #else @@ -1492,9 +1488,9 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { # define FMT_WRAP1(func, arg_type, n) \ template \ inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::Value vals[] = {FMT_GEN(n, FMT_MAKE_REF)}; \ + const fmt::internal::Arg args[] = {FMT_GEN(n, FMT_MAKE_REF)}; \ func(arg1, fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), vals)); \ + fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), args)); \ } // Emulates a variadic function returning void on a pre-C++11 compiler. @@ -1509,9 +1505,9 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \ template \ ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::Value vals[] = {FMT_GEN(n, FMT_MAKE_REF)}; \ + const fmt::internal::Arg args[] = {FMT_GEN(n, FMT_MAKE_REF)}; \ func(arg0, arg1, fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), vals)); \ + fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), args)); \ } // Emulates a variadic constructor on a pre-C++11 compiler. @@ -2304,10 +2300,8 @@ template void format(BasicFormatter &f, const Char *&format_str, const T &value) { std::basic_ostringstream os; os << value; - internal::Arg arg; - internal::Value &arg_value = arg; std::basic_string str = os.str(); - arg_value = internal::MakeValue(str); + internal::Arg arg = internal::MakeValue(str); arg.type = static_cast( internal::MakeValue::type(str)); format_str = f.format(format_str, arg); @@ -2607,12 +2601,12 @@ inline void format_decimal(char *&buffer, T value) { namespace fmt { namespace internal { -inline void set_types(Value *) {} +inline void set_types(Arg *) {} template -inline void set_types(Value *values, const T &arg, const Args & ... tail) { - values->type = static_cast(MakeValue::type(arg)); - set_types(values + 1, tail...); +inline void set_types(Arg *args, const T &arg, const Args & ... tail) { + args->type = static_cast(MakeValue::type(arg)); + set_types(args + 1, tail...); } } } @@ -2639,9 +2633,9 @@ inline void set_types(Value *values, const T &arg, const Args & ... tail) { template \ inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::Value vals[] = {FMT_GEN(n, FMT_MAKE_REF_##Char)}; \ + const fmt::internal::Arg args[] = {FMT_GEN(n, FMT_MAKE_REF_##Char)}; \ call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), vals)); \ + fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), args)); \ } # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \ diff --git a/test/util-test.cc b/test/util-test.cc index 3bae2798..3bca3d46 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -50,7 +50,6 @@ using fmt::StringRef; using fmt::internal::Arg; -using fmt::internal::Value; using fmt::Buffer; using fmt::internal::MemoryBuffer; @@ -67,9 +66,7 @@ std::basic_ostream &operator<<(std::basic_ostream &os, Test) { template Arg make_arg(const T &value) { - Arg arg = Arg(); - Value &arg_value = arg; - arg_value = fmt::internal::MakeValue(value); + Arg arg = fmt::internal::MakeValue(value); arg.type = static_cast( fmt::internal::MakeValue::type(value)); return arg; @@ -409,7 +406,7 @@ struct ArgInfo; #define ARG_INFO(type_code, Type, field) \ template <> \ struct ArgInfo { \ - static Type get(const Value &value) { return value.field; } \ + static Type get(const Arg &arg) { return arg.field; } \ }; ARG_INFO(INT, int, int_value); @@ -426,9 +423,9 @@ ARG_INFO(POINTER, const void *, pointer); ARG_INFO(CUSTOM, Arg::CustomValue, custom); #define CHECK_ARG_INFO(Type, field, value) { \ - Value arg_value = {}; \ - arg_value.field = value; \ - EXPECT_EQ(value, ArgInfo::get(arg_value)); \ + Arg arg = {}; \ + arg.field = value; \ + EXPECT_EQ(value, ArgInfo::get(arg)); \ } TEST(ArgTest, ArgInfo) { @@ -445,9 +442,9 @@ TEST(ArgTest, ArgInfo) { CHECK_ARG_INFO(WSTRING, wstring.value, WSTR); int p = 0; CHECK_ARG_INFO(POINTER, pointer, &p); - Value value = {}; - value.custom.value = &p; - EXPECT_EQ(&p, ArgInfo::get(value).value); + Arg arg = {}; + arg.custom.value = &p; + EXPECT_EQ(&p, ArgInfo::get(arg).value); } #define EXPECT_ARG_(Char, type_code, MakeArgType, ExpectedType, value) { \