Electroneum
ostream.h
Go to the documentation of this file.
1 /*
2  Formatting library for C++ - std::ostream support
3 
4  Copyright (c) 2012 - 2016, Victor Zverovich
5  All rights reserved.
6 
7  For the license information refer to format.h.
8  */
9 
10 #ifndef FMT_OSTREAM_H_
11 #define FMT_OSTREAM_H_
12 
13 #include "format.h"
14 #include <ostream>
15 
16 namespace fmt {
17 
18 namespace internal {
19 
20 template <class Char>
21 class FormatBuf : public std::basic_streambuf<Char> {
22  private:
23  typedef typename std::basic_streambuf<Char>::int_type int_type;
24  typedef typename std::basic_streambuf<Char>::traits_type traits_type;
25 
27  Char *start_;
28 
29  public:
30  FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0]) {
31  this->setp(start_, start_ + buffer_.capacity());
32  }
33 
34  FormatBuf(Buffer<Char> &buffer, Char *start) : buffer_(buffer) , start_(start) {
35  this->setp(start_, start_ + buffer_.capacity());
36  }
37 
38  int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
39  if (!traits_type::eq_int_type(ch, traits_type::eof())) {
40  size_t buf_size = size();
41  buffer_.resize(buf_size);
42  buffer_.reserve(buf_size * 2);
43 
44  start_ = &buffer_[0];
45  start_[buf_size] = traits_type::to_char_type(ch);
46  this->setp(start_+ buf_size + 1, start_ + buf_size * 2);
47  }
48  return ch;
49  }
50 
51  size_t size() const {
52  return to_unsigned(this->pptr() - start_);
53  }
54 };
55 
56 Yes &convert(std::ostream &);
57 
58 struct DummyStream : std::ostream {
59  DummyStream(); // Suppress a bogus warning in MSVC.
60  // Hide all operator<< overloads from std::ostream.
61  void operator<<(Null<>);
62 };
63 
64 No &operator<<(std::ostream &, int);
65 
66 template<typename T>
68  // Convert to int only if T doesn't have an overloaded operator<<.
69  enum {
70  value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
71  };
72 };
73 
74 // Write the content of w to os.
75 void write(std::ostream &os, Writer &w);
76 
77 #if FMT_HAS_DECLTYPE_INCOMPLETE_RETURN_TYPES
78 template<typename T>
79 class is_streamable {
80  template<typename U>
81  static auto test(int) -> decltype(std::declval<std::ostream &>() << std::declval<U>(), std::true_type());
82 
83  template<typename>
84  static auto test(...) -> std::false_type;
85 
86 public:
87  static constexpr bool value = decltype(test<T>(0))::value;
88 };
89 #endif
90 } // namespace internal
91 
92 // Formats a value.
93 template <typename Char, typename ArgFormatter_, typename T>
95  const Char *&format_str, const T &value) {
97 
98  internal::FormatBuf<Char> format_buf(buffer);
99  std::basic_ostream<Char> output(&format_buf);
100  output << value;
101 
102  BasicStringRef<Char> str(&buffer[0], format_buf.size());
103  typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
104  format_str = f.format(format_str, MakeArg(str));
105 }
106 
116 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
117 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
118 
119 #if __cplusplus >= 201103L
120 template<typename T, typename Char>
121 typename std::enable_if<
122  !std::is_same<
124  char *
125  >::value,
126  BasicWriter<Char>&
127 >::type
128 operator<<(BasicWriter<Char> &writer, const T &value) {
129  FMT_STATIC_ASSERT(internal::is_streamable<T>::value, "T must be Streamable");
130 
131  auto &buffer = writer.buffer();
132  Char *start = &buffer[0] + buffer.size();
133 
134  internal::FormatBuf<Char> format_buf(buffer, start);
135  std::basic_ostream<Char> output(&format_buf);
136  output << value;
137 
138  buffer.resize(buffer.size() + format_buf.size());
139  return writer;
140 }
141 #endif
142 } // namespace fmt
143 
144 #ifdef FMT_HEADER_ONLY
145 # include "ostream.cc"
146 #endif
147 
148 #endif // FMT_OSTREAM_H_
FormatBuf(Buffer< Char > &buffer, Char *start)
Definition: ostream.h:34
const uint32_t T[512]
Definition: groestl_tables.h:34
Buffer< Char > & buffer_
Definition: ostream.h:26
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:3565
void resize(std::size_t new_size)
Definition: format.h:700
void format_arg(fmt::BasicFormatter< Char, ArgFormatter > &f, const Char *&format_str, const ArgJoin< Char, It > &e)
Definition: format.h:3917
size_t size() const
Definition: ostream.h:51
char Yes[1]
Definition: format.h:1154
FMT_FUNC void write(std::ostream &os, Writer &w)
Definition: ostream.cc:15
Definition: test.py:1
#define FMT_STATIC_ASSERT(cond, message)
Definition: format.h:1253
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3859
Char * start_
Definition: ostream.h:27
Yes & convert(fmt::ULongLong)
Definition: format.h:459
Definition: ostream.h:58
#define FMT_API
Definition: format.h:78
Definition: ostream.h:21
#define FMT_OVERRIDE
Definition: format.h:212
Definition: format.h:748
BasicCStringRef< char > CStringRef
Definition: format.h:610
FormatBuf(Buffer< Char > &buffer)
Definition: ostream.h:30
Definition: format.h:1165
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:641
std::size_t capacity() const
Definition: format.h:695
type
Definition: json.h:74
int_type overflow(int_type ch=traits_type::eof()) FMT_OVERRIDE
Definition: ostream.h:38
No & operator<<(std::ostream &, int)
char No[2]
Definition: format.h:1155
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:486
Definition: format.h:444
std::basic_streambuf< Char >::int_type int_type
Definition: ostream.h:23
std::basic_streambuf< Char >::traits_type traits_type
Definition: ostream.h:24
Definition: format.h:487
void reserve(std::size_t capacity)
Definition: format.h:711
Definition: format.cc:79
Definition: format.h:1429
#define true
Definition: stdbool.h:37