std.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. template <typename Char> struct formatter<std::filesystem::path, Char> {
  103. private:
  104. format_specs specs_;
  105. detail::arg_ref<Char> width_ref_;
  106. bool debug_ = false;
  107. char path_type_ = 0;
  108. public:
  109. FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
  110. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) {
  111. auto it = ctx.begin(), end = ctx.end();
  112. if (it == end) return it;
  113. it = detail::parse_align(it, end, specs_);
  114. if (it == end) return it;
  115. Char c = *it;
  116. if ((c >= '0' && c <= '9') || c == '{')
  117. it = detail::parse_width(it, end, specs_, width_ref_, ctx);
  118. if (it != end && *it == '?') {
  119. debug_ = true;
  120. ++it;
  121. }
  122. if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++);
  123. return it;
  124. }
  125. template <typename FormatContext>
  126. auto format(const std::filesystem::path& p, FormatContext& ctx) const {
  127. auto specs = specs_;
  128. auto path_string =
  129. !path_type_ ? p.native()
  130. : p.generic_string<std::filesystem::path::value_type>();
  131. detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
  132. ctx);
  133. if (!debug_) {
  134. auto s = detail::get_path_string<Char>(p, path_string);
  135. return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
  136. }
  137. auto quoted = basic_memory_buffer<Char>();
  138. detail::write_escaped_path(quoted, p, path_string);
  139. return detail::write(ctx.out(),
  140. basic_string_view<Char>(quoted.data(), quoted.size()),
  141. specs);
  142. }
  143. };
  144. class path : public std::filesystem::path {
  145. public:
  146. auto display_string() const -> std::string {
  147. const std::filesystem::path& base = *this;
  148. return fmt::format(FMT_STRING("{}"), base);
  149. }
  150. auto system_string() const -> std::string { return string(); }
  151. auto generic_display_string() const -> std::string {
  152. const std::filesystem::path& base = *this;
  153. return fmt::format(FMT_STRING("{:g}"), base);
  154. }
  155. auto generic_system_string() const -> std::string { return generic_string(); }
  156. };
  157. FMT_END_NAMESPACE
  158. #endif // FMT_CPP_LIB_FILESYSTEM
  159. FMT_BEGIN_NAMESPACE
  160. template <std::size_t N, typename Char>
  161. struct formatter<std::bitset<N>, Char>
  162. : nested_formatter<basic_string_view<Char>, Char> {
  163. private:
  164. // Functor because C++11 doesn't support generic lambdas.
  165. struct writer {
  166. const std::bitset<N>& bs;
  167. template <typename OutputIt>
  168. FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
  169. for (auto pos = N; pos > 0; --pos) {
  170. out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
  171. }
  172. return out;
  173. }
  174. };
  175. public:
  176. template <typename FormatContext>
  177. auto format(const std::bitset<N>& bs, FormatContext& ctx) const
  178. -> decltype(ctx.out()) {
  179. return this->write_padded(ctx, writer{bs});
  180. }
  181. };
  182. template <typename Char>
  183. struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
  184. FMT_END_NAMESPACE
  185. #ifdef __cpp_lib_optional
  186. FMT_BEGIN_NAMESPACE
  187. template <typename T, typename Char>
  188. struct formatter<std::optional<T>, Char,
  189. std::enable_if_t<is_formattable<T, Char>::value>> {
  190. private:
  191. formatter<T, Char> underlying_;
  192. static constexpr basic_string_view<Char> optional =
  193. detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l',
  194. '('>{};
  195. static constexpr basic_string_view<Char> none =
  196. detail::string_literal<Char, 'n', 'o', 'n', 'e'>{};
  197. template <class U>
  198. FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set)
  199. -> decltype(u.set_debug_format(set)) {
  200. u.set_debug_format(set);
  201. }
  202. template <class U>
  203. FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {}
  204. public:
  205. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) {
  206. maybe_set_debug_format(underlying_, true);
  207. return underlying_.parse(ctx);
  208. }
  209. template <typename FormatContext>
  210. auto format(const std::optional<T>& opt, FormatContext& ctx) const
  211. -> decltype(ctx.out()) {
  212. if (!opt) return detail::write<Char>(ctx.out(), none);
  213. auto out = ctx.out();
  214. out = detail::write<Char>(out, optional);
  215. ctx.advance_to(out);
  216. out = underlying_.format(*opt, ctx);
  217. return detail::write(out, ')');
  218. }
  219. };
  220. FMT_END_NAMESPACE
  221. #endif // __cpp_lib_optional
  222. #if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT
  223. FMT_BEGIN_NAMESPACE
  224. namespace detail {
  225. template <typename Char, typename OutputIt, typename T>
  226. auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt {
  227. if constexpr (has_to_string_view<T>::value)
  228. return write_escaped_string<Char>(out, detail::to_string_view(v));
  229. if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
  230. return write<Char>(out, v);
  231. }
  232. } // namespace detail
  233. FMT_END_NAMESPACE
  234. #endif
  235. #ifdef __cpp_lib_expected
  236. FMT_BEGIN_NAMESPACE
  237. template <typename T, typename E, typename Char>
  238. struct formatter<std::expected<T, E>, Char,
  239. std::enable_if_t<(std::is_void<T>::value ||
  240. is_formattable<T, Char>::value) &&
  241. is_formattable<E, Char>::value>> {
  242. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  243. return ctx.begin();
  244. }
  245. template <typename FormatContext>
  246. auto format(const std::expected<T, E>& value, FormatContext& ctx) const
  247. -> decltype(ctx.out()) {
  248. auto out = ctx.out();
  249. if (value.has_value()) {
  250. out = detail::write<Char>(out, "expected(");
  251. if constexpr (!std::is_void<T>::value)
  252. out = detail::write_escaped_alternative<Char>(out, *value);
  253. } else {
  254. out = detail::write<Char>(out, "unexpected(");
  255. out = detail::write_escaped_alternative<Char>(out, value.error());
  256. }
  257. *out++ = ')';
  258. return out;
  259. }
  260. };
  261. FMT_END_NAMESPACE
  262. #endif // __cpp_lib_expected
  263. #ifdef __cpp_lib_source_location
  264. FMT_BEGIN_NAMESPACE
  265. template <> struct formatter<std::source_location> {
  266. FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); }
  267. template <typename FormatContext>
  268. auto format(const std::source_location& loc, FormatContext& ctx) const
  269. -> decltype(ctx.out()) {
  270. auto out = ctx.out();
  271. out = detail::write(out, loc.file_name());
  272. out = detail::write(out, ':');
  273. out = detail::write<char>(out, loc.line());
  274. out = detail::write(out, ':');
  275. out = detail::write<char>(out, loc.column());
  276. out = detail::write(out, ": ");
  277. out = detail::write(out, loc.function_name());
  278. return out;
  279. }
  280. };
  281. FMT_END_NAMESPACE
  282. #endif
  283. #if FMT_CPP_LIB_VARIANT
  284. FMT_BEGIN_NAMESPACE
  285. namespace detail {
  286. template <typename T>
  287. using variant_index_sequence =
  288. std::make_index_sequence<std::variant_size<T>::value>;
  289. template <typename> struct is_variant_like_ : std::false_type {};
  290. template <typename... Types>
  291. struct is_variant_like_<std::variant<Types...>> : std::true_type {};
  292. // formattable element check.
  293. template <typename T, typename C> class is_variant_formattable_ {
  294. template <std::size_t... Is>
  295. static std::conjunction<
  296. is_formattable<std::variant_alternative_t<Is, T>, C>...>
  297. check(std::index_sequence<Is...>);
  298. public:
  299. static constexpr const bool value =
  300. decltype(check(variant_index_sequence<T>{}))::value;
  301. };
  302. } // namespace detail
  303. template <typename T> struct is_variant_like {
  304. static constexpr const bool value = detail::is_variant_like_<T>::value;
  305. };
  306. template <typename T, typename C> struct is_variant_formattable {
  307. static constexpr const bool value =
  308. detail::is_variant_formattable_<T, C>::value;
  309. };
  310. template <typename Char> struct formatter<std::monostate, Char> {
  311. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  312. return ctx.begin();
  313. }
  314. template <typename FormatContext>
  315. auto format(const std::monostate&, FormatContext& ctx) const
  316. -> decltype(ctx.out()) {
  317. return detail::write<Char>(ctx.out(), "monostate");
  318. }
  319. };
  320. template <typename Variant, typename Char>
  321. struct formatter<
  322. Variant, Char,
  323. std::enable_if_t<std::conjunction_v<
  324. is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> {
  325. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  326. return ctx.begin();
  327. }
  328. template <typename FormatContext>
  329. auto format(const Variant& value, FormatContext& ctx) const
  330. -> decltype(ctx.out()) {
  331. auto out = ctx.out();
  332. out = detail::write<Char>(out, "variant(");
  333. FMT_TRY {
  334. std::visit(
  335. [&](const auto& v) {
  336. out = detail::write_escaped_alternative<Char>(out, v);
  337. },
  338. value);
  339. }
  340. FMT_CATCH(const std::bad_variant_access&) {
  341. detail::write<Char>(out, "valueless by exception");
  342. }
  343. *out++ = ')';
  344. return out;
  345. }
  346. };
  347. FMT_END_NAMESPACE
  348. #endif // FMT_CPP_LIB_VARIANT
  349. FMT_BEGIN_NAMESPACE
  350. template <> struct formatter<std::error_code> {
  351. private:
  352. format_specs specs_;
  353. detail::arg_ref<char> width_ref_;
  354. bool debug_ = false;
  355. public:
  356. FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
  357. auto it = ctx.begin(), end = ctx.end();
  358. if (it == end) return it;
  359. it = detail::parse_align(it, end, specs_);
  360. char c = *it;
  361. if (it != end && ((c >= '0' && c <= '9') || c == '{'))
  362. it = detail::parse_width(it, end, specs_, width_ref_, ctx);
  363. if (it != end && *it == '?') {
  364. debug_ = true;
  365. ++it;
  366. }
  367. if (it != end && *it == 's') {
  368. specs_.set_type(presentation_type::string);
  369. ++it;
  370. }
  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. auto buf = memory_buffer();
  380. if (specs_.type() == presentation_type::string) {
  381. buf.append(ec.message());
  382. } else {
  383. buf.append(string_view(ec.category().name()));
  384. buf.push_back(':');
  385. detail::write<char>(appender(buf), ec.value());
  386. }
  387. auto quoted = memory_buffer();
  388. auto str = string_view(buf.data(), buf.size());
  389. if (debug_) {
  390. detail::write_escaped_string<char>(std::back_inserter(quoted), str);
  391. str = string_view(quoted.data(), quoted.size());
  392. }
  393. return detail::write<char>(ctx.out(), str, specs);
  394. }
  395. };
  396. #if FMT_USE_RTTI
  397. namespace detail {
  398. template <typename Char, typename OutputIt>
  399. auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt {
  400. # ifdef FMT_HAS_ABI_CXA_DEMANGLE
  401. int status = 0;
  402. std::size_t size = 0;
  403. std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
  404. abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
  405. string_view demangled_name_view;
  406. if (demangled_name_ptr) {
  407. demangled_name_view = demangled_name_ptr.get();
  408. // Normalization of stdlib inline namespace names.
  409. // libc++ inline namespaces.
  410. // std::__1::* -> std::*
  411. // std::__1::__fs::* -> std::*
  412. // libstdc++ inline namespaces.
  413. // std::__cxx11::* -> std::*
  414. // std::filesystem::__cxx11::* -> std::filesystem::*
  415. if (demangled_name_view.starts_with("std::")) {
  416. char* begin = demangled_name_ptr.get();
  417. char* to = begin + 5; // std::
  418. for (char *from = to, *end = begin + demangled_name_view.size();
  419. from < end;) {
  420. // This is safe, because demangled_name is NUL-terminated.
  421. if (from[0] == '_' && from[1] == '_') {
  422. char* next = from + 1;
  423. while (next < end && *next != ':') next++;
  424. if (next[0] == ':' && next[1] == ':') {
  425. from = next + 2;
  426. continue;
  427. }
  428. }
  429. *to++ = *from++;
  430. }
  431. demangled_name_view = {begin, detail::to_unsigned(to - begin)};
  432. }
  433. } else {
  434. demangled_name_view = string_view(ti.name());
  435. }
  436. return detail::write_bytes<Char>(out, demangled_name_view);
  437. # elif FMT_MSC_VERSION
  438. const string_view demangled_name(ti.name());
  439. for (std::size_t i = 0; i < demangled_name.size(); ++i) {
  440. auto sub = demangled_name;
  441. sub.remove_prefix(i);
  442. if (sub.starts_with("enum ")) {
  443. i += 4;
  444. continue;
  445. }
  446. if (sub.starts_with("class ") || sub.starts_with("union ")) {
  447. i += 5;
  448. continue;
  449. }
  450. if (sub.starts_with("struct ")) {
  451. i += 6;
  452. continue;
  453. }
  454. if (*sub.begin() != ' ') *out++ = *sub.begin();
  455. }
  456. return out;
  457. # else
  458. return detail::write_bytes<Char>(out, string_view(ti.name()));
  459. # endif
  460. }
  461. } // namespace detail
  462. template <typename Char>
  463. struct formatter<std::type_info, Char // DEPRECATED! Mixing code unit types.
  464. > {
  465. public:
  466. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  467. return ctx.begin();
  468. }
  469. template <typename Context>
  470. auto format(const std::type_info& ti, Context& ctx) const
  471. -> decltype(ctx.out()) {
  472. return detail::write_demangled_name<Char>(ctx.out(), ti);
  473. }
  474. };
  475. #endif
  476. template <typename T, typename Char>
  477. struct formatter<
  478. T, Char, // DEPRECATED! Mixing code unit types.
  479. typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> {
  480. private:
  481. bool with_typename_ = false;
  482. public:
  483. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  484. auto it = ctx.begin();
  485. auto end = ctx.end();
  486. if (it == end || *it == '}') return it;
  487. if (*it == 't') {
  488. ++it;
  489. with_typename_ = FMT_USE_RTTI != 0;
  490. }
  491. return it;
  492. }
  493. template <typename Context>
  494. auto format(const std::exception& ex, Context& ctx) const
  495. -> decltype(ctx.out()) {
  496. auto out = ctx.out();
  497. #if FMT_USE_RTTI
  498. if (with_typename_) {
  499. out = detail::write_demangled_name<Char>(out, typeid(ex));
  500. *out++ = ':';
  501. *out++ = ' ';
  502. }
  503. #endif
  504. return detail::write_bytes<Char>(out, string_view(ex.what()));
  505. }
  506. };
  507. namespace detail {
  508. template <typename T, typename Enable = void>
  509. struct has_flip : std::false_type {};
  510. template <typename T>
  511. struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
  512. : std::true_type {};
  513. template <typename T> struct is_bit_reference_like {
  514. static constexpr const bool value =
  515. std::is_convertible<T, bool>::value &&
  516. std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value;
  517. };
  518. #ifdef _LIBCPP_VERSION
  519. // Workaround for libc++ incompatibility with C++ standard.
  520. // According to the Standard, `bitset::operator[] const` returns bool.
  521. template <typename C>
  522. struct is_bit_reference_like<std::__bit_const_reference<C>> {
  523. static constexpr const bool value = true;
  524. };
  525. #endif
  526. } // namespace detail
  527. // We can't use std::vector<bool, Allocator>::reference and
  528. // std::bitset<N>::reference because the compiler can't deduce Allocator and N
  529. // in partial specialization.
  530. template <typename BitRef, typename Char>
  531. struct formatter<BitRef, Char,
  532. enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
  533. : formatter<bool, Char> {
  534. template <typename FormatContext>
  535. FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
  536. -> decltype(ctx.out()) {
  537. return formatter<bool, Char>::format(v, ctx);
  538. }
  539. };
  540. template <typename T, typename Deleter>
  541. auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
  542. return p.get();
  543. }
  544. template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
  545. return p.get();
  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. 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_