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 <class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
94{
95public:
97 field_t(string_t const& text_, int width_, int pad_, bool right_)
98 : text(text_), width(width_), pad(pad_), right(right_)
99 {
100 }
102 int const width;
103 int const pad;
104 bool const right;
105 template <class CharT2, class Traits2>
108 {
109 std::size_t const length(f.text.length());
110 if (f.right)
111 {
112 if (length < f.width)
113 os << std::basic_string<CharT2, Traits2>(f.width - length, CharT2(' '));
114 os << f.text;
115 }
116 else
117 {
118 os << f.text;
119 if (length < f.width)
120 os << std::basic_string<CharT2, Traits2>(f.width - length, CharT2(' '));
121 }
122 if (f.pad != 0)
123 os << string_t(f.pad, CharT(' '));
124 return os;
125 }
126};
127
128template <class CharT, class Traits, class Allocator>
129field_t<CharT, Traits, Allocator>
130field(std::basic_string<CharT, Traits, Allocator> const& text, int width = 8, int pad = 0, bool right = false)
131{
132 return field_t<CharT, Traits, Allocator>(text, width, pad, right);
133}
134
135template <class CharT>
136field_t<CharT>
137field(CharT const* text, int width = 8, int pad = 0, bool right = false)
138{
141}
142
143template <typename T>
144field_t<char>
145field(T const& t, int width = 8, int pad = 0, bool right = false)
146{
147 std::string const text(detail::to_string(t));
148 return field(text, width, pad, right);
149}
150
151template <class CharT, class Traits, class Allocator>
152field_t<CharT, Traits, Allocator>
153rField(std::basic_string<CharT, Traits, Allocator> const& text, int width = 8, int pad = 0)
154{
155 return field_t<CharT, Traits, Allocator>(text, width, pad, true);
156}
157
158template <class CharT>
159field_t<CharT>
160rField(CharT const* text, int width = 8, int pad = 0)
161{
164}
165
166template <typename T>
167field_t<char>
168rField(T const& t, int width = 8, int pad = 0)
169{
170 std::string const text(detail::to_string(t));
171 return field(text, width, pad, true);
172}
175} // namespace beast
Justifies a field at the specified width.
Definition iosformat.h:94
int const width
Definition iosformat.h:102
field_t(string_t const &text_, int width_, int pad_, bool right_)
Definition iosformat.h:97
bool const right
Definition iosformat.h:104
int const pad
Definition iosformat.h:103
friend std::basic_ostream< CharT2, Traits2 > & operator<<(std::basic_ostream< CharT2, Traits2 > &os, field_t< CharT, Traits, Allocator > const &f)
Definition iosformat.h:107
std::basic_string< CharT, Traits, Allocator > string_t
Definition iosformat.h:96
string_t const text
Definition iosformat.h:101
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:153
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:130
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)