诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

137 rivejä
4.2 KiB

  1. // Formatting library for C++ - std::ostream support
  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_OSTREAM_H_
  8. #define FMT_OSTREAM_H_
  9. #include <ostream>
  10. #include "format.h"
  11. FMT_BEGIN_NAMESPACE
  12. namespace internal {
  13. template <class Char> class formatbuf : public std::basic_streambuf<Char> {
  14. private:
  15. using int_type = typename std::basic_streambuf<Char>::int_type;
  16. using traits_type = typename std::basic_streambuf<Char>::traits_type;
  17. buffer<Char>& buffer_;
  18. public:
  19. formatbuf(buffer<Char>& buf) : buffer_(buf) {}
  20. protected:
  21. // The put-area is actually always empty. This makes the implementation
  22. // simpler and has the advantage that the streambuf and the buffer are always
  23. // in sync and sputc never writes into uninitialized memory. The obvious
  24. // disadvantage is that each call to sputc always results in a (virtual) call
  25. // to overflow. There is no disadvantage here for sputn since this always
  26. // results in a call to xsputn.
  27. int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
  28. if (!traits_type::eq_int_type(ch, traits_type::eof()))
  29. buffer_.push_back(static_cast<Char>(ch));
  30. return ch;
  31. }
  32. std::streamsize xsputn(const Char* s, std::streamsize count) FMT_OVERRIDE {
  33. buffer_.append(s, s + count);
  34. return count;
  35. }
  36. };
  37. template <typename Char> struct test_stream : std::basic_ostream<Char> {
  38. private:
  39. struct null;
  40. // Hide all operator<< from std::basic_ostream<Char>.
  41. void operator<<(null);
  42. };
  43. // Checks if T has a user-defined operator<< (e.g. not a member of
  44. // std::ostream).
  45. template <typename T, typename Char> class is_streamable {
  46. private:
  47. template <typename U>
  48. static decltype((void)(std::declval<test_stream<Char>&>()
  49. << std::declval<U>()),
  50. std::true_type())
  51. test(int);
  52. template <typename> static std::false_type test(...);
  53. using result = decltype(test<T>(0));
  54. public:
  55. static const bool value = result::value;
  56. };
  57. // Write the content of buf to os.
  58. template <typename Char>
  59. void write(std::basic_ostream<Char>& os, buffer<Char>& buf) {
  60. const Char* buf_data = buf.data();
  61. using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
  62. unsigned_streamsize size = buf.size();
  63. unsigned_streamsize max_size =
  64. to_unsigned((std::numeric_limits<std::streamsize>::max)());
  65. do {
  66. unsigned_streamsize n = size <= max_size ? size : max_size;
  67. os.write(buf_data, static_cast<std::streamsize>(n));
  68. buf_data += n;
  69. size -= n;
  70. } while (size != 0);
  71. }
  72. template <typename Char, typename T>
  73. void format_value(buffer<Char>& buf, const T& value) {
  74. formatbuf<Char> format_buf(buf);
  75. std::basic_ostream<Char> output(&format_buf);
  76. output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
  77. output << value;
  78. buf.resize(buf.size());
  79. }
  80. // Formats an object of type T that has an overloaded ostream operator<<.
  81. template <typename T, typename Char>
  82. struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
  83. : formatter<basic_string_view<Char>, Char> {
  84. template <typename Context>
  85. auto format(const T& value, Context& ctx) -> decltype(ctx.out()) {
  86. basic_memory_buffer<Char> buffer;
  87. format_value(buffer, value);
  88. basic_string_view<Char> str(buffer.data(), buffer.size());
  89. return formatter<basic_string_view<Char>, Char>::format(str, ctx);
  90. }
  91. };
  92. } // namespace internal
  93. template <typename Char>
  94. void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
  95. basic_format_args<buffer_context<Char>> args) {
  96. basic_memory_buffer<Char> buffer;
  97. internal::vformat_to(buffer, format_str, args);
  98. internal::write(os, buffer);
  99. }
  100. /**
  101. \rst
  102. Prints formatted data to the stream *os*.
  103. **Example**::
  104. fmt::print(cerr, "Don't {}!", "panic");
  105. \endrst
  106. */
  107. template <typename S, typename... Args,
  108. typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
  109. void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) {
  110. vprint(os, to_string_view(format_str),
  111. {internal::make_args_checked<Args...>(format_str, args...)});
  112. }
  113. FMT_END_NAMESPACE
  114. #endif // FMT_OSTREAM_H_