From 6cff6d81374fef37bedc3f82eefb9a01ee48e60b Mon Sep 17 00:00:00 2001 From: vitaut Date: Tue, 24 Nov 2015 08:18:19 -0800 Subject: [PATCH] Use overloaded operator<< for enums if available (#232) --- format.h | 63 +++++++++++++++++++++++++++++++++------------ test/format-test.cc | 12 +++++++++ test/util-test.cc | 14 +++++++--- 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/format.h b/format.h index 2acd330e..5f17a687 100644 --- a/format.h +++ b/format.h @@ -937,28 +937,59 @@ typedef char No[2]; // These are non-members to workaround an overload resolution bug in bcc32. Yes &convert(fmt::ULongLong); +Yes &convert(std::ostream &); No &convert(...); template -class IsConvertibleToInt { - protected: - static const T &get(); +T &get(); - public: - enum { value = (sizeof(convert(get())) == sizeof(Yes)) }; +struct DummyStream : std::ostream { + // Hide all operator<< overloads from std::ostream. + void operator<<(Null<>); }; -#define FMT_CONVERTIBLE_TO_INT(Type) \ +No &operator<<(std::ostream &, int); + +template +struct ConvertToIntImpl { + enum { value = false }; +}; + +template +struct ConvertToIntImpl { + // Convert to int only if T doesn't have an overloaded operator<<. + enum { + value = sizeof(convert(get() << get())) == sizeof(No) + }; +}; + +template +struct ConvertToIntImpl2 { + enum { value = false }; +}; + +template +struct ConvertToIntImpl2 { + enum { + // Don't convert numeric types. + value = ConvertToIntImpl::is_specialized>::value + }; +}; + +template +struct ConvertToInt { + enum { enable_conversion = sizeof(convert(get())) == sizeof(Yes) }; + enum { value = ConvertToIntImpl2::value }; +}; + +#define FMT_DISABLE_CONVERSION_TO_INT(Type) \ template <> \ - class IsConvertibleToInt { \ - public: \ - enum { value = 1 }; \ - } + struct ConvertToInt { enum { value = 0 }; } // Silence warnings about convering float to int. -FMT_CONVERTIBLE_TO_INT(float); -FMT_CONVERTIBLE_TO_INT(double); -FMT_CONVERTIBLE_TO_INT(long double); +FMT_DISABLE_CONVERSION_TO_INT(float); +FMT_DISABLE_CONVERSION_TO_INT(double); +FMT_DISABLE_CONVERSION_TO_INT(long double); template struct EnableIf {}; @@ -1113,20 +1144,20 @@ class MakeValue : public Arg { template MakeValue(const T &value, typename EnableIf::value>::value, int>::type = 0) { + ConvertToInt::value>::value, int>::type = 0) { custom.value = &value; custom.format = &format_custom_arg; } template MakeValue(const T &value, - typename EnableIf::value, int>::type = 0) { + typename EnableIf::value, int>::type = 0) { int_value = value; } template static uint64_t type(const T &) { - return IsConvertibleToInt::value ? Arg::INT : Arg::CUSTOM; + return ConvertToInt::value ? Arg::INT : Arg::CUSTOM; } // Additional template param `Char_` is needed here because make_type always diff --git a/test/format-test.cc b/test/format-test.cc index f2df51ea..39195c83 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1637,3 +1637,15 @@ TEST(LiteralsTest, NamedArg) { udl_a_w); } #endif // FMT_USE_USER_DEFINED_LITERALS + +enum TestEnum {}; +std::ostream &operator<<(std::ostream &os, TestEnum) { + return os << "TestEnum"; +} + +enum TestEnum2 { A }; + +TEST(FormatTest, Enum) { + EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum())); + EXPECT_EQ("0", fmt::format("{}", A)); +} diff --git a/test/util-test.cc b/test/util-test.cc index b124e05a..03557d64 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -870,15 +870,21 @@ TEST(UtilTest, ReportWindowsError) { #endif // _WIN32 -TEST(UtilTest, IsConvertibleToInt) { - EXPECT_TRUE(fmt::internal::IsConvertibleToInt::value); - EXPECT_FALSE(fmt::internal::IsConvertibleToInt::value); +enum TestEnum2 {}; +enum TestEnum3 {}; +std::ostream &operator<<(std::ostream &, TestEnum3); + +TEST(UtilTest, ConvertToInt) { + EXPECT_TRUE(fmt::internal::ConvertToInt::enable_conversion); + EXPECT_FALSE(fmt::internal::ConvertToInt::enable_conversion); + EXPECT_TRUE(fmt::internal::ConvertToInt::value); + EXPECT_FALSE(fmt::internal::ConvertToInt::value); } #if FMT_USE_ENUM_BASE enum TestEnum : char {TestValue}; TEST(UtilTest, IsEnumConvertibleToInt) { - EXPECT_TRUE(fmt::internal::IsConvertibleToInt::value); + EXPECT_TRUE(fmt::internal::ConvertToInt::enable_conversion); } #endif