Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:54:19

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 <atomic>
0012 #include <bitset>
0013 #include <cstdlib>
0014 #include <exception>
0015 #include <memory>
0016 #include <thread>
0017 #include <type_traits>
0018 #include <typeinfo>
0019 #include <utility>
0020 #include <vector>
0021 
0022 #include "format.h"
0023 #include "ostream.h"
0024 
0025 #if FMT_HAS_INCLUDE(<version>)
0026 #  include <version>
0027 #endif
0028 // Checking FMT_CPLUSPLUS for warning suppression in MSVC.
0029 #if FMT_CPLUSPLUS >= 201703L
0030 #  if FMT_HAS_INCLUDE(<filesystem>)
0031 #    include <filesystem>
0032 #  endif
0033 #  if FMT_HAS_INCLUDE(<variant>)
0034 #    include <variant>
0035 #  endif
0036 #  if FMT_HAS_INCLUDE(<optional>)
0037 #    include <optional>
0038 #  endif
0039 #endif
0040 
0041 #if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
0042 #  include <source_location>
0043 #endif
0044 
0045 // GCC 4 does not support FMT_HAS_INCLUDE.
0046 #if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__)
0047 #  include <cxxabi.h>
0048 // Android NDK with gabi++ library on some architectures does not implement
0049 // abi::__cxa_demangle().
0050 #  ifndef __GABIXX_CXXABI_H__
0051 #    define FMT_HAS_ABI_CXA_DEMANGLE
0052 #  endif
0053 #endif
0054 
0055 // Check if typeid is available.
0056 #ifndef FMT_USE_TYPEID
0057 // __RTTI is for EDG compilers. In MSVC typeid is available without RTTI.
0058 #  if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || FMT_MSC_VERSION || \
0059       defined(__INTEL_RTTI__) || defined(__RTTI)
0060 #    define FMT_USE_TYPEID 1
0061 #  else
0062 #    define FMT_USE_TYPEID 0
0063 #  endif
0064 #endif
0065 
0066 // For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined.
0067 #ifndef FMT_CPP_LIB_FILESYSTEM
0068 #  ifdef __cpp_lib_filesystem
0069 #    define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem
0070 #  else
0071 #    define FMT_CPP_LIB_FILESYSTEM 0
0072 #  endif
0073 #endif
0074 
0075 #ifndef FMT_CPP_LIB_VARIANT
0076 #  ifdef __cpp_lib_variant
0077 #    define FMT_CPP_LIB_VARIANT __cpp_lib_variant
0078 #  else
0079 #    define FMT_CPP_LIB_VARIANT 0
0080 #  endif
0081 #endif
0082 
0083 #if FMT_CPP_LIB_FILESYSTEM
0084 FMT_BEGIN_NAMESPACE
0085 
0086 namespace detail {
0087 
0088 template <typename Char, typename PathChar>
0089 auto get_path_string(const std::filesystem::path& p,
0090                      const std::basic_string<PathChar>& native) {
0091   if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>)
0092     return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
0093   else
0094     return p.string<Char>();
0095 }
0096 
0097 template <typename Char, typename PathChar>
0098 void write_escaped_path(basic_memory_buffer<Char>& quoted,
0099                         const std::filesystem::path& p,
0100                         const std::basic_string<PathChar>& native) {
0101   if constexpr (std::is_same_v<Char, char> &&
0102                 std::is_same_v<PathChar, wchar_t>) {
0103     auto buf = basic_memory_buffer<wchar_t>();
0104     write_escaped_string<wchar_t>(std::back_inserter(buf), native);
0105     bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
0106     FMT_ASSERT(valid, "invalid utf16");
0107   } else if constexpr (std::is_same_v<Char, PathChar>) {
0108     write_escaped_string<std::filesystem::path::value_type>(
0109         std::back_inserter(quoted), native);
0110   } else {
0111     write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
0112   }
0113 }
0114 
0115 }  // namespace detail
0116 
0117 FMT_EXPORT
0118 template <typename Char> struct formatter<std::filesystem::path, Char> {
0119  private:
0120   format_specs<Char> specs_;
0121   detail::arg_ref<Char> width_ref_;
0122   bool debug_ = false;
0123   char path_type_ = 0;
0124 
0125  public:
0126   FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
0127 
0128   template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
0129     auto it = ctx.begin(), end = ctx.end();
0130     if (it == end) return it;
0131 
0132     it = detail::parse_align(it, end, specs_);
0133     if (it == end) return it;
0134 
0135     it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
0136     if (it != end && *it == '?') {
0137       debug_ = true;
0138       ++it;
0139     }
0140     if (it != end && (*it == 'g')) path_type_ = *it++;
0141     return it;
0142   }
0143 
0144   template <typename FormatContext>
0145   auto format(const std::filesystem::path& p, FormatContext& ctx) const {
0146     auto specs = specs_;
0147 #  ifdef _WIN32
0148     auto path_string = !path_type_ ? p.native() : p.generic_wstring();
0149 #  else
0150     auto path_string = !path_type_ ? p.native() : p.generic_string();
0151 #  endif
0152 
0153     detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
0154                                                        ctx);
0155     if (!debug_) {
0156       auto s = detail::get_path_string<Char>(p, path_string);
0157       return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
0158     }
0159     auto quoted = basic_memory_buffer<Char>();
0160     detail::write_escaped_path(quoted, p, path_string);
0161     return detail::write(ctx.out(),
0162                          basic_string_view<Char>(quoted.data(), quoted.size()),
0163                          specs);
0164   }
0165 };
0166 FMT_END_NAMESPACE
0167 #endif  // FMT_CPP_LIB_FILESYSTEM
0168 
0169 FMT_BEGIN_NAMESPACE
0170 FMT_EXPORT
0171 template <std::size_t N, typename Char>
0172 struct formatter<std::bitset<N>, Char> : nested_formatter<string_view> {
0173  private:
0174   // Functor because C++11 doesn't support generic lambdas.
0175   struct writer {
0176     const std::bitset<N>& bs;
0177 
0178     template <typename OutputIt>
0179     FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
0180       for (auto pos = N; pos > 0; --pos) {
0181         out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
0182       }
0183 
0184       return out;
0185     }
0186   };
0187 
0188  public:
0189   template <typename FormatContext>
0190   auto format(const std::bitset<N>& bs, FormatContext& ctx) const
0191       -> decltype(ctx.out()) {
0192     return write_padded(ctx, writer{bs});
0193   }
0194 };
0195 
0196 FMT_EXPORT
0197 template <typename Char>
0198 struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
0199 FMT_END_NAMESPACE
0200 
0201 #ifdef __cpp_lib_optional
0202 FMT_BEGIN_NAMESPACE
0203 FMT_EXPORT
0204 template <typename T, typename Char>
0205 struct formatter<std::optional<T>, Char,
0206                  std::enable_if_t<is_formattable<T, Char>::value>> {
0207  private:
0208   formatter<T, Char> underlying_;
0209   static constexpr basic_string_view<Char> optional =
0210       detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l',
0211                              '('>{};
0212   static constexpr basic_string_view<Char> none =
0213       detail::string_literal<Char, 'n', 'o', 'n', 'e'>{};
0214 
0215   template <class U>
0216   FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set)
0217       -> decltype(u.set_debug_format(set)) {
0218     u.set_debug_format(set);
0219   }
0220 
0221   template <class U>
0222   FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {}
0223 
0224  public:
0225   template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
0226     maybe_set_debug_format(underlying_, true);
0227     return underlying_.parse(ctx);
0228   }
0229 
0230   template <typename FormatContext>
0231   auto format(const std::optional<T>& opt, FormatContext& ctx) const
0232       -> decltype(ctx.out()) {
0233     if (!opt) return detail::write<Char>(ctx.out(), none);
0234 
0235     auto out = ctx.out();
0236     out = detail::write<Char>(out, optional);
0237     ctx.advance_to(out);
0238     out = underlying_.format(*opt, ctx);
0239     return detail::write(out, ')');
0240   }
0241 };
0242 FMT_END_NAMESPACE
0243 #endif  // __cpp_lib_optional
0244 
0245 #ifdef __cpp_lib_source_location
0246 FMT_BEGIN_NAMESPACE
0247 FMT_EXPORT
0248 template <> struct formatter<std::source_location> {
0249   template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
0250     return ctx.begin();
0251   }
0252 
0253   template <typename FormatContext>
0254   auto format(const std::source_location& loc, FormatContext& ctx) const
0255       -> decltype(ctx.out()) {
0256     auto out = ctx.out();
0257     out = detail::write(out, loc.file_name());
0258     out = detail::write(out, ':');
0259     out = detail::write<char>(out, loc.line());
0260     out = detail::write(out, ':');
0261     out = detail::write<char>(out, loc.column());
0262     out = detail::write(out, ": ");
0263     out = detail::write(out, loc.function_name());
0264     return out;
0265   }
0266 };
0267 FMT_END_NAMESPACE
0268 #endif
0269 
0270 #if FMT_CPP_LIB_VARIANT
0271 FMT_BEGIN_NAMESPACE
0272 namespace detail {
0273 
0274 template <typename T>
0275 using variant_index_sequence =
0276     std::make_index_sequence<std::variant_size<T>::value>;
0277 
0278 template <typename> struct is_variant_like_ : std::false_type {};
0279 template <typename... Types>
0280 struct is_variant_like_<std::variant<Types...>> : std::true_type {};
0281 
0282 // formattable element check.
0283 template <typename T, typename C> class is_variant_formattable_ {
0284   template <std::size_t... Is>
0285   static std::conjunction<
0286       is_formattable<std::variant_alternative_t<Is, T>, C>...>
0287       check(std::index_sequence<Is...>);
0288 
0289  public:
0290   static constexpr const bool value =
0291       decltype(check(variant_index_sequence<T>{}))::value;
0292 };
0293 
0294 template <typename Char, typename OutputIt, typename T>
0295 auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt {
0296   if constexpr (is_string<T>::value)
0297     return write_escaped_string<Char>(out, detail::to_string_view(v));
0298   else if constexpr (std::is_same_v<T, Char>)
0299     return write_escaped_char(out, v);
0300   else
0301     return write<Char>(out, v);
0302 }
0303 
0304 }  // namespace detail
0305 
0306 template <typename T> struct is_variant_like {
0307   static constexpr const bool value = detail::is_variant_like_<T>::value;
0308 };
0309 
0310 template <typename T, typename C> struct is_variant_formattable {
0311   static constexpr const bool value =
0312       detail::is_variant_formattable_<T, C>::value;
0313 };
0314 
0315 FMT_EXPORT
0316 template <typename Char> struct formatter<std::monostate, Char> {
0317   template <typename ParseContext>
0318   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0319     return ctx.begin();
0320   }
0321 
0322   template <typename FormatContext>
0323   auto format(const std::monostate&, FormatContext& ctx) const
0324       -> decltype(ctx.out()) {
0325     return detail::write<Char>(ctx.out(), "monostate");
0326   }
0327 };
0328 
0329 FMT_EXPORT
0330 template <typename Variant, typename Char>
0331 struct formatter<
0332     Variant, Char,
0333     std::enable_if_t<std::conjunction_v<
0334         is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> {
0335   template <typename ParseContext>
0336   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0337     return ctx.begin();
0338   }
0339 
0340   template <typename FormatContext>
0341   auto format(const Variant& value, FormatContext& ctx) const
0342       -> decltype(ctx.out()) {
0343     auto out = ctx.out();
0344 
0345     out = detail::write<Char>(out, "variant(");
0346     FMT_TRY {
0347       std::visit(
0348           [&](const auto& v) {
0349             out = detail::write_variant_alternative<Char>(out, v);
0350           },
0351           value);
0352     }
0353     FMT_CATCH(const std::bad_variant_access&) {
0354       detail::write<Char>(out, "valueless by exception");
0355     }
0356     *out++ = ')';
0357     return out;
0358   }
0359 };
0360 FMT_END_NAMESPACE
0361 #endif  // FMT_CPP_LIB_VARIANT
0362 
0363 FMT_BEGIN_NAMESPACE
0364 FMT_EXPORT
0365 template <typename Char> struct formatter<std::error_code, Char> {
0366   template <typename ParseContext>
0367   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0368     return ctx.begin();
0369   }
0370 
0371   template <typename FormatContext>
0372   FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
0373       -> decltype(ctx.out()) {
0374     auto out = ctx.out();
0375     out = detail::write_bytes(out, ec.category().name(), format_specs<Char>());
0376     out = detail::write<Char>(out, Char(':'));
0377     out = detail::write<Char>(out, ec.value());
0378     return out;
0379   }
0380 };
0381 
0382 FMT_EXPORT
0383 template <typename T, typename Char>
0384 struct formatter<
0385     T, Char,  // DEPRECATED! Mixing code unit types.
0386     typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> {
0387  private:
0388   bool with_typename_ = false;
0389 
0390  public:
0391   FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
0392       -> decltype(ctx.begin()) {
0393     auto it = ctx.begin();
0394     auto end = ctx.end();
0395     if (it == end || *it == '}') return it;
0396     if (*it == 't') {
0397       ++it;
0398       with_typename_ = FMT_USE_TYPEID != 0;
0399     }
0400     return it;
0401   }
0402 
0403   template <typename OutputIt>
0404   auto format(const std::exception& ex,
0405               basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
0406     format_specs<Char> spec;
0407     auto out = ctx.out();
0408     if (!with_typename_)
0409       return detail::write_bytes(out, string_view(ex.what()), spec);
0410 
0411 #if FMT_USE_TYPEID
0412     const std::type_info& ti = typeid(ex);
0413 #  ifdef FMT_HAS_ABI_CXA_DEMANGLE
0414     int status = 0;
0415     std::size_t size = 0;
0416     std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
0417         abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
0418 
0419     string_view demangled_name_view;
0420     if (demangled_name_ptr) {
0421       demangled_name_view = demangled_name_ptr.get();
0422 
0423       // Normalization of stdlib inline namespace names.
0424       // libc++ inline namespaces.
0425       //  std::__1::*       -> std::*
0426       //  std::__1::__fs::* -> std::*
0427       // libstdc++ inline namespaces.
0428       //  std::__cxx11::*             -> std::*
0429       //  std::filesystem::__cxx11::* -> std::filesystem::*
0430       if (demangled_name_view.starts_with("std::")) {
0431         char* begin = demangled_name_ptr.get();
0432         char* to = begin + 5;  // std::
0433         for (char *from = to, *end = begin + demangled_name_view.size();
0434              from < end;) {
0435           // This is safe, because demangled_name is NUL-terminated.
0436           if (from[0] == '_' && from[1] == '_') {
0437             char* next = from + 1;
0438             while (next < end && *next != ':') next++;
0439             if (next[0] == ':' && next[1] == ':') {
0440               from = next + 2;
0441               continue;
0442             }
0443           }
0444           *to++ = *from++;
0445         }
0446         demangled_name_view = {begin, detail::to_unsigned(to - begin)};
0447       }
0448     } else {
0449       demangled_name_view = string_view(ti.name());
0450     }
0451     out = detail::write_bytes(out, demangled_name_view, spec);
0452 #  elif FMT_MSC_VERSION
0453     string_view demangled_name_view(ti.name());
0454     if (demangled_name_view.starts_with("class "))
0455       demangled_name_view.remove_prefix(6);
0456     else if (demangled_name_view.starts_with("struct "))
0457       demangled_name_view.remove_prefix(7);
0458     out = detail::write_bytes(out, demangled_name_view, spec);
0459 #  else
0460     out = detail::write_bytes(out, string_view(ti.name()), spec);
0461 #  endif
0462     *out++ = ':';
0463     *out++ = ' ';
0464     return detail::write_bytes(out, string_view(ex.what()), spec);
0465 #endif
0466   }
0467 };
0468 
0469 namespace detail {
0470 
0471 template <typename T, typename Enable = void>
0472 struct has_flip : std::false_type {};
0473 
0474 template <typename T>
0475 struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
0476     : std::true_type {};
0477 
0478 template <typename T> struct is_bit_reference_like {
0479   static constexpr const bool value =
0480       std::is_convertible<T, bool>::value &&
0481       std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value;
0482 };
0483 
0484 #ifdef _LIBCPP_VERSION
0485 
0486 // Workaround for libc++ incompatibility with C++ standard.
0487 // According to the Standard, `bitset::operator[] const` returns bool.
0488 template <typename C>
0489 struct is_bit_reference_like<std::__bit_const_reference<C>> {
0490   static constexpr const bool value = true;
0491 };
0492 
0493 #endif
0494 
0495 }  // namespace detail
0496 
0497 // We can't use std::vector<bool, Allocator>::reference and
0498 // std::bitset<N>::reference because the compiler can't deduce Allocator and N
0499 // in partial specialization.
0500 FMT_EXPORT
0501 template <typename BitRef, typename Char>
0502 struct formatter<BitRef, Char,
0503                  enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
0504     : formatter<bool, Char> {
0505   template <typename FormatContext>
0506   FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
0507       -> decltype(ctx.out()) {
0508     return formatter<bool, Char>::format(v, ctx);
0509   }
0510 };
0511 
0512 FMT_EXPORT
0513 template <typename T, typename Char>
0514 struct formatter<std::atomic<T>, Char,
0515                  enable_if_t<is_formattable<T, Char>::value>>
0516     : formatter<T, Char> {
0517   template <typename FormatContext>
0518   auto format(const std::atomic<T>& v, FormatContext& ctx) const
0519       -> decltype(ctx.out()) {
0520     return formatter<T, Char>::format(v.load(), ctx);
0521   }
0522 };
0523 
0524 #ifdef __cpp_lib_atomic_flag_test
0525 FMT_EXPORT
0526 template <typename Char>
0527 struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
0528   template <typename FormatContext>
0529   auto format(const std::atomic_flag& v, FormatContext& ctx) const
0530       -> decltype(ctx.out()) {
0531     return formatter<bool, Char>::format(v.test(), ctx);
0532   }
0533 };
0534 #endif  // __cpp_lib_atomic_flag_test
0535 
0536 FMT_END_NAMESPACE
0537 #endif  // FMT_STD_H_