Skip to content

Commit d45ffa8

Browse files
committed
Cleanup standard formatters
1 parent 4404dc0 commit d45ffa8

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

include/fmt/std.h

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
# include <atomic>
1616
# include <bitset>
1717
# include <complex>
18-
# include <cstdlib>
1918
# include <exception>
20-
# include <functional>
19+
# include <functional> // std::reference_wrapper
2120
# include <memory>
2221
# include <thread>
2322
# include <type_traits>
24-
# include <typeinfo>
25-
# include <utility>
23+
# include <typeinfo> // std::type_info
24+
# include <utility> // std::make_index_sequence
2625

2726
// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC.
2827
# if FMT_CPLUSPLUS >= 201703L
@@ -132,16 +131,15 @@ template <typename> struct is_variant_like_ : std::false_type {};
132131
template <typename... Types>
133132
struct is_variant_like_<std::variant<Types...>> : std::true_type {};
134133

135-
// formattable element check.
136-
template <typename T, typename C> class is_variant_formattable_ {
137-
template <std::size_t... Is>
134+
template <typename Variant, typename Char> class is_variant_formattable {
135+
template <size_t... Is>
138136
static std::conjunction<
139-
is_formattable<std::variant_alternative_t<Is, T>, C>...>
137+
is_formattable<std::variant_alternative_t<Is, Variant>, Char>...>
140138
check(std::index_sequence<Is...>);
141139

142140
public:
143141
static constexpr const bool value =
144-
decltype(check(variant_index_sequence<T>{}))::value;
142+
decltype(check(variant_index_sequence<Variant>{}))::value;
145143
};
146144

147145
#endif // FMT_CPP_LIB_VARIANT
@@ -152,7 +150,7 @@ template <typename Char, typename OutputIt>
152150
auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt {
153151
# ifdef FMT_HAS_ABI_CXA_DEMANGLE
154152
int status = 0;
155-
std::size_t size = 0;
153+
size_t size = 0;
156154
std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
157155
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
158156

@@ -191,7 +189,7 @@ auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt {
191189
return detail::write_bytes<Char>(out, demangled_name_view);
192190
# elif FMT_MSC_VERSION
193191
const string_view demangled_name(ti.name());
194-
for (std::size_t i = 0; i < demangled_name.size(); ++i) {
192+
for (size_t i = 0; i < demangled_name.size(); ++i) {
195193
auto sub = demangled_name;
196194
sub.remove_prefix(i);
197195
if (sub.starts_with("enum ")) {
@@ -240,8 +238,31 @@ struct is_bit_reference_like<std::__bit_const_reference<C>> {
240238

241239
} // namespace detail
242240

241+
template <typename T, typename Deleter>
242+
auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
243+
return p.get();
244+
}
245+
template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
246+
return p.get();
247+
}
248+
243249
#if FMT_CPP_LIB_FILESYSTEM
244250

251+
class path : public std::filesystem::path {
252+
public:
253+
auto display_string() const -> std::string {
254+
const std::filesystem::path& base = *this;
255+
return fmt::format(FMT_STRING("{}"), base);
256+
}
257+
auto system_string() const -> std::string { return string(); }
258+
259+
auto generic_display_string() const -> std::string {
260+
const std::filesystem::path& base = *this;
261+
return fmt::format(FMT_STRING("{:g}"), base);
262+
}
263+
auto generic_system_string() const -> std::string { return generic_string(); }
264+
};
265+
245266
template <typename Char> struct formatter<std::filesystem::path, Char> {
246267
private:
247268
format_specs specs_;
@@ -291,37 +312,20 @@ template <typename Char> struct formatter<std::filesystem::path, Char> {
291312
}
292313
};
293314

294-
class path : public std::filesystem::path {
295-
public:
296-
auto display_string() const -> std::string {
297-
const std::filesystem::path& base = *this;
298-
return fmt::format(FMT_STRING("{}"), base);
299-
}
300-
auto system_string() const -> std::string { return string(); }
301-
302-
auto generic_display_string() const -> std::string {
303-
const std::filesystem::path& base = *this;
304-
return fmt::format(FMT_STRING("{:g}"), base);
305-
}
306-
auto generic_system_string() const -> std::string { return generic_string(); }
307-
};
308-
309315
#endif // FMT_CPP_LIB_FILESYSTEM
310316

311-
template <std::size_t N, typename Char>
317+
template <size_t N, typename Char>
312318
struct formatter<std::bitset<N>, Char>
313319
: nested_formatter<basic_string_view<Char>, Char> {
314320
private:
315-
// Functor because C++11 doesn't support generic lambdas.
321+
// This is functor because C++11 doesn't support generic lambdas.
316322
struct writer {
317323
const std::bitset<N>& bs;
318324

319325
template <typename OutputIt>
320326
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
321-
for (auto pos = N; pos > 0; --pos) {
327+
for (auto pos = N; pos > 0; --pos)
322328
out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
323-
}
324-
325329
return out;
326330
}
327331
};
@@ -433,11 +437,6 @@ template <typename T> struct is_variant_like {
433437
static constexpr const bool value = detail::is_variant_like_<T>::value;
434438
};
435439

436-
template <typename T, typename C> struct is_variant_formattable {
437-
static constexpr const bool value =
438-
detail::is_variant_formattable_<T, C>::value;
439-
};
440-
441440
template <typename Char> struct formatter<std::monostate, Char> {
442441
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
443442
return ctx.begin();
@@ -454,7 +453,7 @@ template <typename Variant, typename Char>
454453
struct formatter<
455454
Variant, Char,
456455
std::enable_if_t<std::conjunction_v<
457-
is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> {
456+
is_variant_like<Variant>, detail::is_variant_formattable<Variant, Char>>>> {
458457
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
459458
return ctx.begin();
460459
}
@@ -599,14 +598,6 @@ struct formatter<BitRef, Char,
599598
}
600599
};
601600

602-
template <typename T, typename Deleter>
603-
auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
604-
return p.get();
605-
}
606-
template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
607-
return p.get();
608-
}
609-
610601
template <typename T, typename Char>
611602
struct formatter<std::atomic<T>, Char,
612603
enable_if_t<is_formattable<T, Char>::value>>

0 commit comments

Comments
 (0)