fmt.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_FMT_HPP__
  11. #define __ASIO2_FMT_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/detail/push_options.hpp>
  16. #include <asio2/util/string.hpp>
  17. #include <string_view>
  18. #ifndef ASIO2_DISABLE_AUTO_HEADER_ONLY
  19. #ifndef FMT_HEADER_ONLY
  20. #define FMT_HEADER_ONLY
  21. #endif
  22. #endif
  23. // used to compatible with the UE4 "check" macro
  24. #pragma push_macro("check")
  25. #undef check
  26. #include <fmt/format.h>
  27. #include <fmt/args.h>
  28. #include <fmt/chrono.h>
  29. #include <fmt/color.h>
  30. #include <fmt/compile.h>
  31. #include <fmt/os.h>
  32. #include <fmt/ostream.h>
  33. #include <fmt/printf.h>
  34. #include <fmt/ranges.h>
  35. #include <fmt/xchar.h>
  36. // https://fmt.dev/latest/api.html#udt
  37. // Custom format for MFC/ATL CString
  38. #if defined(__AFXSTR_H__) || defined(__ATLSTR_H__)
  39. #if __has_include(<afxstr.h>) || __has_include(<atlstr.h>)
  40. template <>
  41. struct fmt::formatter<CStringA, char>
  42. {
  43. template<typename FormatParseContext>
  44. constexpr auto parse(FormatParseContext& ctx) -> decltype(ctx.begin())
  45. {
  46. // Parse the presentation format and store it in the formatter:
  47. auto it = ctx.begin(), end = ctx.end();
  48. // Check if reached the end of the range:
  49. if (it != end && *it != '}') throw format_error("invalid format");
  50. // Return an iterator past the end of the parsed range:
  51. return it;
  52. }
  53. template <typename FormatContext>
  54. auto format(const CStringA& s, FormatContext& ctx) -> decltype(ctx.out())
  55. {
  56. return format_to(ctx.out(), "{}", (LPCSTR)s);
  57. }
  58. };
  59. // CStringA s;
  60. // fmt::format(L"{}", s);
  61. // above code will compile failed, beacuse the CStringA can be implicitly converted to const char*
  62. // then then fmt::format(L"{}", (const char*)...); will compile failed.
  63. template <>
  64. struct fmt::formatter<CStringA, wchar_t>
  65. {
  66. template<typename FormatParseContext>
  67. constexpr auto parse(FormatParseContext& ctx) -> decltype(ctx.begin())
  68. {
  69. // Parse the presentation format and store it in the formatter:
  70. auto it = ctx.begin(), end = ctx.end();
  71. // Check if reached the end of the range:
  72. if (it != end && *it != '}') throw format_error("invalid format");
  73. // Return an iterator past the end of the parsed range:
  74. return it;
  75. }
  76. template <typename FormatContext>
  77. auto format(const CStringA& s, FormatContext& ctx) -> decltype(ctx.out())
  78. {
  79. return format_to(ctx.out(), L"{}", (LPCWSTR)CStringW(s));
  80. }
  81. };
  82. template <>
  83. struct fmt::formatter<CStringW, char>
  84. {
  85. template<typename FormatParseContext>
  86. constexpr auto parse(FormatParseContext& ctx) -> decltype(ctx.begin())
  87. {
  88. // Parse the presentation format and store it in the formatter:
  89. auto it = ctx.begin(), end = ctx.end();
  90. // Check if reached the end of the range:
  91. if (it != end && *it != '}') throw format_error("invalid format");
  92. // Return an iterator past the end of the parsed range:
  93. return it;
  94. }
  95. template <typename FormatContext>
  96. auto format(const CStringW& s, FormatContext& ctx) -> decltype(ctx.out())
  97. {
  98. return format_to(ctx.out(), "{}", (LPCSTR)CStringA(s));
  99. }
  100. };
  101. template <>
  102. struct fmt::formatter<CStringW, wchar_t>
  103. {
  104. template<typename FormatParseContext>
  105. constexpr auto parse(FormatParseContext& ctx) -> decltype(ctx.begin())
  106. {
  107. // Parse the presentation format and store it in the formatter:
  108. auto it = ctx.begin(), end = ctx.end();
  109. // Check if reached the end of the range:
  110. if (it != end && *it != '}') throw format_error("invalid format");
  111. // Return an iterator past the end of the parsed range:
  112. return it;
  113. }
  114. template <typename FormatContext>
  115. auto format(const CStringW& s, FormatContext& ctx) -> decltype(ctx.out())
  116. {
  117. return format_to(ctx.out(), L"{}", (LPCWSTR)s);
  118. }
  119. };
  120. #endif
  121. #endif
  122. // Custom format for wxWidgets wxString
  123. // beacuse the wxString can be implicitly converted to const char* and const wchar_t*
  124. // so the wxString can be use fmt::format("{}", wxString()); directly.
  125. #if defined(_WX_WXSTRING_H__) && defined(ASIO2_ENABLE_WXSTRING_FORMATTER)
  126. #if __has_include(<wx/string.h>)
  127. template <>
  128. struct fmt::formatter<wxString, char>
  129. {
  130. template<typename FormatParseContext>
  131. constexpr auto parse(FormatParseContext& ctx) -> decltype(ctx.begin())
  132. {
  133. // Parse the presentation format and store it in the formatter:
  134. auto it = ctx.begin(), end = ctx.end();
  135. // Check if reached the end of the range:
  136. if (it != end && *it != '}') throw format_error("invalid format");
  137. // Return an iterator past the end of the parsed range:
  138. return it;
  139. }
  140. template <typename FormatContext>
  141. auto format(const wxString& s, FormatContext& ctx) -> decltype(ctx.out())
  142. {
  143. return format_to(ctx.out(), "{}", (const char*)s);
  144. }
  145. };
  146. template <>
  147. struct fmt::formatter<wxString, wchar_t>
  148. {
  149. template<typename FormatParseContext>
  150. constexpr auto parse(FormatParseContext& ctx) -> decltype(ctx.begin())
  151. {
  152. // Parse the presentation format and store it in the formatter:
  153. auto it = ctx.begin(), end = ctx.end();
  154. // Check if reached the end of the range:
  155. if (it != end && *it != '}') throw format_error("invalid format");
  156. // Return an iterator past the end of the parsed range:
  157. return it;
  158. }
  159. template <typename FormatContext>
  160. auto format(const wxString& s, FormatContext& ctx) -> decltype(ctx.out())
  161. {
  162. return format_to(ctx.out(), L"{}", (const wchar_t*)s);
  163. }
  164. };
  165. #endif
  166. #endif
  167. FMT_BEGIN_NAMESPACE
  168. namespace detail {
  169. template<class AllFmt, class Tuple, class FmtStr, class T, class... Args>
  170. auto kvformat_cat(AllFmt& allfmt, Tuple&& tp_vals, FmtStr&& fmtstr, T&& val, Args&&... args)
  171. {
  172. allfmt += ::std::forward<FmtStr>(fmtstr);
  173. if constexpr (sizeof...(Args) > ::std::size_t(0))
  174. {
  175. return detail::kvformat_cat(allfmt,
  176. ::std::tuple_cat(::std::forward<Tuple>(tp_vals), ::std::tuple(::std::forward<T>(val))),
  177. ::std::forward<Args>(args)...);
  178. }
  179. else
  180. {
  181. return ::std::tuple_cat(::std::forward<Tuple>(tp_vals), ::std::tuple(::std::forward<T>(val)));
  182. }
  183. }
  184. template<class CharT, class Tuple, ::std::size_t... I>
  185. auto kvformat_do(::std::basic_string<CharT>& allfmt, Tuple&& tp_vals, ::std::index_sequence<I...>)
  186. {
  187. if constexpr (::std::is_same_v<CharT, wchar_t>)
  188. {
  189. return fmt::vformat(fmt::wstring_view(allfmt),
  190. fmt::make_wformat_args(::std::get<I>(::std::forward<Tuple>(tp_vals))...));
  191. }
  192. else
  193. {
  194. return fmt::vformat(allfmt,
  195. fmt::make_format_args(::std::get<I>(::std::forward<Tuple>(tp_vals))...));
  196. }
  197. }
  198. template<class CharT, class Tuple>
  199. auto kvformat_do(::std::basic_string<CharT>& allfmt, Tuple&& tp_vals)
  200. {
  201. return detail::kvformat_do(allfmt, ::std::forward<Tuple>(tp_vals),
  202. ::std::make_index_sequence<::std::tuple_size_v<::std::remove_cv_t<::std::remove_reference_t<Tuple>>>>{});
  203. }
  204. } // namespace detail
  205. template<class FmtStr, class T, class... Args>
  206. auto kvformat(FmtStr&& fmtstr, T&& val, Args&&... args)
  207. {
  208. auto allfmt = asio2::to_basic_string(::std::forward<FmtStr>(fmtstr));
  209. if constexpr (sizeof...(Args) > ::std::size_t(0))
  210. {
  211. return detail::kvformat_do(allfmt, detail::kvformat_cat(allfmt,
  212. ::std::tuple(::std::forward<T>(val)), ::std::forward<Args>(args)...));
  213. }
  214. else
  215. {
  216. // this will compile failed on c++ 20
  217. //return fmt::format(::std::forward<FmtStr>(fmtstr), ::std::forward<T>(val));
  218. return detail::kvformat_do(allfmt, ::std::tuple(::std::forward<T>(val)));
  219. }
  220. }
  221. FMT_END_NAMESPACE
  222. #pragma pop_macro("check")
  223. #include <asio2/base/detail/pop_options.hpp>
  224. #endif // !__ASIO2_FMT_HPP__