NGL  6.5
The NCCA Graphics Library
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 "fmt/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  int_type overflow(int_type ch = traits_type::eof()) {
35  if (!traits_type::eq_int_type(ch, traits_type::eof())) {
36  size_t buf_size = size();
37  buffer_.resize(buf_size);
38  buffer_.reserve(buf_size * 2);
39 
40  start_ = &buffer_[0];
41  start_[buf_size] = traits_type::to_char_type(ch);
42  this->setp(start_+ buf_size + 1, start_ + buf_size * 2);
43  }
44  return ch;
45  }
46 
47  size_t size() const {
48  return to_unsigned(this->pptr() - start_);
49  }
50 };
51 
52 Yes &convert(std::ostream &);
53 
54 struct DummyStream : std::ostream {
55  DummyStream(); // Suppress a bogus warning in MSVC.
56  // Hide all operator<< overloads from std::ostream.
57  void operator<<(Null<>);
58 };
59 
60 No &operator<<(std::ostream &, int);
61 
62 template<typename T>
63 struct ConvertToIntImpl<T, true> {
64  // Convert to int only if T doesn't have an overloaded operator<<.
65  enum {
66  value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
67  };
68 };
69 
70 // Write the content of w to os.
71 void write(std::ostream &os, Writer &w);
72 } // namespace internal
73 
74 // Formats a value.
75 template <typename Char, typename ArgFormatter, typename T>
77  const Char *&format_str, const T &value) {
79 
80  internal::FormatBuf<Char> format_buf(buffer);
81  std::basic_ostream<Char> output(&format_buf);
82  output << value;
83 
84  BasicStringRef<Char> str(&buffer[0], format_buf.size());
86  format_str = f.format(format_str, MakeArg(str));
87 }
88 
98 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
99 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
100 } // namespace fmt
101 
102 #ifdef FMT_HEADER_ONLY
103 # include "ostream.cc"
104 #endif
105 
106 #endif // FMT_OSTREAM_H_
void format_arg(Formatter &, const Char *, const T &)
Definition: format.h:1164
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:3440
char Yes[1]
Definition: format.h:1062
std::basic_streambuf< Char >::int_type int_type
Definition: ostream.h:23
FMT_FUNC void write(std::ostream &os, Writer &w)
Definition: ostream.cc:15
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3734
GLsizei const GLfloat * value
Definition: glew.h:1852
size_t size() const
Definition: ostream.h:47
Yes & convert(fmt::ULongLong)
int_type overflow(int_type ch=traits_type::eof())
Definition: ostream.h:34
#define FMT_API
Definition: format.h:75
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1893
void reserve(std::size_t capacity)
Definition: format.h:634
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:564
GLuint buffer
Definition: glew.h:1683
std::basic_streambuf< Char >::traits_type traits_type
Definition: ostream.h:24
No & operator<<(std::ostream &, int)
char No[2]
Definition: format.h:1063
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:486
std::size_t capacity() const
Definition: format.h:618
void resize(std::size_t new_size)
Definition: format.h:623
Buffer< Char > & buffer_
Definition: ostream.h:26
Definition: format.cc:82
FormatBuf(Buffer< Char > &buffer)
Definition: ostream.h:30
GLclampf f
Definition: glew.h:3511