Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:25

0001 // Formatting library for C++ - formatters for standard library types
0002 //
0003 // Copyright (c) 2012 - present, Victor Zverovich
0004 // All rights reserved.
0005 //
0006 // For the license information refer to format.h.
0007 
0008 #ifndef FMT_STD_H_
0009 #define FMT_STD_H_
0010 
0011 #include <thread>
0012 #include <type_traits>
0013 #include <utility>
0014 
0015 #include "ostream.h"
0016 
0017 #if FMT_HAS_INCLUDE(<version>)
0018 #  include <version>
0019 #endif
0020 // Checking FMT_CPLUSPLUS for warning suppression in MSVC.
0021 #if FMT_CPLUSPLUS >= 201703L
0022 #  if FMT_HAS_INCLUDE(<filesystem>)
0023 #    include <filesystem>
0024 #  endif
0025 #  if FMT_HAS_INCLUDE(<variant>)
0026 #    include <variant>
0027 #  endif
0028 #endif
0029 
0030 #ifdef __cpp_lib_filesystem
0031 FMT_BEGIN_NAMESPACE
0032 
0033 namespace detail {
0034 
0035 template <typename Char>
0036 void write_escaped_path(basic_memory_buffer<Char>& quoted,
0037                         const std::filesystem::path& p) {
0038   write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
0039 }
0040 #  ifdef _WIN32
0041 template <>
0042 inline void write_escaped_path<char>(basic_memory_buffer<char>& quoted,
0043                                      const std::filesystem::path& p) {
0044   auto s = p.u8string();
0045   write_escaped_string<char>(
0046       std::back_inserter(quoted),
0047       string_view(reinterpret_cast<const char*>(s.c_str()), s.size()));
0048 }
0049 #  endif
0050 template <>
0051 inline void write_escaped_path<std::filesystem::path::value_type>(
0052     basic_memory_buffer<std::filesystem::path::value_type>& quoted,
0053     const std::filesystem::path& p) {
0054   write_escaped_string<std::filesystem::path::value_type>(
0055       std::back_inserter(quoted), p.native());
0056 }
0057 
0058 }  // namespace detail
0059 
0060 template <typename Char>
0061 struct formatter<std::filesystem::path, Char>
0062     : formatter<basic_string_view<Char>> {
0063   template <typename FormatContext>
0064   auto format(const std::filesystem::path& p, FormatContext& ctx) const ->
0065       typename FormatContext::iterator {
0066     basic_memory_buffer<Char> quoted;
0067     detail::write_escaped_path(quoted, p);
0068     return formatter<basic_string_view<Char>>::format(
0069         basic_string_view<Char>(quoted.data(), quoted.size()), ctx);
0070   }
0071 };
0072 FMT_END_NAMESPACE
0073 #endif
0074 
0075 FMT_BEGIN_NAMESPACE
0076 template <typename Char>
0077 struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
0078 FMT_END_NAMESPACE
0079 
0080 #ifdef __cpp_lib_variant
0081 FMT_BEGIN_NAMESPACE
0082 template <typename Char> struct formatter<std::monostate, Char> {
0083   template <typename ParseContext>
0084   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0085     return ctx.begin();
0086   }
0087 
0088   template <typename FormatContext>
0089   auto format(const std::monostate&, FormatContext& ctx) const
0090       -> decltype(ctx.out()) {
0091     auto out = ctx.out();
0092     out = detail::write<Char>(out, "monostate");
0093     return out;
0094   }
0095 };
0096 
0097 namespace detail {
0098 
0099 template <typename T>
0100 using variant_index_sequence =
0101     std::make_index_sequence<std::variant_size<T>::value>;
0102 
0103 // variant_size and variant_alternative check.
0104 template <typename T, typename U = void>
0105 struct is_variant_like_ : std::false_type {};
0106 template <typename T>
0107 struct is_variant_like_<T, std::void_t<decltype(std::variant_size<T>::value)>>
0108     : std::true_type {};
0109 
0110 // formattable element check
0111 template <typename T, typename C> class is_variant_formattable_ {
0112   template <std::size_t... I>
0113   static std::conjunction<
0114       is_formattable<std::variant_alternative_t<I, T>, C>...>
0115       check(std::index_sequence<I...>);
0116 
0117  public:
0118   static constexpr const bool value =
0119       decltype(check(variant_index_sequence<T>{}))::value;
0120 };
0121 
0122 template <typename Char, typename OutputIt, typename T>
0123 auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt {
0124   if constexpr (is_string<T>::value)
0125     return write_escaped_string<Char>(out, detail::to_string_view(v));
0126   else if constexpr (std::is_same_v<T, Char>)
0127     return write_escaped_char(out, v);
0128   else
0129     return write<Char>(out, v);
0130 }
0131 
0132 }  // namespace detail
0133 
0134 template <typename T> struct is_variant_like {
0135   static constexpr const bool value = detail::is_variant_like_<T>::value;
0136 };
0137 
0138 template <typename T, typename C> struct is_variant_formattable {
0139   static constexpr const bool value =
0140       detail::is_variant_formattable_<T, C>::value;
0141 };
0142 
0143 template <typename Variant, typename Char>
0144 struct formatter<
0145     Variant, Char,
0146     std::enable_if_t<std::conjunction_v<
0147         is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> {
0148   template <typename ParseContext>
0149   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0150     return ctx.begin();
0151   }
0152 
0153   template <typename FormatContext>
0154   auto format(const Variant& value, FormatContext& ctx) const
0155       -> decltype(ctx.out()) {
0156     auto out = ctx.out();
0157 
0158     out = detail::write<Char>(out, "variant(");
0159     std::visit(
0160         [&](const auto& v) {
0161           out = detail::write_variant_alternative<Char>(out, v);
0162         },
0163         value);
0164     *out++ = ')';
0165     return out;
0166   }
0167 };
0168 FMT_END_NAMESPACE
0169 #endif
0170 
0171 #endif  // FMT_STD_H_