Skip to content

Commit

Permalink
Added bit_reference-like formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
  • Loading branch information
phprus committed Aug 5, 2023
1 parent 64c905e commit 85bc49b
Showing 1 changed file with 17 additions and 45 deletions.
62 changes: 17 additions & 45 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,64 +394,36 @@ struct formatter<

FMT_EXPORT

#ifdef _LIBCPP_VERSION

template <typename C, bool St, typename Char>
struct formatter<std::__bit_reference<C, St>, Char> : formatter<bool, Char> {
template <typename FormatContext>
FMT_CONSTEXPR20 auto format(const std::__bit_reference<C, St>& v,
FormatContext& ctx) const -> decltype(ctx.out()) {
return formatter<bool, Char>::format(static_cast<bool>(v), ctx);
}
};

template <typename C, typename Char>
struct formatter<std::__bit_const_reference<C>, Char> : formatter<bool, Char> {
template <typename FormatContext>
FMT_CONSTEXPR20 auto format(const std::__bit_const_reference<C>& v,
FormatContext& ctx) const -> decltype(ctx.out()) {
return formatter<bool, Char>::format(static_cast<bool>(v), ctx);
}
};

#else
namespace detail {

# ifdef __GLIBCXX__
template <typename T, typename Enable = void>
struct has_flip : std::false_type {};

template <typename Char>
struct formatter<std::_Bit_reference, Char> : formatter<bool, Char> {
template <typename FormatContext>
auto format(const std::_Bit_reference& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<bool, Char>::format(static_cast<bool>(v), ctx);
}
};
template <typename T>
struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
: std::true_type {};

# elif defined(_CPPLIB_VER)
template <typename T> struct is_bit_reference_like {
static constexpr const bool value =
std::is_convertible<T, bool>::value &&
std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value

template <typename C, typename Char>
struct formatter<std::_Vb_reference<C>, Char> : formatter<bool, Char> {
template <typename FormatContext>
FMT_CONSTEXPR20 auto format(const std::_Vb_reference<C>& v,
FormatContext& ctx) const -> decltype(ctx.out()) {
return formatter<bool, Char>::format(static_cast<bool>(v), ctx);
}
;
};

# endif
} // namespace detail

template <std::size_t N, typename Char>
struct formatter<typename std::bitset<N>::reference, Char>
template <typename BitRef, typename Char>
struct formatter<BitRef, Char,
enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
: formatter<bool, Char> {
template <typename FormatContext>
auto format(const typename std::bitset<N>::reference& v,
FormatContext& ctx) const -> decltype(ctx.out()) {
FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<bool, Char>::format(static_cast<bool>(v), ctx);
}
};

#endif

FMT_END_NAMESPACE

#endif // FMT_STD_H_

0 comments on commit 85bc49b

Please sign in to comment.