Skip to content

Commit

Permalink
Implement printing of values that are convertible to int such as enums
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 10, 2015
1 parent 08d759c commit 5821aec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
34 changes: 32 additions & 2 deletions format.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,26 @@ struct WCharHelper<T, wchar_t> {
typedef None<T> Unsupported;
};

template <typename T>
class IsConvertibleToInt {
private:
typedef char yes[1];
typedef char no[2];

static const T &get();
static yes &check(int);
static no &check(...);

public:
enum { value = (sizeof(check(get())) == sizeof(yes)) };
};

template<bool B, class T = void>
struct EnableIf {};

template<class T>
struct EnableIf<true, T> { typedef T type; };

// Makes a Value object from any type.
template <typename Char>
class MakeValue : public Value {
Expand Down Expand Up @@ -885,12 +905,22 @@ class MakeValue : public Value {
FMT_MAKE_VALUE(const void *, pointer, POINTER)

template <typename T>
MakeValue(const T &value) {
MakeValue(const T &value,
typename EnableIf<!IsConvertibleToInt<T>::value, int>::type = 0) {
custom.value = &value;
custom.format = &format_custom_arg<T>;
}

template <typename T>
static uint64_t type(const T &) { return Arg::CUSTOM; }
MakeValue(const T &value,
typename EnableIf<IsConvertibleToInt<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;
}
};

#define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
Expand Down
6 changes: 6 additions & 0 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ TEST(PrintfTest, Location) {
// TODO: test %n
}

enum E { A = 42 };

TEST(PrintfTest, Enum) {
EXPECT_PRINTF("42", "%d", A);
}

#if FMT_USE_FILE_DESCRIPTORS
TEST(PrintfTest, Examples) {
const char *weekday = "Thursday";
Expand Down
5 changes: 5 additions & 0 deletions test/util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,8 @@ TEST(UtilTest, ReportWindowsError) {
}

#endif // _WIN32

TEST(UtilTest, IsConvertibleToInt) {
EXPECT_TRUE(fmt::internal::IsConvertibleToInt<char>::value);
EXPECT_FALSE(fmt::internal::IsConvertibleToInt<const char *>::value);
}

0 comments on commit 5821aec

Please sign in to comment.