rippled
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 
27 namespace beast {
28 
29 // A collection of handy stream manipulators and
30 // functions to produce nice looking log output.
31 
33 struct 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 
50 template <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 
64 struct 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 
83 struct 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 
102 namespace detail {
103 
104 template <typename T>
106 to_string(T const& t)
107 {
109  ss << t;
110  return ss.str();
111 }
112 
113 } // namespace detail
114 
117 template <
118  class CharT,
119  class Traits = std::char_traits<CharT>,
120  class Allocator = std::allocator<CharT>>
121 class field_t
122 {
123 public:
125  field_t(string_t const& text_, int width_, int pad_, bool right_)
126  : text(text_), width(width_), pad(pad_), right(right_)
127  {
128  }
129  string_t const text;
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 
160 template <class CharT, class Traits, class Allocator>
161 field_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 
171 template <class CharT>
172 field_t<CharT>
173 field(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 
184 template <typename T>
185 field_t<char>
186 field(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 
192 template <class CharT, class Traits, class Allocator>
193 field_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 
202 template <class CharT>
203 field_t<CharT>
204 rfield(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 
215 template <typename T>
216 field_t<char>
217 rfield(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
std::basic_ios::width
T width(T... args)
sstream
std::basic_string::resize
T resize(T... args)
std::basic_string
STL class.
beast::fpad::fpad
fpad(int width_, int pad_=0, char fill_=' ')
Definition: iosformat.h:85
beast::fpad::operator<<
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, fpad const &f)
Definition: iosformat.h:93
beast::fpad::fill
const char fill
Definition: iosformat.h:90
std::basic_string::reserve
T reserve(T... args)
std::basic_string::length
T length(T... args)
beast::field_t::text
const string_t text
Definition: iosformat.h:129
beast::rfield
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::stringstream
STL class.
beast::divider::CharT
char CharT
Definition: iosformat.h:66
beast::field_t::right
const bool right
Definition: iosformat.h:132
beast::divider::width
const int width
Definition: iosformat.h:71
beast::leftw::operator<<
friend std::basic_ios< CharT, Traits > & operator<<(std::basic_ios< CharT, Traits > &ios, leftw const &p)
Definition: iosformat.h:41
beast::field_t::operator<<
friend std::basic_ostream< CharT2, Traits2 > & operator<<(std::basic_ostream< CharT2, Traits2 > &os, field_t< CharT, Traits, Allocator > const &f)
Definition: iosformat.h:135
beast::field_t::string_t
std::basic_string< CharT, Traits, Allocator > string_t
Definition: iosformat.h:124
beast::leftw::width
const int width
Definition: iosformat.h:38
std::basic_string::push_back
T push_back(T... args)
beast::field_t::pad
const int pad
Definition: iosformat.h:131
std::char_traits
beast::field_t::field_t
field_t(string_t const &text_, int width_, int pad_, bool right_)
Definition: iosformat.h:125
std::basic_ostream
STL class.
beast::divider::divider
divider(int width_=80, CharT fill_=CharT('-'))
Definition: iosformat.h:67
std::basic_ios::setf
T setf(T... args)
beast::fpad::width
const int width
Definition: iosformat.h:89
beast::leftw
Left justifies a field at the specified width.
Definition: iosformat.h:33
beast::field
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
beast::heading
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
beast::fpad
Creates a padded field with an optiona fill character.
Definition: iosformat.h:83
beast::field_t::width
const int width
Definition: iosformat.h:130
beast::field_t
Justifies a field at the specified width.
Definition: iosformat.h:121
beast::leftw::leftw
leftw(int width_)
Definition: iosformat.h:35
beast::divider
Produce a dashed line separator, with a specified or default size.
Definition: iosformat.h:64
std::allocator
STL class.
std::stringstream::str
T str(T... args)
std::size_t
beast::divider::operator<<
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, divider const &d)
Definition: iosformat.h:75
beast::detail::to_string
std::string to_string(T const &t)
Definition: iosformat.h:106
ostream
std::basic_ios
STL class.
beast::divider::fill
const CharT fill
Definition: iosformat.h:72
beast
Definition: base_uint.h:641
string