std.h 22 KB

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