rippled
Loading...
Searching...
No Matches
iosformat.h
1#ifndef XRPL_PEERFINDER_IOSFORMAT_H_INCLUDED
2#define XRPL_PEERFINDER_IOSFORMAT_H_INCLUDED
3
4#include <ostream>
5#include <sstream>
6#include <string>
7
8namespace beast {
9
10// A collection of handy stream manipulators and
11// functions to produce nice looking log output.
12
14struct leftw
15{
16 explicit leftw(int width_) : width(width_)
17 {
18 }
19 int const width;
20 template <class CharT, class Traits>
23 {
24 ios.setf(std::ios_base::left, std::ios_base::adjustfield);
25 ios.width(p.width);
26 return ios;
27 }
28};
29
31template <class CharT, class Traits, class Allocator>
35 int width = 80,
36 CharT fill = CharT('-'))
37{
38 title.reserve(width);
39 title.push_back(CharT(' '));
40 title.resize(width, fill);
41 return title;
42}
43
45struct divider
46{
47 using CharT = char;
48 explicit divider(int width_ = 80, CharT fill_ = CharT('-'))
49 : width(width_), fill(fill_)
50 {
51 }
52 int const width;
53 CharT const fill;
54 template <class CharT, class Traits>
57 {
58 os << std::basic_string<CharT, Traits>(d.width, d.fill);
59 return os;
60 }
61};
62
64struct fpad
65{
66 explicit fpad(int width_, int pad_ = 0, char fill_ = ' ')
67 : width(width_ + pad_), fill(fill_)
68 {
69 }
70 int const width;
71 char const fill;
72 template <class CharT, class Traits>
75 {
76 os << std::basic_string<CharT, Traits>(f.width, f.fill);
77 return os;
78 }
79};
80
81//------------------------------------------------------------------------------
82
83namespace detail {
84
85template <typename T>
87to_string(T const& t)
88{
90 ss << t;
91 return ss.str();
92}
93
94} // namespace detail
95
98template <
99 class CharT,
100 class Traits = std::char_traits<CharT>,
101 class Allocator = std::allocator<CharT>>
103{
104public:
106 field_t(string_t const& text_, int width_, int pad_, bool right_)
107 : text(text_), width(width_), pad(pad_), right(right_)
108 {
109 }
111 int const width;
112 int const pad;
113 bool const right;
114 template <class CharT2, class Traits2>
119 {
120 std::size_t const length(f.text.length());
121 if (f.right)
122 {
123 if (length < f.width)
124 os << std::basic_string<CharT2, Traits2>(
125 f.width - length, CharT2(' '));
126 os << f.text;
127 }
128 else
129 {
130 os << f.text;
131 if (length < f.width)
132 os << std::basic_string<CharT2, Traits2>(
133 f.width - length, CharT2(' '));
134 }
135 if (f.pad != 0)
136 os << string_t(f.pad, CharT(' '));
137 return os;
138 }
139};
140
141template <class CharT, class Traits, class Allocator>
142field_t<CharT, Traits, Allocator>
145 int width = 8,
146 int pad = 0,
147 bool right = false)
148{
149 return field_t<CharT, Traits, Allocator>(text, width, pad, right);
150}
151
152template <class CharT>
153field_t<CharT>
154field(CharT const* text, int width = 8, int pad = 0, bool right = false)
155{
157 std::
159 text),
160 width,
161 pad,
162 right);
163}
164
165template <typename T>
166field_t<char>
167field(T const& t, int width = 8, int pad = 0, bool right = false)
168{
169 std::string const text(detail::to_string(t));
170 return field(text, width, pad, right);
171}
172
173template <class CharT, class Traits, class Allocator>
174field_t<CharT, Traits, Allocator>
177 int width = 8,
178 int pad = 0)
179{
180 return field_t<CharT, Traits, Allocator>(text, width, pad, true);
181}
182
183template <class CharT>
184field_t<CharT>
185rfield(CharT const* text, int width = 8, int pad = 0)
186{
188 std::
190 text),
191 width,
192 pad,
193 true);
194}
195
196template <typename T>
197field_t<char>
198rfield(T const& t, int width = 8, int pad = 0)
199{
200 std::string const text(detail::to_string(t));
201 return field(text, width, pad, true);
202}
205} // namespace beast
206
207#endif
Justifies a field at the specified width.
Definition iosformat.h:103
int const width
Definition iosformat.h:111
field_t(string_t const &text_, int width_, int pad_, bool right_)
Definition iosformat.h:106
bool const right
Definition iosformat.h:113
int const pad
Definition iosformat.h:112
friend std::basic_ostream< CharT2, Traits2 > & operator<<(std::basic_ostream< CharT2, Traits2 > &os, field_t< CharT, Traits, Allocator > const &f)
Definition iosformat.h:116
std::basic_string< CharT, Traits, Allocator > string_t
Definition iosformat.h:105
string_t const text
Definition iosformat.h:110
T is_same_v
std::string to_string(T const &t)
Definition iosformat.h:87
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:143
field_t< CharT, Traits, Allocator > rfield(std::basic_string< CharT, Traits, Allocator > const &text, int width=8, int pad=0)
Definition iosformat.h:175
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:33
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:46
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, divider const &d)
Definition iosformat.h:56
CharT const fill
Definition iosformat.h:53
int const width
Definition iosformat.h:52
divider(int width_=80, CharT fill_=CharT('-'))
Definition iosformat.h:48
Creates a padded field with an optiona fill character.
Definition iosformat.h:65
fpad(int width_, int pad_=0, char fill_=' ')
Definition iosformat.h:66
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, fpad const &f)
Definition iosformat.h:74
int const width
Definition iosformat.h:70
char const fill
Definition iosformat.h:71
Left justifies a field at the specified width.
Definition iosformat.h:15
leftw(int width_)
Definition iosformat.h:16
int const width
Definition iosformat.h:19
friend std::basic_ios< CharT, Traits > & operator<<(std::basic_ios< CharT, Traits > &ios, leftw const &p)
Definition iosformat.h:22
T width(T... args)