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_)
36  : width (width_)
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>
53  int width = 80, CharT fill = CharT ('-'))
54 {
55  title.reserve (width);
56  title.push_back (CharT (' '));
57  title.resize (width, fill);
58  return title;
59 }
60 
62 struct divider
63 {
64  using CharT = char;
65  explicit divider (int width_ = 80, CharT fill_ = CharT ('-'))
66  : width (width_)
67  , fill (fill_)
68  { }
69  int const width;
70  CharT const fill;
71  template <class CharT, class Traits>
74  {
75  os << std::basic_string <CharT, Traits> (d.width, d.fill);
76  return os;
77  }
78 };
79 
81 struct fpad
82 {
83  explicit fpad (int width_, int pad_ = 0, char fill_ = ' ')
84  : width (width_ + pad_)
85  , fill (fill_)
86  { }
87  int const width;
88  char const fill;
89  template <class CharT, class Traits>
92  {
93  os << std::basic_string <CharT, Traits> (f.width, f.fill);
94  return os;
95  }
96 };
97 
98 //------------------------------------------------------------------------------
99 
100 namespace detail {
101 
102 template <typename T>
103 std::string to_string (T const& t)
104 {
106  ss << t;
107  return ss.str();
108 }
109 
110 }
111 
114 template <class CharT,
115  class Traits = std::char_traits <CharT>,
116  class Allocator = std::allocator <CharT>>
117 class field_t
118 {
119 public:
121  field_t (string_t const& text_, int width_, int pad_, bool right_)
122  : text (text_)
123  , width (width_)
124  , pad (pad_)
125  , right (right_)
126  { }
127  string_t const text;
128  int const width;
129  int const pad;
130  bool const right;
131  template <class CharT2, class Traits2>
135  {
136  std::size_t const length (f.text.length());
137  if (f.right)
138  {
139  if (length < f.width)
140  os << std::basic_string <CharT2, Traits2> (
141  f.width - length, CharT2 (' '));
142  os << f.text;
143  }
144  else
145  {
146  os << f.text;
147  if (length < f.width)
148  os << std::basic_string <CharT2, Traits2> (
149  f.width - length, CharT2 (' '));
150  }
151  if (f.pad != 0)
152  os << string_t (f.pad, CharT (' '));
153  return os;
154  }
155 };
156 
157 template <class CharT, class Traits, class Allocator>
160  int width = 8, int pad = 0, bool right = false)
161 {
163  text, width, pad, right);
164 }
165 
166 template <class CharT>
168  CharT const* text, int width = 8, int pad = 0, bool right = false)
169 {
173  width, pad, right);
174 }
175 
176 template <typename T>
178  T const& t, int width = 8, int pad = 0, bool right = false)
179 {
180  std::string const text (detail::to_string (t));
181  return field (text, width, pad, right);
182 }
183 
184 template <class CharT, class Traits, class Allocator>
187  int width = 8, int pad = 0)
188 {
190  text, width, pad, true);
191 }
192 
193 template <class CharT>
195  CharT const* text, int width = 8, int pad = 0)
196 {
200  width, pad, true);
201 }
202 
203 template <typename T>
205  T const& t, int width = 8, int pad = 0)
206 {
207  std::string const text (detail::to_string (t));
208  return field (text, width, pad, true);
209 }
212 }
213 
214 #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:83
beast::fpad::operator<<
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, fpad const &f)
Definition: iosformat.h:90
beast::fpad::fill
const char fill
Definition: iosformat.h:88
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:127
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:185
std::stringstream
STL class.
beast::divider::CharT
char CharT
Definition: iosformat.h:64
beast::field_t::right
const bool right
Definition: iosformat.h:130
beast::divider::width
const int width
Definition: iosformat.h:69
beast::leftw::operator<<
friend std::basic_ios< CharT, Traits > & operator<<(std::basic_ios< CharT, Traits > &ios, leftw const &p)
Definition: iosformat.h:40
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:132
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:129
std::char_traits
beast::field_t::field_t
field_t(string_t const &text_, int width_, int pad_, bool right_)
Definition: iosformat.h:121
std::basic_ostream
STL class.
beast::divider::divider
divider(int width_=80, CharT fill_=CharT('-'))
Definition: iosformat.h:65
std::basic_ios::setf
T setf(T... args)
beast::fpad::width
const int width
Definition: iosformat.h:87
beast::field_t::string_t
std::basic_string< CharT, Traits, Allocator > string_t
Definition: iosformat.h:120
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:158
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:51
beast::fpad
Creates a padded field with an optiona fill character.
Definition: iosformat.h:81
beast::field_t::width
const int width
Definition: iosformat.h:128
beast::field_t
Justifies a field at the specified width.
Definition: iosformat.h:117
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:62
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:72
beast::detail::to_string
std::string to_string(T const &t)
Definition: iosformat.h:103
ostream
std::basic_ios
STL class.
beast::divider::fill
const CharT fill
Definition: iosformat.h:70
beast
Definition: base_uint.h:582
string