DIY 3.0 data-parallel out-of-core C++ library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

All Classes Namespaces Functions Typedefs Groups Pages
ostream.h
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 "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:
41 typedef typename std::basic_streambuf<Char>::int_type int_type;
42 typedef typename std::basic_streambuf<Char>::traits_type traits_type;
43
44 Buffer<Char> &buffer_;
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>
91 void format(BasicFormatter<Char, ArgFormatter> &f,
92 const Char *&format_str, const T &value) {
93 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
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
113 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
114 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
115
116
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::internal::ConvertToIntImpl
Definition: format.h:1059
fmt::internal::MakeArg
Definition: format.h:1268
fmt::BasicFormatter::format
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3733
fmt::internal::FormatBuf
Definition: ostream.h:39
fmt::Buffer< Char >
fmt::BasicFormatter
Definition: format.h:382
fmt::Buffer::reserve
void reserve(std::size_t capacity)
Definition: format.h:628
fmt::internal::DummyStream
Definition: ostream.h:72
fmt::internal::MemoryBuffer
Definition: format.h:665
fmt::Buffer::capacity
std::size_t capacity() const
Definition: format.h:612
fmt::Buffer::resize
void resize(std::size_t new_size)
Definition: format.h:617
fmt::BasicStringRef
Definition: format.h:409
fmt::Buffer::size
std::size_t size() const
Definition: format.h:609