Skip to content

Commit 5536eb4

Browse files
committed
Consolidate implementation details
1 parent 7bb6fcb commit 5536eb4

File tree

1 file changed

+43
-58
lines changed

1 file changed

+43
-58
lines changed

include/fmt/std.h

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# include <type_traits>
2424
# include <typeinfo>
2525
# include <utility>
26-
# include <vector>
2726

2827
// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC.
2928
# if FMT_CPLUSPLUS >= 201703L
@@ -79,11 +78,11 @@
7978
# endif
8079
#endif
8180

82-
#if FMT_CPP_LIB_FILESYSTEM
8381
FMT_BEGIN_NAMESPACE
84-
8582
namespace detail {
8683

84+
#if FMT_CPP_LIB_FILESYSTEM
85+
8786
template <typename Char, typename PathChar>
8887
auto get_path_string(const std::filesystem::path& p,
8988
const std::basic_string<PathChar>& native) {
@@ -111,8 +110,46 @@ void write_escaped_path(basic_memory_buffer<Char>& quoted,
111110
}
112111
}
113112

113+
#endif // FMT_CPP_LIB_FILESYSTEM
114+
115+
#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT
116+
template <typename Char, typename OutputIt, typename T>
117+
auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt {
118+
if constexpr (has_to_string_view<T>::value)
119+
return write_escaped_string<Char>(out, detail::to_string_view(v));
120+
if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
121+
return write<Char>(out, v);
122+
}
123+
#endif
124+
125+
#if FMT_CPP_LIB_VARIANT
126+
127+
template <typename T>
128+
using variant_index_sequence =
129+
std::make_index_sequence<std::variant_size<T>::value>;
130+
131+
template <typename> struct is_variant_like_ : std::false_type {};
132+
template <typename... Types>
133+
struct is_variant_like_<std::variant<Types...>> : std::true_type {};
134+
135+
// formattable element check.
136+
template <typename T, typename C> class is_variant_formattable_ {
137+
template <std::size_t... Is>
138+
static std::conjunction<
139+
is_formattable<std::variant_alternative_t<Is, T>, C>...>
140+
check(std::index_sequence<Is...>);
141+
142+
public:
143+
static constexpr const bool value =
144+
decltype(check(variant_index_sequence<T>{}))::value;
145+
};
146+
147+
#endif // FMT_CPP_LIB_VARIANT
148+
114149
} // namespace detail
115150

151+
#if FMT_CPP_LIB_FILESYSTEM
152+
116153
template <typename Char> struct formatter<std::filesystem::path, Char> {
117154
private:
118155
format_specs specs_;
@@ -177,10 +214,8 @@ class path : public std::filesystem::path {
177214
auto generic_system_string() const -> std::string { return generic_string(); }
178215
};
179216

180-
FMT_END_NAMESPACE
181217
#endif // FMT_CPP_LIB_FILESYSTEM
182218

183-
FMT_BEGIN_NAMESPACE
184219
template <std::size_t N, typename Char>
185220
struct formatter<std::bitset<N>, Char>
186221
: nested_formatter<basic_string_view<Char>, Char> {
@@ -209,10 +244,8 @@ struct formatter<std::bitset<N>, Char>
209244

210245
template <typename Char>
211246
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
212-
FMT_END_NAMESPACE
213247

214248
#ifdef __cpp_lib_optional
215-
FMT_BEGIN_NAMESPACE
216249
template <typename T, typename Char>
217250
struct formatter<std::optional<T>, Char,
218251
std::enable_if_t<is_formattable<T, Char>::value>> {
@@ -251,30 +284,9 @@ struct formatter<std::optional<T>, Char,
251284
return detail::write(out, ')');
252285
}
253286
};
254-
FMT_END_NAMESPACE
255287
#endif // __cpp_lib_optional
256288

257-
#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT
258-
259-
FMT_BEGIN_NAMESPACE
260-
namespace detail {
261-
262-
template <typename Char, typename OutputIt, typename T>
263-
auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt {
264-
if constexpr (has_to_string_view<T>::value)
265-
return write_escaped_string<Char>(out, detail::to_string_view(v));
266-
if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
267-
return write<Char>(out, v);
268-
}
269-
270-
} // namespace detail
271-
272-
FMT_END_NAMESPACE
273-
#endif
274-
275289
#ifdef __cpp_lib_expected
276-
FMT_BEGIN_NAMESPACE
277-
278290
template <typename T, typename E, typename Char>
279291
struct formatter<std::expected<T, E>, Char,
280292
std::enable_if_t<(std::is_void<T>::value ||
@@ -301,11 +313,9 @@ struct formatter<std::expected<T, E>, Char,
301313
return out;
302314
}
303315
};
304-
FMT_END_NAMESPACE
305316
#endif // __cpp_lib_expected
306317

307318
#ifdef __cpp_lib_source_location
308-
FMT_BEGIN_NAMESPACE
309319
template <> struct formatter<std::source_location> {
310320
FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); }
311321

@@ -323,34 +333,9 @@ template <> struct formatter<std::source_location> {
323333
return out;
324334
}
325335
};
326-
FMT_END_NAMESPACE
327336
#endif
328337

329338
#if FMT_CPP_LIB_VARIANT
330-
FMT_BEGIN_NAMESPACE
331-
namespace detail {
332-
333-
template <typename T>
334-
using variant_index_sequence =
335-
std::make_index_sequence<std::variant_size<T>::value>;
336-
337-
template <typename> struct is_variant_like_ : std::false_type {};
338-
template <typename... Types>
339-
struct is_variant_like_<std::variant<Types...>> : std::true_type {};
340-
341-
// formattable element check.
342-
template <typename T, typename C> class is_variant_formattable_ {
343-
template <std::size_t... Is>
344-
static std::conjunction<
345-
is_formattable<std::variant_alternative_t<Is, T>, C>...>
346-
check(std::index_sequence<Is...>);
347-
348-
public:
349-
static constexpr const bool value =
350-
decltype(check(variant_index_sequence<T>{}))::value;
351-
};
352-
353-
} // namespace detail
354339

355340
template <typename T> struct is_variant_like {
356341
static constexpr const bool value = detail::is_variant_like_<T>::value;
@@ -402,10 +387,9 @@ struct formatter<
402387
return out;
403388
}
404389
};
405-
FMT_END_NAMESPACE
390+
406391
#endif // FMT_CPP_LIB_VARIANT
407392

408-
FMT_BEGIN_NAMESPACE
409393
template <> struct formatter<std::error_code> {
410394
private:
411395
format_specs specs_;
@@ -543,7 +527,7 @@ struct formatter<std::type_info, Char // DEPRECATED! Mixing code unit types.
543527
return detail::write_demangled_name<Char>(ctx.out(), ti);
544528
}
545529
};
546-
#endif
530+
#endif // FMT_USE_RTTI
547531

548532
template <typename T, typename Char>
549533
struct formatter<
@@ -725,4 +709,5 @@ struct formatter<std::reference_wrapper<T>, Char,
725709
};
726710

727711
FMT_END_NAMESPACE
712+
728713
#endif // FMT_STD_H_

0 commit comments

Comments
 (0)