rippled
Loading...
Searching...
No Matches
iosformat.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PEERFINDER_IOSFORMAT_H_INCLUDED
21#define RIPPLE_PEERFINDER_IOSFORMAT_H_INCLUDED
22
23#include <ostream>
24#include <sstream>
25#include <string>
26
27namespace beast {
28
29// A collection of handy stream manipulators and
30// functions to produce nice looking log output.
31
33struct leftw
34{
35 explicit leftw(int width_) : width(width_)
36 {
37 }
38 int const width;
39 template <class CharT, class Traits>
42 {
43 ios.setf(std::ios_base::left, std::ios_base::adjustfield);
44 ios.width(p.width);
45 return ios;
46 }
47};
48
50template <class CharT, class Traits, class Allocator>
54 int width = 80,
55 CharT fill = CharT('-'))
56{
57 title.reserve(width);
58 title.push_back(CharT(' '));
59 title.resize(width, fill);
60 return title;
61}
62
64struct divider
65{
66 using CharT = char;
67 explicit divider(int width_ = 80, CharT fill_ = CharT('-'))
68 : width(width_), fill(fill_)
69 {
70 }
71 int const width;
72 CharT const fill;
73 template <class CharT, class Traits>
76 {
77 os << std::basic_string<CharT, Traits>(d.width, d.fill);
78 return os;
79 }
80};
81
83struct fpad
84{
85 explicit fpad(int width_, int pad_ = 0, char fill_ = ' ')
86 : width(width_ + pad_), fill(fill_)
87 {
88 }
89 int const width;
90 char const fill;
91 template <class CharT, class Traits>
94 {
95 os << std::basic_string<CharT, Traits>(f.width, f.fill);
96 return os;
97 }
98};
99
100//------------------------------------------------------------------------------
101
102namespace detail {
103
104template <typename T>
106to_string(T const& t)
107{
109 ss << t;
110 return ss.str();
111}
112
113} // namespace detail
114
117template <
118 class CharT,
119 class Traits = std::char_traits<CharT>,
120 class Allocator = std::allocator<CharT>>
122{
123public:
125 field_t(string_t const& text_, int width_, int pad_, bool right_)
126 : text(text_), width(width_), pad(pad_), right(right_)
127 {
128 }
130 int const width;
131 int const pad;
132 bool const right;
133 template <class CharT2, class Traits2>
138 {
139 std::size_t const length(f.text.length());
140 if (f.right)
141 {
142 if (length < f.width)
143 os << std::basic_string<CharT2, Traits2>(
144 f.width - length, CharT2(' '));
145 os << f.text;
146 }
147 else
148 {
149 os << f.text;
150 if (length < f.width)
151 os << std::basic_string<CharT2, Traits2>(
152 f.width - length, CharT2(' '));
153 }
154 if (f.pad != 0)
155 os << string_t(f.pad, CharT(' '));
156 return os;
157 }
158};
159
160template <class CharT, class Traits, class Allocator>
161field_t<CharT, Traits, Allocator>
164 int width = 8,
165 int pad = 0,
166 bool right = false)
167{
168 return field_t<CharT, Traits, Allocator>(text, width, pad, right);
169}
170
171template <class CharT>
172field_t<CharT>
173field(CharT const* text, int width = 8, int pad = 0, bool right = false)
174{
176 std::
177 basic_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>(
178 text),
179 width,
180 pad,
181 right);
182}
183
184template <typename T>
185field_t<char>
186field(T const& t, int width = 8, int pad = 0, bool right = false)
187{
188 std::string const text(detail::to_string(t));
189 return field(text, width, pad, right);
190}
191
192template <class CharT, class Traits, class Allocator>
193field_t<CharT, Traits, Allocator>
196 int width = 8,
197 int pad = 0)
198{
199 return field_t<CharT, Traits, Allocator>(text, width, pad, true);
200}
201
202template <class CharT>
203field_t<CharT>
204rfield(CharT const* text, int width = 8, int pad = 0)
205{
207 std::
208 basic_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>(
209 text),
210 width,
211 pad,
212 true);
213}
214
215template <typename T>
216field_t<char>
217rfield(T const& t, int width = 8, int pad = 0)
218{
219 std::string const text(detail::to_string(t));
220 return field(text, width, pad, true);
221}
224} // namespace beast
225
226#endif
Justifies a field at the specified width.
Definition: iosformat.h:122
int const width
Definition: iosformat.h:130
field_t(string_t const &text_, int width_, int pad_, bool right_)
Definition: iosformat.h:125
bool const right
Definition: iosformat.h:132
int const pad
Definition: iosformat.h:131
friend std::basic_ostream< CharT2, Traits2 > & operator<<(std::basic_ostream< CharT2, Traits2 > &os, field_t< CharT, Traits, Allocator > const &f)
Definition: iosformat.h:135
std::basic_string< CharT, Traits, Allocator > string_t
Definition: iosformat.h:124
string_t const text
Definition: iosformat.h:129
std::string to_string(T const &t)
Definition: iosformat.h:106
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:162
field_t< CharT, Traits, Allocator > rfield(std::basic_string< CharT, Traits, Allocator > const &text, int width=8, int pad=0)
Definition: iosformat.h:194
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:52
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:65
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, divider const &d)
Definition: iosformat.h:75
CharT const fill
Definition: iosformat.h:72
int const width
Definition: iosformat.h:71
divider(int width_=80, CharT fill_=CharT('-'))
Definition: iosformat.h:67
Creates a padded field with an optiona fill character.
Definition: iosformat.h:84
fpad(int width_, int pad_=0, char fill_=' ')
Definition: iosformat.h:85
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, fpad const &f)
Definition: iosformat.h:93
int const width
Definition: iosformat.h:89
char const fill
Definition: iosformat.h:90
Left justifies a field at the specified width.
Definition: iosformat.h:34
leftw(int width_)
Definition: iosformat.h:35
int const width
Definition: iosformat.h:38
friend std::basic_ios< CharT, Traits > & operator<<(std::basic_ios< CharT, Traits > &ios, leftw const &p)
Definition: iosformat.h:41
T width(T... args)