ZeroErr
载入中...
搜索中...
未找到
std_adapter.h
浏览该文件的文档.
1#pragma once
2
3// Explicit opt-in to the standard string/iostream backend. Core headers never
4// auto-enable it based on which standard headers happen to have been included.
5#include <ostream>
6#include <sstream>
7#include <string>
8#include "zeroerr/print.h"
9
10namespace zeroerr {
11inline std::ostream& operator<<(std::ostream& stream, const Printer& printer) {
12 if (printer.use_stringstream) {
13 const auto text = printer.str();
14 stream.write(text.data(), static_cast<std::streamsize>(text.size()));
15 }
16 return stream;
17}
18namespace detail {
19inline void applyFormat(std::ostream& stream, const StringBuffer::Format& f) {
20 stream.precision(f.precision);
21 stream.width(f.width);
22 stream.fill(f.fill);
23 if (f.base == 16)
24 stream << std::hex;
25 else if (f.base == 8)
26 stream << std::oct;
27 else
28 stream << std::dec;
29 if (f.style == 1)
30 stream << std::fixed;
31 else if (f.style == 2)
32 stream << std::scientific;
33}
34inline void saveFormat(StringBuffer& buffer, const std::ostream& stream) {
35 auto& f = buffer.format();
36 auto flags = stream.flags();
37 f.base = (flags & std::ios_base::basefield) == std::ios_base::hex ? 16
38 : (flags & std::ios_base::basefield) == std::ios_base::oct ? 8
39 : 10;
40 f.style = (flags & std::ios_base::floatfield) == std::ios_base::fixed ? 1
41 : (flags & std::ios_base::floatfield) == std::ios_base::scientific ? 2
42 : 0;
43 f.precision = static_cast<int>(stream.precision());
44 f.width = static_cast<int>(stream.width());
45 f.fill = stream.fill();
46}
47template <class T>
48StringBuffer& standardInsert(StringBuffer& buffer, const T& value) {
49 std::ostringstream stream;
50 applyFormat(stream, buffer.format());
51 stream << value;
52 saveFormat(buffer, stream);
53 const auto text = stream.str();
54 return buffer.write(text.data(), text.size());
55}
56} // namespace detail
57inline StringBuffer& operator<<(StringBuffer& buffer, std::ios_base& (*manip)(std::ios_base&)) {
58 return detail::standardInsert(buffer, manip);
59}
60inline StringBuffer& operator<<(StringBuffer& buffer, std::ostream& (*manip)(std::ostream&)) {
61 detail::standardInsert(buffer, manip);
62 buffer.flush();
63 return buffer;
64}
65template <class T>
66typename std::enable_if<std::is_class<T>::value && !detail::is_string<T>::value &&
67 detail::is_streamable<std::ostream, T>::value,
69operator<<(StringBuffer& buffer, const T& value) {
70 return detail::standardInsert(buffer, value);
71}
72
73template <class T>
74typename std::enable_if<std::is_class<typename std::decay<T>::type>::value && !detail::is_string<typename std::decay<T>::type>::value &&
75 !detail::is_container<typename std::decay<T>::type>::value &&
76 !detail::is_pointer_like<typename std::decay<T>::type>::value &&
77 !detail::is_weak_pointer_like<typename std::decay<T>::type>::value &&
78 !detail::is_tuple_like<typename std::decay<T>::type>::value && !detail::is_complex<typename std::decay<T>::type>::value &&
79 detail::is_streamable<std::ostream, T>::value,
80 void>::type
81PrinterExt(Printer& printer, T&& value, unsigned level, const char* lb, rank<1>) {
82 printer.os << printer.tab(level);
83 detail::standardInsert(printer.os, value);
84 printer.os << lb;
85}
86} // namespace zeroerr
Definition buffer.h:7
Format & format()
Definition buffer.h:38
StringBuffer & write(const char *data, std::size_t size)
Definition buffer.cpp:5
void flush()
Definition buffer.cpp:12
void saveFormat(StringBuffer &buffer, const std::ostream &stream)
Definition std_adapter.h:34
StringBuffer & standardInsert(StringBuffer &buffer, const T &value)
Definition std_adapter.h:48
void applyFormat(std::ostream &stream, const StringBuffer::Format &f)
Definition std_adapter.h:19
Definition assert_text.cpp:5
StringBuffer & operator<<(StringBuffer &buffer, const Printer &printer)
Definition print.h:331
void PrinterExt(Printer &, const T &, unsigned, const char *, rank< 0 >)
PrinterExt is an extension of Printer that allows user to write custom rules for printing.
Definition print.h:370
A functor class Printer for printing a value of any type.
Definition print.h:74
StringBuffer & os
Definition print.h:121
bool use_stringstream
Definition print.h:122
Definition buffer.h:9
int base
Definition buffer.h:10
char fill
Definition buffer.h:13
int precision
Definition buffer.h:11
int style
Definition buffer.h:14
int width
Definition buffer.h:12
rank is a helper class for Printer to define the priority of overloaded functions.
Definition typetraits.h:43