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 <ostream>
12 
13 #include "format.h"
14 
16 
17 template <typename Char> class basic_printf_parse_context;
18 template <typename OutputIt, typename Char> class basic_printf_context;
19 
20 namespace detail {
21 
22 template <class Char> class formatbuf : public std::basic_streambuf<Char> {
23  private:
24  using int_type = typename std::basic_streambuf<Char>::int_type;
25  using traits_type = typename std::basic_streambuf<Char>::traits_type;
26 
27  buffer<Char>& buffer_;
28 
29  public:
30  formatbuf(buffer<Char>& buf) : buffer_(buf) {}
31 
32  protected:
33  // The put-area is actually always empty. This makes the implementation
34  // simpler and has the advantage that the streambuf and the buffer are always
35  // in sync and sputc never writes into uninitialized memory. The obvious
36  // disadvantage is that each call to sputc always results in a (virtual) call
37  // to overflow. There is no disadvantage here for sputn since this always
38  // results in a call to xsputn.
39 
40  int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
41  if (!traits_type::eq_int_type(ch, traits_type::eof()))
42  buffer_.push_back(static_cast<Char>(ch));
43  return ch;
44  }
45 
46  std::streamsize xsputn(const Char* s, std::streamsize count) FMT_OVERRIDE {
47  buffer_.append(s, s + count);
48  return count;
49  }
50 };
51 
52 struct converter {
54 };
55 
56 template <typename Char> struct test_stream : std::basic_ostream<Char> {
57  private:
58  void_t<> operator<<(converter);
59 };
60 
61 // Hide insertion operators for built-in types.
62 template <typename Char, typename Traits>
63 void_t<> operator<<(std::basic_ostream<Char, Traits>&, Char);
64 template <typename Char, typename Traits>
65 void_t<> operator<<(std::basic_ostream<Char, Traits>&, char);
66 template <typename Traits>
67 void_t<> operator<<(std::basic_ostream<char, Traits>&, char);
68 template <typename Traits>
69 void_t<> operator<<(std::basic_ostream<char, Traits>&, signed char);
70 template <typename Traits>
71 void_t<> operator<<(std::basic_ostream<char, Traits>&, unsigned char);
72 
73 // Checks if T has a user-defined operator<< (e.g. not a member of
74 // std::ostream).
75 template <typename T, typename Char> class is_streamable {
76  private:
77  template <typename U>
79  << std::declval<U>()),
80  void_t<>>::value>
81  test(int);
82 
83  template <typename> static std::false_type test(...);
84 
85  using result = decltype(test<T>(0));
86 
87  public:
88  static const bool value = result::value;
89 };
90 
91 // Write the content of buf to os.
92 template <typename Char>
93 void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
94  const Char* buf_data = buf.data();
95  using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
96  unsigned_streamsize size = buf.size();
97  unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
98  do {
99  unsigned_streamsize n = size <= max_size ? size : max_size;
100  os.write(buf_data, static_cast<std::streamsize>(n));
101  buf_data += n;
102  size -= n;
103  } while (size != 0);
104 }
105 
106 template <typename Char, typename T>
108  locale_ref loc = locale_ref()) {
109  formatbuf<Char> format_buf(buf);
110  std::basic_ostream<Char> output(&format_buf);
111 #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
112  if (loc) output.imbue(loc.get<std::locale>());
113 #endif
114  output << value;
115  output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
116  buf.try_resize(buf.size());
117 }
118 
119 // Formats an object of type T that has an overloaded ostream operator<<.
120 template <typename T, typename Char>
121 struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
122  : private formatter<basic_string_view<Char>, Char> {
124  -> decltype(ctx.begin()) {
125  return formatter<basic_string_view<Char>, Char>::parse(ctx);
126  }
127  template <typename ParseCtx,
128  FMT_ENABLE_IF(std::is_same<
129  ParseCtx, basic_printf_parse_context<Char>>::value)>
130  auto parse(ParseCtx& ctx) -> decltype(ctx.begin()) {
131  return ctx.begin();
132  }
133 
134  template <typename OutputIt>
135  auto format(const T& value, basic_format_context<OutputIt, Char>& ctx)
136  -> OutputIt {
138  format_value(buffer, value, ctx.locale());
139  basic_string_view<Char> str(buffer.data(), buffer.size());
140  return formatter<basic_string_view<Char>, Char>::format(str, ctx);
141  }
142  template <typename OutputIt>
143  auto format(const T& value, basic_printf_context<OutputIt, Char>& ctx)
144  -> OutputIt {
146  format_value(buffer, value, ctx.locale());
147  return std::copy(buffer.begin(), buffer.end(), ctx.out());
148  }
149 };
150 } // namespace detail
151 
152 template <typename Char>
153 void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
156  detail::vformat_to(buffer, format_str, args);
157  detail::write_buffer(os, buffer);
158 }
159 
160 /**
161  \rst
162  Prints formatted data to the stream *os*.
163 
164  **Example**::
165 
166  fmt::print(cerr, "Don't {}!", "panic");
167  \endrst
168  */
169 template <typename S, typename... Args,
171 void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) {
172  vprint(os, to_string_view(format_str),
173  fmt::make_args_checked<Args...>(format_str, args...));
174 }
176 
177 #endif // FMT_OSTREAM_H_
type
Definition: core.h:977
#define FMT_ENABLE_IF(...)
Definition: core.h:277
FMT_CONSTEXPR auto parse(basic_format_parse_context< Char > &ctx) -> decltype(ctx.begin())
Definition: ostream.h:123
void try_resize(size_t count)
Definition: core.h:722
basic_string_view< Char > to_string_view(const Char *s)
Definition: core.h:468
size_t size() const FMT_NOEXCEPT
Definition: core.h:706
T * end() FMT_NOEXCEPT
Definition: core.h:700
GLuint buffer
Definition: glcorearb.h:659
#define FMT_END_NAMESPACE
Definition: core.h:198
void print(std::basic_ostream< Char > &os, const S &format_str, Args &&...args)
Definition: ostream.h:171
GLsizeiptr size
Definition: glcorearb.h:663
formatbuf(buffer< Char > &buf)
Definition: ostream.h:30
FMT_CONSTEXPR std::make_unsigned< Int >::type to_unsigned(Int value)
Definition: core.h:325
T * begin() FMT_NOEXCEPT
Definition: core.h:699
constexpr size_t count()
Definition: core.h:968
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)
Definition: ostream.h:93
void push_back(const T &value)
Definition: core.h:735
void vformat_to(buffer< Char > &buf, basic_string_view< Char > format_str, basic_format_args< FMT_BUFFER_CONTEXT(type_identity_t< Char >)> args, detail::locale_ref loc={})
typename detail::void_t_impl< Ts...>::type void_t
Definition: core.h:1364
GLint GLsizei count
Definition: glcorearb.h:404
GLdouble n
Definition: glcorearb.h:2007
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
void format_value(buffer< Char > &buf, const T &value, locale_ref loc=locale_ref())
Definition: ostream.h:107
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE
Definition: ostream.h:46
void vprint(std::basic_ostream< Char > &os, basic_string_view< Char > format_str, basic_format_args< buffer_context< type_identity_t< Char >>> args)
Definition: ostream.h:153
int_type overflow(int_type ch=traits_type::eof()) FMT_OVERRIDE
Definition: ostream.h:40
#define FMT_CONSTEXPR
Definition: core.h:98
void append(const U *begin, const U *end)
Definition: format.h:600
typename type_identity< T >::type type_identity_t
Definition: core.h:270
OIIO_API bool copy(string_view from, string_view to, std::string &err)
GLsizei const GLfloat * value
Definition: glcorearb.h:823
auto format(const T &value, basic_format_context< OutputIt, Char > &ctx) -> OutputIt
Definition: ostream.h:135
**If you just want to fire and args
Definition: thread.h:615
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:259
#define FMT_BEGIN_NAMESPACE
Definition: core.h:203
auto format(const T &value, basic_printf_context< OutputIt, Char > &ctx) -> OutputIt
Definition: ostream.h:143
#define FMT_OVERRIDE
Definition: core.h:107
GLdouble s
Definition: glew.h:1395
std::integral_constant< bool, B > bool_constant
Definition: core.h:262
PUGIXML_CHAR char_t
Definition: pugixml.hpp:125
T * data() FMT_NOEXCEPT
Definition: core.h:712