dodo  0.0.1
A C++ library to create containerized Linux services
puts.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file puts.hpp
20  * Defines the dodo::common::Puts class.
21  */
22 
23 #ifndef common_puts_hpp
24 #define common_puts_hpp
25 
26 #include <iomanip>
27 #include <sstream>
28 #include <string>
29 #include <thread>
30 
31 namespace dodo::common {
32 
33  /**
34  * Helper class to write strings in stream format, eg
35  * \code
36  * string s = common::Puts() << "integer: " << 3 << " double: " << common::Puts:setprecision(2) << 3.145;
37  * \endcode
38  * which is very convenient in, for example, throwing exceptions, where the throw_Exception macro already inserts
39  * the "Puts() <<" in the expression, so one can write
40  * \code
41  * throw Exception( "open failed with errorcode: " << errorcode );
42  * \endcode
43  *
44  *
45  */
46  class Puts {
47  public:
48 
49  /**
50  * Mimics std::endl.
51  */
52  struct endl {
53  };
54 
55  /**
56  * Put the stream in floating point fixed-format mode.
57  * @see operator<<( fixed )
58  */
59  struct fixed {
60  };
61 
62  /**
63  * Put the stream in floating point scientific-format mode.
64  * @see operator<<( scientific )
65  */
66  struct scientific {
67  };
68 
69  /**
70  * Put the stream in decimal mode.
71  * @see operator<<( dec )
72  */
73  struct dec {
74  };
75 
76  /**
77  * Put the stream in hexadecimal mode.
78  * @see operator<<( hex )
79  */
80  struct hex {
81  };
82 
83  /**
84  * Put the stream in octal mode.
85  * @see operator<<( oct )
86  */
87  struct oct {
88  };
89 
90 
91  /**
92  * Set the width of things to w characters.
93  * @see operator<<( setw w )
94  */
95  struct setw {
96  /**
97  * Construct with width.
98  * @param w The width.
99  */
100  setw( int w ) : w_(w) {};
101 
102  /** The width. */
103  int w_;
104  };
105 
106  /**
107  * Set the precision for floating point fixed format
108  * @see opeartor<<( setprecision )
109  */
110  struct setprecision {
111  /**
112  * Construct with precision.
113  * @param p The precision.
114  */
115  setprecision( int p ) : p_(p) {};
116  /** The precision. */
117  int p_;
118  };
119 
120  /**
121  * Constructor inits to fixed format for double with precision 3.
122  */
123  Puts() { ss_ << std::setprecision(3) << std::fixed; };
124 
125  /**
126  * Append a STL string.
127  * @param s The string.
128  * @return This Puts.
129  */
130  const Puts& operator<<( const std::string& s ) const {
131  ss_ << s;
132  return *this;
133  }
134 
135  /**
136  * Append a C string.
137  * @param s The const char*
138  * @return This Puts.
139  */
140  const Puts& operator<<( const char* s ) const {
141  ss_ << s;
142  return *this;
143  }
144 
145  /**
146  * Append a char.
147  * @param c The char
148  * @return This Puts.
149  */
150  const Puts& operator<<( char c ) const {
151  ss_ << c;
152  return *this;
153  }
154 
155  /**
156  * Append an integer.
157  * @param i The int
158  * @return This Puts.
159  */
160  const Puts& operator<<( int i ) const {
161  ss_ << i;
162  return *this;
163  }
164 
165  /**
166  * Append a long.
167  * @param l The long
168  * @return This Puts.
169  */
170  const Puts& operator<<( long l ) const {
171  ss_ << l;
172  return *this;
173  }
174 
175  /**
176  * Append a long long.
177  * @param l The long long
178  * @return This Puts.
179  */
180  const Puts& operator<<( long long l ) const {
181  ss_ << l;
182  return *this;
183  }
184 
185  /**
186  * Append an unsigned integer.
187  * @param i The unsigned integer to append.
188  * @return This Puts.
189  */
190  const Puts& operator<<( unsigned int i ) const {
191  ss_ << i;
192  return *this;
193  }
194 
195  /**
196  * Append an unsigned long.
197  * @param l The unsigned long to append.
198  * @return This Puts.
199  */
200  const Puts& operator<<( unsigned long l ) const {
201  ss_ << l;
202  return *this;
203  }
204 
205  /**
206  * Append an unsigned long long.
207  * @param l The unsigned long long to append.
208  * @return This Puts.
209  */
210  const Puts& operator<<( unsigned long long l ) const {
211  ss_ << l;
212  return *this;
213  }
214 
215  /**
216  * Append a float.
217  * @param f The float to append.
218  * @return This Puts.
219  */
220  const Puts& operator<<( float f ) const {
221  ss_ << f;
222  return *this;
223  }
224 
225  /**
226  * Append a double.
227  * @param d The double to append.
228  * @return This Puts.
229  */
230  const Puts& operator<<( double d ) const {
231  ss_ << d;
232  return *this;
233  }
234 
235  /**
236  * Append a void*.
237  * @param p The void* to append.
238  * @return This Puts.
239  */
240  const Puts& operator<<( void *p ) const {
241  ss_ << p;
242  return *this;
243  }
244 
245  /**
246  * Append a std::thread::id.
247  * @param id The std::thread::id to append.
248  * @return This Puts.
249  */
250  const Puts& operator<<( std::thread::id id ) const {
251  ss_ << id;
252  return *this;
253  }
254 
255  /**
256  * Appends std::endl
257  * @return This Puts.
258  */
259  const Puts& operator<<( Puts::endl ) const {
260  ss_ << std::endl;
261  return *this;
262  }
263 
264  /**
265  * Applies std::fixed
266  * @return This Puts.
267  */
268  const Puts& operator<<( fixed ) const {
269  ss_ << std::fixed;
270  return *this;
271  }
272 
273  /**
274  * Applies std::scientific
275  * @return This Puts.
276  */
277  const Puts& operator<<( scientific ) const {
278  ss_ << std::scientific;
279  return *this;
280  }
281 
282  /**
283  * Applies std::dec
284  * @return This Puts.
285  */
286  const Puts& operator<<( dec ) const {
287  ss_ << std::dec;
288  return *this;
289  }
290 
291  /**
292  * Applies std::oct
293  * @return This Puts.
294  */
295  const Puts& operator<<( oct ) const {
296  ss_ << std::oct;
297  return *this;
298  }
299 
300  /**
301  * Applies std::hex
302  * @return This Puts.
303  */
304  const Puts& operator<<( hex ) const {
305  ss_ << std::hex;
306  return *this;
307  }
308 
309  /**
310  * Applies std::setw
311  * @param w The width.
312  * @return This Piuts.
313  */
314  const Puts& operator<<( setw w ) const {
315  ss_ << std::setw( w.w_ );
316  return *this;
317  }
318 
319  /**
320  * Applies std::setprecision
321  * @param p The precision.
322  * @return This Piuts.
323  */
324  const Puts& operator<<( setprecision p ) const {
325  ss_ << std::setprecision( p.p_ );
326  return *this;
327  }
328 
329  /**
330  * Implicit Puts conversion to string for the compiler
331  * @return The string build in stringstream ss_.
332  */
333  operator std::string() const { return ss_.str(); };
334 
335  private:
336  /**
337  * Use a stringstream internally.
338  */
339  mutable std::stringstream ss_;
340 
341  };
342 
343 }
344 
345 #endif
dodo::common::Puts::operator<<
const Puts & operator<<(float f) const
Append a float.
Definition: puts.hpp:220
dodo::common::Puts::oct
Put the stream in octal mode.
Definition: puts.hpp:87
dodo::common::Puts::setw::w_
int w_
The width.
Definition: puts.hpp:100
dodo::common::Puts::operator<<
const Puts & operator<<(unsigned int i) const
Append an unsigned integer.
Definition: puts.hpp:190
dodo::common::Puts::setprecision
Set the precision for floating point fixed format.
Definition: puts.hpp:110
dodo::common::Puts::hex
Put the stream in hexadecimal mode.
Definition: puts.hpp:80
dodo::common::Puts::operator<<
const Puts & operator<<(void *p) const
Append a void*.
Definition: puts.hpp:240
dodo::common::Puts::setprecision::p_
int p_
The precision.
Definition: puts.hpp:115
dodo::common::Puts::operator<<
const Puts & operator<<(oct) const
Applies std::oct.
Definition: puts.hpp:295
dodo::common::Puts::operator<<
const Puts & operator<<(scientific) const
Applies std::scientific.
Definition: puts.hpp:277
dodo::common::Puts::operator<<
const Puts & operator<<(hex) const
Applies std::hex.
Definition: puts.hpp:304
dodo::common::Puts::operator<<
const Puts & operator<<(fixed) const
Applies std::fixed.
Definition: puts.hpp:268
dodo::common::Puts::setw::setw
setw(int w)
Construct with width.
Definition: puts.hpp:100
dodo::common::Puts::setprecision::setprecision
setprecision(int p)
Construct with precision.
Definition: puts.hpp:115
dodo::common::Puts::fixed
Put the stream in floating point fixed-format mode.
Definition: puts.hpp:59
dodo::common::Puts::scientific
Put the stream in floating point scientific-format mode.
Definition: puts.hpp:66
dodo::common::Puts::endl
Mimics std::endl.
Definition: puts.hpp:52
dodo::common::Puts::operator<<
const Puts & operator<<(setw w) const
Applies std::setw.
Definition: puts.hpp:314
dodo::common::Puts::operator<<
const Puts & operator<<(setprecision p) const
Applies std::setprecision.
Definition: puts.hpp:324
dodo::common::Puts::operator<<
const Puts & operator<<(char c) const
Append a char.
Definition: puts.hpp:150
dodo::common::Puts::operator<<
const Puts & operator<<(const std::string &s) const
Append a STL string.
Definition: puts.hpp:130
dodo::common::Puts::operator<<
const Puts & operator<<(Puts::endl) const
Appends std::endl.
Definition: puts.hpp:259
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::Puts::Puts
Puts()
Constructor inits to fixed format for double with precision 3.
Definition: puts.hpp:123
dodo::common::Puts::operator<<
const Puts & operator<<(const char *s) const
Append a C string.
Definition: puts.hpp:140
dodo::common::Puts::setw
Set the width of things to w characters.
Definition: puts.hpp:95
dodo::common::Puts::operator<<
const Puts & operator<<(unsigned long long l) const
Append an unsigned long long.
Definition: puts.hpp:210
dodo::common::Puts::ss_
std::stringstream ss_
Use a stringstream internally.
Definition: puts.hpp:333
dodo::common::Puts::operator<<
const Puts & operator<<(unsigned long l) const
Append an unsigned long.
Definition: puts.hpp:200
dodo::common::Puts::operator<<
const Puts & operator<<(dec) const
Applies std::dec.
Definition: puts.hpp:286
dodo::common::Puts
Helper class to write strings in stream format, eg.
Definition: puts.hpp:46
dodo::common::Puts::dec
Put the stream in decimal mode.
Definition: puts.hpp:73
dodo::common::Puts::operator<<
const Puts & operator<<(long l) const
Append a long.
Definition: puts.hpp:170
dodo::common::Puts::operator<<
const Puts & operator<<(int i) const
Append an integer.
Definition: puts.hpp:160
dodo::common::Puts::operator<<
const Puts & operator<<(std::thread::id id) const
Append a std::thread::id.
Definition: puts.hpp:250
dodo::common::Puts::operator<<
const Puts & operator<<(double d) const
Append a double.
Definition: puts.hpp:230
dodo::common::Puts::operator<<
const Puts & operator<<(long long l) const
Append a long long.
Definition: puts.hpp:180