From b2a0d8914a05ce97ca21eb35e1e8306393e7c281 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 30 Dec 2016 09:25:01 -0800 Subject: [PATCH] Merge value and MakeValue --- fmt/format.h | 85 ++++++++++++++++++++++------------------------- test/util-test.cc | 3 +- 2 files changed, 41 insertions(+), 47 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index 14c5d7d1..f82713f3 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -1017,7 +1017,7 @@ template struct Null {}; // A helper class template to enable or disable overloads taking wide -// characters and strings in MakeValue. +// characters and strings in value's constructor. template struct WCharHelper { typedef Null Supported; @@ -1097,25 +1097,6 @@ struct CustomValue { FormatFunc format; }; -// A formatting argument value. -template -struct value { - union { - int int_value; - unsigned uint_value; - LongLong long_long_value; - ULongLong ulong_long_value; - double double_value; - long double long_double_value; - const void *pointer; - StringValue string; - StringValue sstring; - StringValue ustring; - StringValue tstring; - CustomValue custom; - }; -}; - template struct NamedArg; @@ -1175,10 +1156,25 @@ template <> constexpr Type gettype() { return POINTER; } template constexpr Type type() { return gettype::type>(); } -// Makes a format_arg object from any type. +// A formatting argument value. template -class MakeValue : public value { +class value { public: + union { + int int_value; + unsigned uint_value; + LongLong long_long_value; + ULongLong ulong_long_value; + double double_value; + long double long_double_value; + const void *pointer; + StringValue string; + StringValue sstring; + StringValue ustring; + StringValue tstring; + CustomValue custom; + }; + typedef typename Context::char_type Char; private: @@ -1188,21 +1184,21 @@ class MakeValue : public value { // of "[const] volatile char *" which is printed as bool by iostreams. // Do not implement! template - MakeValue(const T *value); + value(const T *value); template - MakeValue(T *value); + value(T *value); // The following methods are private to disallow formatting of wide // characters and strings into narrow strings as in // fmt::format("{}", L"test"); // To fix this, use a wide format string: fmt::format(L"{}", L"test"). #if !FMT_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED) - MakeValue(typename WCharHelper::Unsupported); + value(typename WCharHelper::Unsupported); #endif - MakeValue(typename WCharHelper::Unsupported); - MakeValue(typename WCharHelper::Unsupported); - MakeValue(typename WCharHelper::Unsupported); - MakeValue(typename WCharHelper::Unsupported); + value(typename WCharHelper::Unsupported); + value(typename WCharHelper::Unsupported); + value(typename WCharHelper::Unsupported); + value(typename WCharHelper::Unsupported); void set_string(StringRef str) { this->string.value = str.data(); @@ -1223,10 +1219,10 @@ class MakeValue : public value { } public: - MakeValue() {} + value() {} #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \ - MakeValue(Type value) { \ + value(Type value) { \ static_assert(internal::type() == internal::TYPE, "invalid type"); \ this->field = rhs; \ } @@ -1240,7 +1236,7 @@ class MakeValue : public value { FMT_MAKE_VALUE(int, int_value, INT) FMT_MAKE_VALUE(unsigned, uint_value, UINT) - MakeValue(long value) { + value(long value) { // To minimize the number of types we need to deal with, long is // translated either to int or to long long depending on its size. if (const_check(sizeof(long) == sizeof(int))) @@ -1249,7 +1245,7 @@ class MakeValue : public value { this->long_long_value = value; } - MakeValue(unsigned long value) { + value(unsigned long value) { if (const_check(sizeof(unsigned long) == sizeof(unsigned))) this->uint_value = static_cast(value); else @@ -1267,14 +1263,14 @@ class MakeValue : public value { #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) typedef typename WCharHelper::Supported WChar; - MakeValue(WChar value) { + value(WChar value) { static_assert(internal::type() == internal::CHAR, "invalid type"); this->int_value = value; } #endif #define FMT_MAKE_STR_VALUE(Type, TYPE) \ - MakeValue(Type value) { \ + value(Type value) { \ static_assert(internal::type() == internal::TYPE, "invalid type"); \ set_string(value); \ } @@ -1290,8 +1286,8 @@ class MakeValue : public value { FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str()) #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ - MakeValue(typename WCharHelper::Supported value) { \ - static_assert(internal::type() == internal::TYPE, "invalid type"); \ + value(typename WCharHelper::Supported value) { \ + static_assert(internal::type() == internal::TYPE, "invalid type"); \ set_string(value); \ } @@ -1304,17 +1300,16 @@ class MakeValue : public value { FMT_MAKE_VALUE(const void *, pointer, POINTER) template - MakeValue(const T &value, - typename EnableIf::value>::value, int>::type = 0) { + value(const T &value, + typename EnableIf::value>::value, int>::type = 0) { static_assert(internal::type() == internal::CUSTOM, "invalid type"); this->custom.value = &value; this->custom.format = &format_custom_arg; } template - MakeValue(const T &value, - typename EnableIf::value, int>::type = 0) { + value(const T &value, + typename EnableIf::value, int>::type = 0) { static_assert(internal::type() == internal::INT, "invalid type"); this->int_value = value; } @@ -1322,7 +1317,7 @@ class MakeValue : public value { // Additional template param `Char_` is needed here because make_type always // uses char. template - MakeValue(const NamedArg &value) { + value(const NamedArg &value) { static_assert( internal::type &>() == internal::NAMED_ARG, "invalid type"); @@ -1437,7 +1432,7 @@ template basic_format_arg make_arg(const T &value) { basic_format_arg arg; arg.type_ = internal::type(); - arg.value_ = internal::MakeValue(value); + arg.value_ = value; return arg; } @@ -1516,7 +1511,7 @@ enum { MAX_PACKED_ARGS = 16 }; template inline typename std::enable_if>::type make_arg(const T& value) { - return MakeValue(value); + return value; } template diff --git a/test/util-test.cc b/test/util-test.cc index f12d0fbb..978a8cc8 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -423,8 +423,7 @@ void format_value(fmt::Writer &, const Test &, CustomContext &ctx) { TEST(UtilTest, MakeValueWithCustomFormatter) { ::Test t; - fmt::internal::value arg = - fmt::internal::MakeValue(t); + fmt::internal::value arg(t); CustomContext ctx = {false}; fmt::MemoryWriter w; arg.custom.format(w, &t, &ctx);