Skip to content

Commit

Permalink
Use overloaded operator<< for enums if available (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 24, 2015
1 parent 1a2a333 commit f803179
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
54 changes: 38 additions & 16 deletions format.h
Original file line number Diff line number Diff line change
Expand Up @@ -937,28 +937,50 @@ 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 <typename T>
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<>);
};

No &operator<<(std::ostream &, int);

template<typename T, bool IS_CONVERTIBLE_TO_INT>
struct ConvertToIntImpl {
enum { value = false };
};

template<typename T>
struct ConvertToIntImpl<T, true> {
// Convert to int only if T doesn't have an overloaded operator<<.
enum {
value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
};
};

template<typename T>
struct ConvertToInt {
enum { is_convertible_to_int = sizeof(convert(get<T>())) == sizeof(Yes) };
enum { value = ConvertToIntImpl<T, is_convertible_to_int>::value };
};

#define FMT_CONVERTIBLE_TO_INT(Type) \
#define FMT_DISABLE_CONVERSION_TO_INT(Type) \
template <> \
class IsConvertibleToInt<Type> { \
public: \
enum { value = 1 }; \
}
struct ConvertToInt<Type> { 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);

// Disable conversion from integral types to avoid ambiguity.
FMT_DISABLE_CONVERSION_TO_INT(unsigned);
FMT_DISABLE_CONVERSION_TO_INT(long);

template<bool B, class T = void>
struct EnableIf {};
Expand Down Expand Up @@ -1113,20 +1135,20 @@ class MakeValue : public Arg {
template <typename T>
MakeValue(const T &value,
typename EnableIf<Not<
IsConvertibleToInt<T>::value>::value, int>::type = 0) {
ConvertToInt<T>::value>::value, int>::type = 0) {
custom.value = &value;
custom.format = &format_custom_arg<T>;
}

template <typename T>
MakeValue(const T &value,
typename EnableIf<IsConvertibleToInt<T>::value, int>::type = 0) {
typename EnableIf<ConvertToInt<T>::value, int>::type = 0) {
int_value = value;
}

template <typename T>
static uint64_t type(const T &) {
return IsConvertibleToInt<T>::value ? Arg::INT : Arg::CUSTOM;
return ConvertToInt<T>::value ? Arg::INT : Arg::CUSTOM;
}

// Additional template param `Char_` is needed here because make_type always
Expand Down
12 changes: 12 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
13 changes: 10 additions & 3 deletions test/util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,16 @@ TEST(UtilTest, ReportWindowsError) {

#endif // _WIN32

TEST(UtilTest, IsConvertibleToInt) {
EXPECT_TRUE(fmt::internal::IsConvertibleToInt<char>::value);
EXPECT_FALSE(fmt::internal::IsConvertibleToInt<const char *>::value);
enum TestEnum2 {};
enum TestEnum3 {};
std::ostream &operator<<(std::ostream &, TestEnum3);

TEST(UtilTest, ConvertToInt) {
EXPECT_TRUE(fmt::internal::ConvertToInt<char>::is_convertible_to_int);
EXPECT_FALSE(
fmt::internal::ConvertToInt<const char *>::is_convertible_to_int);
EXPECT_TRUE(fmt::internal::ConvertToInt<TestEnum2>::value);
EXPECT_FALSE(fmt::internal::ConvertToInt<TestEnum3>::value);
}

#if FMT_USE_ENUM_BASE
Expand Down

0 comments on commit f803179

Please sign in to comment.