Flow123d  JS_before_hm-1804-gf2ad740aa
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  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12  2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FMT_OSTREAM_H_
29 #define FMT_OSTREAM_H_
30 
31 #include "system/fmt/format.h"
32 #include <ostream>
33 
34 namespace fmt {
35 
36 namespace internal {
37 
38 template <class Char>
39 class FormatBuf : public std::basic_streambuf<Char> {
40  private:
42  typedef typename std::basic_streambuf<Char>::traits_type traits_type;
43 
45  Char *start_;
46 
47  public:
48  FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0]) {
49  this->setp(start_, start_ + buffer_.capacity());
50  }
51 
52  int_type overflow(int_type ch = traits_type::eof()) {
53  if (!traits_type::eq_int_type(ch, traits_type::eof())) {
54  size_t buf_size = size();
55  buffer_.resize(buf_size);
56  buffer_.reserve(buf_size * 2);
57 
58  start_ = &buffer_[0];
59  start_[buf_size] = traits_type::to_char_type(ch);
60  this->setp(start_+ buf_size + 1, start_ + buf_size * 2);
61  }
62  return ch;
63  }
64 
65  size_t size() const {
66  return to_unsigned(this->pptr() - start_);
67  }
68 };
69 
70 Yes &convert(std::ostream &);
71 
72 struct DummyStream : std::ostream {
73  DummyStream(); // Suppress a bogus warning in MSVC.
74  // Hide all operator<< overloads from std::ostream.
75  void operator<<(Null<>);
76 };
77 
78 No &operator<<(std::ostream &, int);
79 
80 template<typename T>
81 struct ConvertToIntImpl<T, true> {
82  // Convert to int only if T doesn't have an overloaded operator<<.
83  enum {
84  value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
85  };
86 };
87 } // namespace internal
88 
89 // Formats a value.
90 template <typename Char, typename ArgFormatter, typename T>
92  const Char *&format_str, const T &value) {
94 
95  internal::FormatBuf<Char> format_buf(buffer);
96  std::basic_ostream<Char> output(&format_buf);
97  output << value;
98 
99  BasicStringRef<Char> str(&buffer[0], format_buf.size());
100  typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
101  format_str = f.format(format_str, MakeArg(str));
102 }
103 
104 /**
105  \rst
106  Prints formatted data to the stream *os*.
107 
108  **Example**::
109 
110  print(cerr, "Don't {}!", "panic");
111  \endrst
112  */
113 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
114 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
115 
116 /**
117  \rst
118  Prints formatted data to the stream *os*.
119 
120  **Example**::
121 
122  fprintf(cerr, "Don't %s!", "panic");
123  \endrst
124  */
125 FMT_API int fprintf(std::ostream &os, CStringRef format_str, ArgList args);
126 FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
127 } // namespace fmt
128 
129 #ifdef FMT_HEADER_ONLY
130 # include "ostream.cc"
131 #endif
132 
133 #endif // FMT_OSTREAM_H_
fmt::Buffer< Char >
fmt::internal::DummyStream::DummyStream
DummyStream()
fmt::Buffer::capacity
std::size_t capacity() const
Definition: format.h:615
fmt::internal::MemoryBuffer
Definition: format.h:668
fmt::internal::FormatBuf::buffer_
Buffer< Char > & buffer_
Definition: ostream.h:44
fmt::internal::FormatBuf
Definition: ostream.h:39
fmt::Buffer::reserve
void reserve(std::size_t capacity)
Definition: format.h:631
format.h
fmt::internal::ConvertToIntImpl::value
@ value
Definition: format.h:1070
fmt::fprintf
FMT_FUNC int fprintf(std::ostream &os, CStringRef format, ArgList args)
Definition: ostream.cc:56
value
static constexpr bool value
Definition: json.hpp:87
fmt::internal::MakeArg
Definition: format.h:1293
fmt::internal::ConvertToIntImpl
Definition: format.h:1069
FMT_API
#define FMT_API
Definition: format.h:75
fmt::internal::FormatBuf::size
size_t size() const
Definition: ostream.h:65
fmt::internal::to_unsigned
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:561
fmt::format
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
fmt::BasicFormatter
Definition: format.h:386
fmt::internal::FormatBuf::overflow
int_type overflow(int_type ch=traits_type::eof())
Definition: ostream.h:52
json_spirit::int_type
@ int_type
Definition: json_spirit_value.h:32
fmt::BasicFormatter::format
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3701
fmt::Buffer::resize
void resize(std::size_t new_size)
Definition: format.h:620
fmt::internal::convert
Yes & convert(fmt::ULongLong)
fmt::internal::FormatBuf::start_
Char * start_
Definition: ostream.h:45
fmt::internal::FormatBuf::FormatBuf
FormatBuf(Buffer< Char > &buffer)
Definition: ostream.h:48
fmt::internal::Null
Definition: format.h:1042
fmt::internal::Yes
char Yes[1]
Definition: format.h:1058
fmt::internal::DummyStream
Definition: ostream.h:72
fmt::print
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:489
fmt::internal::FormatBuf::traits_type
std::basic_streambuf< Char >::traits_type traits_type
Definition: ostream.h:42
fmt::internal::No
char No[2]
Definition: format.h:1059
fmt::BasicStringRef
Definition: format.h:413
fmt
Definition: format.cc:82
fmt::internal::operator<<
No & operator<<(std::ostream &, int)
FMT_VARIADIC
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:3407
internal
Definition: field_values.cc:20
fmt::internal::FormatBuf::int_type
std::basic_streambuf< Char >::int_type int_type
Definition: ostream.h:41
fmt::internal::DummyStream::operator<<
void operator<<(Null<>)
ostream.cc
fmt::CStringRef
BasicCStringRef< char > CStringRef
Definition: format.h:531