rippled
Loading...
Searching...
No Matches
iosformat.h
1#pragma once
2
3#include <ostream>
4#include <sstream>
5#include <string>
6
7namespace beast {
8
9// A collection of handy stream manipulators and
10// functions to produce nice looking log output.
11
13struct leftw
14{
15 explicit leftw(int width_) : width(width_)
16 {
17 }
18 int const width;
19 template <class CharT, class Traits>
22 {
23 ios.setf(std::ios_base::left, std::ios_base::adjustfield);
24 ios.width(p.width);
25 return ios;
26 }
27};
28
30template <class CharT, class Traits, class Allocator>
32heading(std::basic_string<CharT, Traits, Allocator> title, int width = 80, CharT fill = CharT('-'))
33{
34 title.reserve(width);
35 title.push_back(CharT(' '));
36 title.resize(width, fill);
37 return title;
38}
39
41struct divider
42{
43 using CharT = char;
44 explicit divider(int width_ = 80, CharT fill_ = CharT('-')) : width(width_), fill(fill_)
45 {
46 }
47 int const width;
48 CharT const fill;
49 template <class CharT, class Traits>
52 {
53 os << std::basic_string<CharT, Traits>(d.width, d.fill);
54 return os;
55 }
56};
57
59struct fpad
60{
61 explicit fpad(int width_, int pad_ = 0, char fill_ = ' ') : width(width_ + pad_), fill(fill_)
62 {
63 }
64 int const width;
65 char const fill;
66 template <class CharT, class Traits>
69 {
70 os << std::basic_string<CharT, Traits>(f.width, f.fill);
71 return os;
72 }
73};
74
75//------------------------------------------------------------------------------
76
77namespace detail {
78
79template <typename T>
81to_string(T const& t)
82{
84 ss << t;
85 return ss.str();
86}
87
88} // namespace detail
89
92template <
93 class CharT,
94 class Traits = std::char_traits<CharT>,
95 class Allocator = std::allocator<CharT>>
97{
98public:
100 field_t(string_t const& text_, int width_, int pad_, bool right_)
101 : text(text_), width(width_), pad(pad_), right(right_)
102 {
103 }
105 int const width;
106 int const pad;
107 bool const right;
108 template <class CharT2, class Traits2>
111 {
112 std::size_t const length(f.text.length());
113 if (f.right)
114 {
115 if (length < f.width)
116 os << std::basic_string<CharT2, Traits2>(f.width - length, CharT2(' '));
117 os << f.text;
118 }
119 else
120 {
121 os << f.text;
122 if (length < f.width)
123 os << std::basic_string<CharT2, Traits2>(f.width - length, CharT2(' '));
124 }
125 if (f.pad != 0)
126 os << string_t(f.pad, CharT(' '));
127 return os;
128 }
129};
130
131template <class CharT, class Traits, class Allocator>
132field_t<CharT, Traits, Allocator>
135 int width = 8,
136 int pad = 0,
137 bool right = false)
138{
139 return field_t<CharT, Traits, Allocator>(text, width, pad, right);
140}
141
142template <class CharT>
143field_t<CharT>
144field(CharT const* text, int width = 8, int pad = 0, bool right = false)
145{
148 width,
149 pad,
150 right);
151}
152
153template <typename T>
154field_t<char>
155field(T const& t, int width = 8, int pad = 0, bool right = false)
156{
157 std::string const text(detail::to_string(t));
158 return field(text, width, pad, right);
159}
160
161template <class CharT, class Traits, class Allocator>
162field_t<CharT, Traits, Allocator>
163rField(std::basic_string<CharT, Traits, Allocator> const& text, int width = 8, int pad = 0)
164{
165 return field_t<CharT, Traits, Allocator>(text, width, pad, true);
166}
167
168template <class CharT>
169field_t<CharT>
170rField(CharT const* text, int width = 8, int pad = 0)
171{
174 width,
175 pad,
176 true);
177}
178
179template <typename T>
180field_t<char>
181rField(T const& t, int width = 8, int pad = 0)
182{
183 std::string const text(detail::to_string(t));
184 return field(text, width, pad, true);
185}
188} // namespace beast
Justifies a field at the specified width.
Definition iosformat.h:97
int const width
Definition iosformat.h:105
field_t(string_t const &text_, int width_, int pad_, bool right_)
Definition iosformat.h:100
bool const right
Definition iosformat.h:107
int const pad
Definition iosformat.h:106
friend std::basic_ostream< CharT2, Traits2 > & operator<<(std::basic_ostream< CharT2, Traits2 > &os, field_t< CharT, Traits, Allocator > const &f)
Definition iosformat.h:110
std::basic_string< CharT, Traits, Allocator > string_t
Definition iosformat.h:99
string_t const text
Definition iosformat.h:104
std::string to_string(T const &t)
Definition iosformat.h:81
field_t< CharT, Traits, Allocator > rField(std::basic_string< CharT, Traits, Allocator > const &text, int width=8, int pad=0)
Definition iosformat.h:163
field_t< CharT, Traits, Allocator > field(std::basic_string< CharT, Traits, Allocator > const &text, int width=8, int pad=0, bool right=false)
Definition iosformat.h:133
std::basic_string< CharT, Traits, Allocator > heading(std::basic_string< CharT, Traits, Allocator > title, int width=80, CharT fill=CharT('-'))
Produce a section heading and fill the rest of the line with dashes.
Definition iosformat.h:32
T push_back(T... args)
T reserve(T... args)
T resize(T... args)
T setf(T... args)
T length(T... args)
T str(T... args)
Produce a dashed line separator, with a specified or default size.
Definition iosformat.h:42
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, divider const &d)
Definition iosformat.h:51
CharT const fill
Definition iosformat.h:48
int const width
Definition iosformat.h:47
divider(int width_=80, CharT fill_=CharT('-'))
Definition iosformat.h:44
Creates a padded field with an optional fill character.
Definition iosformat.h:60
fpad(int width_, int pad_=0, char fill_=' ')
Definition iosformat.h:61
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, fpad const &f)
Definition iosformat.h:68
int const width
Definition iosformat.h:64
char const fill
Definition iosformat.h:65
Left justifies a field at the specified width.
Definition iosformat.h:14
leftw(int width_)
Definition iosformat.h:15
int const width
Definition iosformat.h:18
friend std::basic_ios< CharT, Traits > & operator<<(std::basic_ios< CharT, Traits > &ios, leftw const &p)
Definition iosformat.h:21
T width(T... args)