HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ostream.h
Go to the documentation of this file.
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 
8 #ifndef FMT_OSTREAM_H_
9 #define FMT_OSTREAM_H_
10 
11 #include <fstream> // std::filebuf
12 
13 #if defined(_WIN32) && defined(__GLIBCXX__)
14 # include <ext/stdio_filebuf.h>
15 # include <ext/stdio_sync_filebuf.h>
16 #elif defined(_WIN32) && defined(_LIBCPP_VERSION)
17 # include <__std_stream>
18 #endif
19 
20 #include "format.h"
21 
23 
24 namespace detail {
25 
26 // Generate a unique explicit instantion in every translation unit using a tag
27 // type in an anonymous namespace.
28 namespace {
29 struct file_access_tag {};
30 } // namespace
31 template <typename Tag, typename BufType, FILE* BufType::*FileMemberPtr>
32 class file_access {
33  friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
34 };
35 
36 #if FMT_MSC_VERSION
37 template class file_access<file_access_tag, std::filebuf,
38  &std::filebuf::_Myfile>;
39 auto get_file(std::filebuf&) -> FILE*;
40 #elif defined(_WIN32) && defined(_LIBCPP_VERSION)
41 template class file_access<file_access_tag, std::__stdoutbuf<char>,
42  &std::__stdoutbuf<char>::__file_>;
43 auto get_file(std::__stdoutbuf<char>&) -> FILE*;
44 #endif
45 
46 inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
47 #if FMT_MSC_VERSION
48  if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
49  if (FILE* f = get_file(*buf)) return write_console(f, data);
50 #elif defined(_WIN32) && defined(__GLIBCXX__)
51  auto* rdbuf = os.rdbuf();
52  FILE* c_file;
53  if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
54  c_file = sfbuf->file();
55  else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
56  c_file = fbuf->file();
57  else
58  return false;
59  if (c_file) return write_console(c_file, data);
60 #elif defined(_WIN32) && defined(_LIBCPP_VERSION)
61  if (auto* buf = dynamic_cast<std::__stdoutbuf<char>*>(os.rdbuf()))
62  if (FILE* f = get_file(*buf)) return write_console(f, data);
63 #else
64  ignore_unused(os, data);
65 #endif
66  return false;
67 }
68 inline bool write_ostream_unicode(std::wostream&,
69  fmt::basic_string_view<wchar_t>) {
70  return false;
71 }
72 
73 // Write the content of buf to os.
74 // It is a separate function rather than a part of vprint to simplify testing.
75 template <typename Char>
76 void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
77  const Char* buf_data = buf.data();
78  using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
79  unsigned_streamsize size = buf.size();
80  unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
81  do {
82  unsigned_streamsize n = size <= max_size ? size : max_size;
83  os.write(buf_data, static_cast<std::streamsize>(n));
84  buf_data += n;
85  size -= n;
86  } while (size != 0);
87 }
88 
89 template <typename Char, typename T>
91  locale_ref loc = locale_ref()) {
92  auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
93  auto&& output = std::basic_ostream<Char>(&format_buf);
94 #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
95  if (loc) output.imbue(loc.get<std::locale>());
96 #endif
97  output << value;
98  output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
99 }
100 
101 template <typename T> struct streamed_view { const T& value; };
102 
103 } // namespace detail
104 
105 // Formats an object of type T that has an overloaded ostream operator<<.
106 template <typename Char>
107 struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
108  void set_debug_format() = delete;
109 
110  template <typename T, typename OutputIt>
112  -> OutputIt {
114  detail::format_value(buffer, value, ctx.locale());
116  {buffer.data(), buffer.size()}, ctx);
117  }
118 };
119 
121 
122 template <typename T, typename Char>
123 struct formatter<detail::streamed_view<T>, Char>
124  : basic_ostream_formatter<Char> {
125  template <typename OutputIt>
127  basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
128  return basic_ostream_formatter<Char>::format(view.value, ctx);
129  }
130 };
131 
132 /**
133  \rst
134  Returns a view that formats `value` via an ostream ``operator<<``.
135 
136  **Example**::
137 
138  fmt::print("Current thread id: {}\n",
139  fmt::streamed(std::this_thread::get_id()));
140  \endrst
141  */
142 template <typename T>
144  return {value};
145 }
146 
147 namespace detail {
148 
149 inline void vprint_directly(std::ostream& os, string_view format_str,
150  format_args args) {
151  auto buffer = memory_buffer();
152  detail::vformat_to(buffer, format_str, args);
154 }
155 
156 } // namespace detail
157 
158 FMT_MODULE_EXPORT template <typename Char>
159 void vprint(std::basic_ostream<Char>& os,
163  detail::vformat_to(buffer, format_str, args);
164  if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return;
166 }
167 
168 /**
169  \rst
170  Prints formatted data to the stream *os*.
171 
172  **Example**::
173 
174  fmt::print(cerr, "Don't {}!", "panic");
175  \endrst
176  */
177 FMT_MODULE_EXPORT template <typename... T>
178 void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
179  const auto& vargs = fmt::make_format_args(args...);
180  if (detail::is_utf8())
181  vprint(os, fmt, vargs);
182  else
183  detail::vprint_directly(os, fmt, vargs);
184 }
185 
187 template <typename... Args>
188 void print(std::wostream& os,
190  Args&&... args) {
192 }
193 
194 FMT_MODULE_EXPORT template <typename... T>
195 void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
196  fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
197 }
198 
200 template <typename... Args>
201 void println(std::wostream& os,
203  Args&&... args) {
204  print(os, L"{}\n", fmt::format(fmt, std::forward<Args>(args)...));
205 }
206 
208 
209 #endif // FMT_OSTREAM_H_
type
Definition: core.h:556
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
auto format(const T &value, basic_format_context< OutputIt, Char > &ctx) const -> OutputIt
Definition: ostream.h:111
FMT_CONSTEXPR auto data() noexcept-> T *
Definition: core.h:907
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition: core.h:302
GLsizei const GLfloat * value
Definition: glcorearb.h:824
void vformat_to(buffer< Char > &buf, basic_string_view< Char > fmt, typename vformat_args< Char >::type args, locale_ref loc={})
FMT_CONSTEXPR auto to_unsigned(Int value) -> typename std::make_unsigned< Int >::type
Definition: core.h:374
FMT_MODULE_EXPORT void vprint(std::basic_ostream< Char > &os, basic_string_view< type_identity_t< Char >> format_str, basic_format_args< buffer_context< type_identity_t< Char >>> args)
Definition: ostream.h:159
GLuint buffer
Definition: glcorearb.h:660
bool write_ostream_unicode(std::ostream &os, fmt::string_view data)
Definition: ostream.h:46
#define FMT_END_NAMESPACE
Definition: core.h:179
basic_string_view< char > string_view
Definition: core.h:501
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
void vprint_directly(std::ostream &os, string_view format_str, format_args args)
Definition: ostream.h:149
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)
Definition: ostream.h:76
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
basic_memory_buffer< char > memory_buffer
Definition: format.h:1036
constexpr auto size() const noexcept-> size_t
Definition: core.h:901
auto format(detail::streamed_view< T > view, basic_format_context< OutputIt, Char > &ctx) const -> OutputIt
Definition: ostream.h:126
void format_value(buffer< Char > &buf, const T &value, locale_ref loc=locale_ref())
Definition: ostream.h:90
constexpr auto make_format_args(T &&...args) -> format_arg_store< Context, remove_cvref_t< T >...>
Definition: core.h:1842
GLsizeiptr size
Definition: glcorearb.h:664
typename type_identity< T >::type type_identity_t
Definition: core.h:275
FMT_MODULE_EXPORT void println(std::ostream &os, format_string< T...> fmt, T &&...args)
Definition: ostream.h:195
friend auto get_file(BufType &obj) -> FILE *
Definition: ostream.h:33
**If you just want to fire and args
Definition: thread.h:618
FMT_CONSTEXPR auto is_utf8() -> bool
Definition: core.h:380
#define FMT_BEGIN_NAMESPACE
Definition: core.h:176
auto streamed(const T &value) -> detail::streamed_view< T >
Definition: ostream.h:143
FMT_MODULE_EXPORT void print(std::ostream &os, format_string< T...> fmt, T &&...args)
Definition: ostream.h:178
FMT_FUNC bool write_console(std::FILE *, string_view)
Definition: format-inl.h:1449
void set_debug_format()=delete
#define FMT_MODULE_EXPORT
Definition: core.h:185
Definition: format.h:1821