std.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Formatting library for C++ - formatters for standard library types
  3. //
  4. // Copyright (c) 2012 - present, Victor Zverovich
  5. // All rights reserved.
  6. //
  7. // For the license information refer to format.h.
  8. #ifndef FMT_STD_H_
  9. #define FMT_STD_H_
  10. #include "format.h"
  11. #include "ostream.h"
  12. #ifndef FMT_MODULE
  13. # include <atomic>
  14. # include <bitset>
  15. # include <complex>
  16. # include <exception>
  17. # include <functional> // std::reference_wrapper
  18. # include <memory>
  19. # include <thread>
  20. # include <type_traits>
  21. # include <typeinfo> // std::type_info
  22. # include <utility> // std::make_index_sequence
  23. // Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC.
  24. # if FMT_CPLUSPLUS >= 201703L
  25. # if FMT_HAS_INCLUDE(<filesystem>) && \
  26. (!defined(FMT_CPP_LIB_FILESYSTEM) || FMT_CPP_LIB_FILESYSTEM != 0)
  27. # include <filesystem>
  28. # endif
  29. # if FMT_HAS_INCLUDE(<variant>)
  30. # include <variant>
  31. # endif
  32. # if FMT_HAS_INCLUDE(<optional>)
  33. # include <optional>
  34. # endif
  35. # endif
  36. // Use > instead of >= in the version check because <source_location> may be
  37. // available after C++17 but before C++20 is marked as implemented.
  38. # if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
  39. # include <source_location>
  40. # endif
  41. # if FMT_CPLUSPLUS > 202002L && FMT_HAS_INCLUDE(<expected>)
  42. # include <expected>
  43. # endif
  44. #endif // FMT_MODULE
  45. #if FMT_HAS_INCLUDE(<version>)
  46. # include <version>
  47. #endif
  48. // GCC 4 does not support FMT_HAS_INCLUDE.
  49. #if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__)
  50. # include <cxxabi.h>
  51. // Android NDK with gabi++ library on some architectures does not implement
  52. // abi::__cxa_demangle().
  53. # ifndef __GABIXX_CXXABI_H__
  54. # define FMT_HAS_ABI_CXA_DEMANGLE
  55. # endif
  56. #endif
  57. #ifdef FMT_CPP_LIB_FILESYSTEM
  58. // Use the provided definition.
  59. #elif defined(__cpp_lib_filesystem)
  60. # define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem
  61. #else
  62. # define FMT_CPP_LIB_FILESYSTEM 0
  63. #endif
  64. #ifdef FMT_CPP_LIB_VARIANT
  65. // Use the provided definition.
  66. #elif defined(__cpp_lib_variant)
  67. # define FMT_CPP_LIB_VARIANT __cpp_lib_variant
  68. #else
  69. # define FMT_CPP_LIB_VARIANT 0
  70. #endif
  71. FMT_BEGIN_NAMESPACE
  72. namespace detail {
  73. #if FMT_CPP_LIB_FILESYSTEM
  74. template <typename Char, typename PathChar>
  75. auto get_path_string(const std::filesystem::path& p,
  76. const std::basic_string<PathChar>& native) {
  77. if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>)
  78. return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
  79. else
  80. return p.string<Char>();
  81. }
  82. template <typename Char, typename PathChar>
  83. void write_escaped_path(basic_memory_buffer<Char>& quoted,
  84. const std::filesystem::path& p,
  85. const std::basic_string<PathChar>& native) {
  86. if constexpr (std::is_same_v<Char, char> &&
  87. std::is_same_v<PathChar, wchar_t>) {
  88. auto buf = basic_memory_buffer<wchar_t>();
  89. write_escaped_string<wchar_t>(std::back_inserter(buf), native);
  90. bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
  91. FMT_ASSERT(valid, "invalid utf16");
  92. } else if constexpr (std::is_same_v<Char, PathChar>) {
  93. write_escaped_string<std::filesystem::path::value_type>(
  94. std::back_inserter(quoted), native);
  95. } else {
  96. write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
  97. }
  98. }
  99. #endif // FMT_CPP_LIB_FILESYSTEM
  100. #if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT
  101. template <typename Char, typename OutputIt, typename T, typename FormatContext>
  102. auto write_escaped_alternative(OutputIt out, const T& v, FormatContext& ctx)
  103. -> OutputIt {
  104. if constexpr (has_to_string_view<T>::value)
  105. return write_escaped_string<Char>(out, detail::to_string_view(v));
  106. if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
  107. formatter<std::remove_cv_t<T>, Char> underlying;
  108. maybe_set_debug_format(underlying, true);
  109. return underlying.format(v, ctx);
  110. }
  111. #endif
  112. #if FMT_CPP_LIB_VARIANT
  113. template <typename> struct is_variant_like_ : std::false_type {};
  114. template <typename... Types>
  115. struct is_variant_like_<std::variant<Types...>> : std::true_type {};
  116. template <typename Variant, typename Char> class is_variant_formattable {
  117. template <size_t... Is>
  118. static auto check(std::index_sequence<Is...>) -> std::conjunction<
  119. is_formattable<std::variant_alternative_t<Is, Variant>, Char>...>;
  120. public:
  121. static constexpr bool value = decltype(check(
  122. std::make_index_sequence<std::variant_size<Variant>::value>()))::value;
  123. };
  124. #endif // FMT_CPP_LIB_VARIANT
  125. #if FMT_USE_RTTI
  126. inline auto normalize_libcxx_inline_namespaces(string_view demangled_name_view,
  127. char* begin) -> string_view {
  128. // Normalization of stdlib inline namespace names.
  129. // libc++ inline namespaces.
  130. // std::__1::* -> std::*
  131. // std::__1::__fs::* -> std::*
  132. // libstdc++ inline namespaces.
  133. // std::__cxx11::* -> std::*
  134. // std::filesystem::__cxx11::* -> std::filesystem::*
  135. if (demangled_name_view.starts_with("std::")) {
  136. char* to = begin + 5; // std::
  137. for (const char *from = to, *end = begin + demangled_name_view.size();
  138. from < end;) {
  139. // This is safe, because demangled_name is NUL-terminated.
  140. if (from[0] == '_' && from[1] == '_') {
  141. const char* next = from + 1;
  142. while (next < end && *next != ':') next++;
  143. if (next[0] == ':' && next[1] == ':') {
  144. from = next + 2;
  145. continue;
  146. }
  147. }
  148. *to++ = *from++;
  149. }
  150. demangled_name_view = {begin, detail::to_unsigned(to - begin)};
  151. }
  152. return demangled_name_view;
  153. }
  154. template <class OutputIt>
  155. auto normalize_msvc_abi_name(string_view abi_name_view, OutputIt out)
  156. -> OutputIt {
  157. const string_view demangled_name(abi_name_view);
  158. for (size_t i = 0; i < demangled_name.size(); ++i) {
  159. auto sub = demangled_name;
  160. sub.remove_prefix(i);
  161. if (sub.starts_with("enum ")) {
  162. i += 4;
  163. continue;
  164. }
  165. if (sub.starts_with("class ") || sub.starts_with("union ")) {
  166. i += 5;
  167. continue;
  168. }
  169. if (sub.starts_with("struct ")) {
  170. i += 6;
  171. continue;
  172. }
  173. if (*sub.begin() != ' ') *out++ = *sub.begin();
  174. }
  175. return out;
  176. }
  177. template <typename OutputIt>
  178. auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt {
  179. # ifdef FMT_HAS_ABI_CXA_DEMANGLE
  180. int status = 0;
  181. size_t size = 0;
  182. std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
  183. abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &free);
  184. string_view demangled_name_view;
  185. if (demangled_name_ptr) {
  186. demangled_name_view = normalize_libcxx_inline_namespaces(
  187. demangled_name_ptr.get(), demangled_name_ptr.get());
  188. } else {
  189. demangled_name_view = string_view(ti.name());
  190. }
  191. return detail::write_bytes<char>(out, demangled_name_view);
  192. # elif FMT_MSC_VERSION && defined(_MSVC_STL_UPDATE)
  193. return normalize_msvc_abi_name(ti.name(), out);
  194. # elif FMT_MSC_VERSION && defined(_LIBCPP_VERSION)
  195. const string_view demangled_name = ti.name();
  196. std::string name_copy(demangled_name.size(), '\0');
  197. // normalize_msvc_abi_name removes class, struct, union etc that MSVC has in
  198. // front of types
  199. name_copy.erase(normalize_msvc_abi_name(demangled_name, name_copy.begin()),
  200. name_copy.end());
  201. // normalize_libcxx_inline_namespaces removes the inline __1, __2, etc
  202. // namespaces libc++ uses for ABI versioning On MSVC ABI + libc++
  203. // environments, we need to eliminate both of them.
  204. const string_view normalized_name =
  205. normalize_libcxx_inline_namespaces(name_copy, name_copy.data());
  206. return detail::write_bytes<char>(out, normalized_name);
  207. # else
  208. return detail::write_bytes<char>(out, string_view(ti.name()));
  209. # endif
  210. }
  211. #endif // FMT_USE_RTTI
  212. template <typename T, typename Enable = void>
  213. struct has_flip : std::false_type {};
  214. template <typename T>
  215. struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
  216. : std::true_type {};
  217. template <typename T> struct is_bit_reference_like {
  218. static constexpr bool value = std::is_convertible<T, bool>::value &&
  219. std::is_nothrow_assignable<T, bool>::value &&
  220. has_flip<T>::value;
  221. };
  222. // Workaround for libc++ incompatibility with C++ standard.
  223. // According to the Standard, `bitset::operator[] const` returns bool.
  224. #if defined(_LIBCPP_VERSION) && !defined(FMT_IMPORT_STD)
  225. template <typename C>
  226. struct is_bit_reference_like<std::__bit_const_reference<C>> {
  227. static constexpr bool value = true;
  228. };
  229. #endif
  230. template <typename T, typename Enable = void>
  231. struct has_format_as : std::false_type {};
  232. template <typename T>
  233. struct has_format_as<T, void_t<decltype(format_as(std::declval<const T&>()))>>
  234. : std::true_type {};
  235. template <typename T, typename Enable = void>
  236. struct has_format_as_member : std::false_type {};
  237. template <typename T>
  238. struct has_format_as_member<
  239. T, void_t<decltype(formatter<T>::format_as(std::declval<const T&>()))>>
  240. : std::true_type {};
  241. } // namespace detail
  242. template <typename T, typename Deleter>
  243. auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
  244. return p.get();
  245. }
  246. template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
  247. return p.get();
  248. }
  249. #if FMT_CPP_LIB_FILESYSTEM
  250. template <typename Char> struct formatter<std::filesystem::path, Char> {
  251. private:
  252. format_specs specs_;
  253. detail::arg_ref<Char> width_ref_;
  254. bool debug_ = false;
  255. char path_type_ = 0;
  256. public:
  257. FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
  258. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) {
  259. auto it = ctx.begin(), end = ctx.end();
  260. if (it == end) return it;
  261. it = detail::parse_align(it, end, specs_);
  262. if (it == end) return it;
  263. Char c = *it;
  264. if ((c >= '0' && c <= '9') || c == '{')
  265. it = detail::parse_width(it, end, specs_, width_ref_, ctx);
  266. if (it != end && *it == '?') {
  267. debug_ = true;
  268. ++it;
  269. }
  270. if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++);
  271. return it;
  272. }
  273. template <typename FormatContext>
  274. auto format(const std::filesystem::path& p, FormatContext& ctx) const {
  275. auto specs = specs_;
  276. auto path_string =
  277. !path_type_ ? p.native()
  278. : p.generic_string<std::filesystem::path::value_type>();
  279. detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
  280. ctx);
  281. if (!debug_) {
  282. auto s = detail::get_path_string<Char>(p, path_string);
  283. return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
  284. }
  285. auto quoted = basic_memory_buffer<Char>();
  286. detail::write_escaped_path(quoted, p, path_string);
  287. return detail::write(ctx.out(),
  288. basic_string_view<Char>(quoted.data(), quoted.size()),
  289. specs);
  290. }
  291. };
  292. class path : public std::filesystem::path {
  293. public:
  294. auto display_string() const -> std::string {
  295. const std::filesystem::path& base = *this;
  296. return fmt::format(FMT_STRING("{}"), base);
  297. }
  298. auto system_string() const -> std::string { return string(); }
  299. auto generic_display_string() const -> std::string {
  300. const std::filesystem::path& base = *this;
  301. return fmt::format(FMT_STRING("{:g}"), base);
  302. }
  303. auto generic_system_string() const -> std::string { return generic_string(); }
  304. };
  305. #endif // FMT_CPP_LIB_FILESYSTEM
  306. template <size_t N, typename Char>
  307. struct formatter<std::bitset<N>, Char>
  308. : nested_formatter<basic_string_view<Char>, Char> {
  309. private:
  310. // This is a functor because C++11 doesn't support generic lambdas.
  311. struct writer {
  312. const std::bitset<N>& bs;
  313. template <typename OutputIt>
  314. FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
  315. for (auto pos = N; pos > 0; --pos)
  316. out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
  317. return out;
  318. }
  319. };
  320. public:
  321. template <typename FormatContext>
  322. auto format(const std::bitset<N>& bs, FormatContext& ctx) const
  323. -> decltype(ctx.out()) {
  324. return this->write_padded(ctx, writer{bs});
  325. }
  326. };
  327. template <typename Char>
  328. struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
  329. #ifdef __cpp_lib_optional
  330. template <typename T, typename Char>
  331. struct formatter<std::optional<T>, Char,
  332. std::enable_if_t<is_formattable<T, Char>::value>> {
  333. private:
  334. formatter<std::remove_cv_t<T>, Char> underlying_;
  335. static constexpr basic_string_view<Char> optional =
  336. detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l',
  337. '('>{};
  338. static constexpr basic_string_view<Char> none =
  339. detail::string_literal<Char, 'n', 'o', 'n', 'e'>{};
  340. public:
  341. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) {
  342. detail::maybe_set_debug_format(underlying_, true);
  343. return underlying_.parse(ctx);
  344. }
  345. template <typename FormatContext>
  346. auto format(const std::optional<T>& opt, FormatContext& ctx) const
  347. -> decltype(ctx.out()) {
  348. if (!opt) return detail::write<Char>(ctx.out(), none);
  349. auto out = ctx.out();
  350. out = detail::write<Char>(out, optional);
  351. ctx.advance_to(out);
  352. out = underlying_.format(*opt, ctx);
  353. return detail::write(out, ')');
  354. }
  355. };
  356. #endif // __cpp_lib_optional
  357. #ifdef __cpp_lib_expected
  358. template <typename T, typename E, typename Char>
  359. struct formatter<std::expected<T, E>, Char,
  360. std::enable_if_t<(std::is_void<T>::value ||
  361. is_formattable<T, Char>::value) &&
  362. is_formattable<E, Char>::value>> {
  363. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  364. return ctx.begin();
  365. }
  366. template <typename FormatContext>
  367. auto format(const std::expected<T, E>& value, FormatContext& ctx) const
  368. -> decltype(ctx.out()) {
  369. auto out = ctx.out();
  370. if (value.has_value()) {
  371. out = detail::write<Char>(out, "expected(");
  372. if constexpr (!std::is_void<T>::value)
  373. out = detail::write_escaped_alternative<Char>(out, *value, ctx);
  374. } else {
  375. out = detail::write<Char>(out, "unexpected(");
  376. out = detail::write_escaped_alternative<Char>(out, value.error(), ctx);
  377. }
  378. *out++ = ')';
  379. return out;
  380. }
  381. };
  382. #endif // __cpp_lib_expected
  383. #ifdef __cpp_lib_source_location
  384. template <> struct formatter<std::source_location> {
  385. FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); }
  386. template <typename FormatContext>
  387. auto format(const std::source_location& loc, FormatContext& ctx) const
  388. -> decltype(ctx.out()) {
  389. auto out = ctx.out();
  390. out = detail::write(out, loc.file_name());
  391. out = detail::write(out, ':');
  392. out = detail::write<char>(out, loc.line());
  393. out = detail::write(out, ':');
  394. out = detail::write<char>(out, loc.column());
  395. out = detail::write(out, ": ");
  396. out = detail::write(out, loc.function_name());
  397. return out;
  398. }
  399. };
  400. #endif
  401. #if FMT_CPP_LIB_VARIANT
  402. template <typename T> struct is_variant_like {
  403. static constexpr bool value = detail::is_variant_like_<T>::value;
  404. };
  405. template <typename Char> struct formatter<std::monostate, Char> {
  406. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  407. return ctx.begin();
  408. }
  409. template <typename FormatContext>
  410. auto format(const std::monostate&, FormatContext& ctx) const
  411. -> decltype(ctx.out()) {
  412. return detail::write<Char>(ctx.out(), "monostate");
  413. }
  414. };
  415. template <typename Variant, typename Char>
  416. struct formatter<Variant, Char,
  417. std::enable_if_t<std::conjunction_v<
  418. is_variant_like<Variant>,
  419. detail::is_variant_formattable<Variant, Char>>>> {
  420. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  421. return ctx.begin();
  422. }
  423. template <typename FormatContext>
  424. auto format(const Variant& value, FormatContext& ctx) const
  425. -> decltype(ctx.out()) {
  426. auto out = ctx.out();
  427. out = detail::write<Char>(out, "variant(");
  428. FMT_TRY {
  429. std::visit(
  430. [&](const auto& v) {
  431. out = detail::write_escaped_alternative<Char>(out, v, ctx);
  432. },
  433. value);
  434. }
  435. FMT_CATCH(const std::bad_variant_access&) {
  436. detail::write<Char>(out, "valueless by exception");
  437. }
  438. *out++ = ')';
  439. return out;
  440. }
  441. };
  442. #endif // FMT_CPP_LIB_VARIANT
  443. template <> struct formatter<std::error_code> {
  444. private:
  445. format_specs specs_;
  446. detail::arg_ref<char> width_ref_;
  447. bool debug_ = false;
  448. public:
  449. FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
  450. FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
  451. auto it = ctx.begin(), end = ctx.end();
  452. if (it == end) return it;
  453. it = detail::parse_align(it, end, specs_);
  454. char c = *it;
  455. if (it != end && ((c >= '0' && c <= '9') || c == '{'))
  456. it = detail::parse_width(it, end, specs_, width_ref_, ctx);
  457. if (it != end && *it == '?') {
  458. debug_ = true;
  459. ++it;
  460. }
  461. if (it != end && *it == 's') {
  462. specs_.set_type(presentation_type::string);
  463. ++it;
  464. }
  465. return it;
  466. }
  467. template <typename FormatContext>
  468. FMT_CONSTEXPR20 auto format(const std::error_code& ec,
  469. FormatContext& ctx) const -> decltype(ctx.out()) {
  470. auto specs = specs_;
  471. detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
  472. ctx);
  473. auto buf = memory_buffer();
  474. if (specs_.type() == presentation_type::string) {
  475. buf.append(ec.message());
  476. } else {
  477. buf.append(string_view(ec.category().name()));
  478. buf.push_back(':');
  479. detail::write<char>(appender(buf), ec.value());
  480. }
  481. auto quoted = memory_buffer();
  482. auto str = string_view(buf.data(), buf.size());
  483. if (debug_) {
  484. detail::write_escaped_string<char>(std::back_inserter(quoted), str);
  485. str = string_view(quoted.data(), quoted.size());
  486. }
  487. return detail::write<char>(ctx.out(), str, specs);
  488. }
  489. };
  490. #if FMT_USE_RTTI
  491. template <> struct formatter<std::type_info> {
  492. public:
  493. FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
  494. return ctx.begin();
  495. }
  496. template <typename Context>
  497. auto format(const std::type_info& ti, Context& ctx) const
  498. -> decltype(ctx.out()) {
  499. return detail::write_demangled_name(ctx.out(), ti);
  500. }
  501. };
  502. #endif // FMT_USE_RTTI
  503. template <typename T>
  504. struct formatter<
  505. T, char,
  506. typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> {
  507. private:
  508. bool with_typename_ = false;
  509. public:
  510. FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
  511. auto it = ctx.begin();
  512. auto end = ctx.end();
  513. if (it == end || *it == '}') return it;
  514. if (*it == 't') {
  515. ++it;
  516. with_typename_ = FMT_USE_RTTI != 0;
  517. }
  518. return it;
  519. }
  520. template <typename Context>
  521. auto format(const std::exception& ex, Context& ctx) const
  522. -> decltype(ctx.out()) {
  523. auto out = ctx.out();
  524. #if FMT_USE_RTTI
  525. if (with_typename_) {
  526. out = detail::write_demangled_name(out, typeid(ex));
  527. *out++ = ':';
  528. *out++ = ' ';
  529. }
  530. #endif
  531. return detail::write_bytes<char>(out, string_view(ex.what()));
  532. }
  533. };
  534. // We can't use std::vector<bool, Allocator>::reference and
  535. // std::bitset<N>::reference because the compiler can't deduce Allocator and N
  536. // in partial specialization.
  537. template <typename BitRef, typename Char>
  538. struct formatter<BitRef, Char,
  539. enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
  540. : formatter<bool, Char> {
  541. template <typename FormatContext>
  542. FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
  543. -> decltype(ctx.out()) {
  544. return formatter<bool, Char>::format(v, ctx);
  545. }
  546. };
  547. template <typename T, typename Char>
  548. struct formatter<std::atomic<T>, Char,
  549. enable_if_t<is_formattable<T, Char>::value>>
  550. : formatter<T, Char> {
  551. template <typename FormatContext>
  552. auto format(const std::atomic<T>& v, FormatContext& ctx) const
  553. -> decltype(ctx.out()) {
  554. return formatter<T, Char>::format(v.load(), ctx);
  555. }
  556. };
  557. #ifdef __cpp_lib_atomic_flag_test
  558. template <typename Char>
  559. struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
  560. template <typename FormatContext>
  561. auto format(const std::atomic_flag& v, FormatContext& ctx) const
  562. -> decltype(ctx.out()) {
  563. return formatter<bool, Char>::format(v.test(), ctx);
  564. }
  565. };
  566. #endif // __cpp_lib_atomic_flag_test
  567. template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
  568. private:
  569. detail::dynamic_format_specs<Char> specs_;
  570. template <typename FormatContext, typename OutputIt>
  571. FMT_CONSTEXPR auto do_format(const std::complex<T>& c,
  572. detail::dynamic_format_specs<Char>& specs,
  573. FormatContext& ctx, OutputIt out) const
  574. -> OutputIt {
  575. if (c.real() != 0) {
  576. *out++ = Char('(');
  577. out = detail::write<Char>(out, c.real(), specs, ctx.locale());
  578. specs.set_sign(sign::plus);
  579. out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
  580. if (!detail::isfinite(c.imag())) *out++ = Char(' ');
  581. *out++ = Char('i');
  582. *out++ = Char(')');
  583. return out;
  584. }
  585. out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
  586. if (!detail::isfinite(c.imag())) *out++ = Char(' ');
  587. *out++ = Char('i');
  588. return out;
  589. }
  590. public:
  591. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  592. if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
  593. return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
  594. detail::type_constant<T, Char>::value);
  595. }
  596. template <typename FormatContext>
  597. auto format(const std::complex<T>& c, FormatContext& ctx) const
  598. -> decltype(ctx.out()) {
  599. auto specs = specs_;
  600. if (specs.dynamic()) {
  601. detail::handle_dynamic_spec(specs.dynamic_width(), specs.width,
  602. specs.width_ref, ctx);
  603. detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
  604. specs.precision_ref, ctx);
  605. }
  606. if (specs.width == 0) return do_format(c, specs, ctx, ctx.out());
  607. auto buf = basic_memory_buffer<Char>();
  608. auto outer_specs = format_specs();
  609. outer_specs.width = specs.width;
  610. outer_specs.copy_fill_from(specs);
  611. outer_specs.set_align(specs.align());
  612. specs.width = 0;
  613. specs.set_fill({});
  614. specs.set_align(align::none);
  615. do_format(c, specs, ctx, basic_appender<Char>(buf));
  616. return detail::write<Char>(ctx.out(),
  617. basic_string_view<Char>(buf.data(), buf.size()),
  618. outer_specs);
  619. }
  620. };
  621. template <typename T, typename Char>
  622. struct formatter<std::reference_wrapper<T>, Char,
  623. // Guard against format_as because reference_wrapper is
  624. // implicitly convertible to T&.
  625. enable_if_t<is_formattable<remove_cvref_t<T>, Char>::value &&
  626. !detail::has_format_as<T>::value &&
  627. !detail::has_format_as_member<T>::value>>
  628. : formatter<remove_cvref_t<T>, Char> {
  629. template <typename FormatContext>
  630. auto format(std::reference_wrapper<T> ref, FormatContext& ctx) const
  631. -> decltype(ctx.out()) {
  632. return formatter<remove_cvref_t<T>, Char>::format(ref.get(), ctx);
  633. }
  634. };
  635. FMT_END_NAMESPACE
  636. #endif // FMT_STD_H_
  637. #else
  638. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  639. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)