The Battle for Wesnoth  1.15.9+dev
formatter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
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; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <sstream>
18 #include <utility>
19 
20 /**
21  * std::ostringstream wrapper.
22  *
23  * ostringstream's operator<< doesn't return a ostringstream&. It returns an
24  * ostream& instead. This is unfortunate, because it means that you can't do
25  * something like this: (ostringstream() << n).str() to convert an integer to a
26  * string, all in one line instead you have to use this far more tedious
27  * approach:
28  * ostringstream s;
29  * s << n;
30  * s.str();
31  * This class corrects this shortcoming, allowing something like this:
32  * string result = (formatter() << "blah " << n << x << " blah").str();
33  *
34  * Actually, due to the ref qualified versions below, you can get away with this
35  *
36  * string result = formatter() << "blah " << n << x << " blah";
37  */
38 class formatter
39 {
40 public:
42  : stream_()
43  {
44  }
45 
46  template<typename T>
47  formatter& operator<<(const T& o) &
48  {
49  stream_ << o;
50  return *this;
51  }
52 
53  template<typename T>
54  formatter&& operator<<(const T& o) &&
55  {
56  stream_ << o;
57  return std::move(*this);
58  }
59 
60  std::string str() const
61  {
62  return stream_.str();
63  }
64 
65  // Implicit x-value conversion to string
66  operator std::string() const &&
67  {
68  return stream_.str();
69  }
70 
71  // Support manipulators
72  formatter& operator<<(std::ostream&(*fn)(std::ostream&)) &
73  {
74  fn(stream_);
75  return *this;
76  }
77 
78  formatter&& operator<<(std::ostream&(*fn)(std::ostream&)) &&
79  {
80  fn(stream_);
81  return std::move(*this);
82  }
83 
84  formatter& operator<<(std::ios_base&(*fn)(std::ios_base&)) &
85  {
86  fn(stream_);
87  return *this;
88  }
89 
90  formatter&& operator<<(std::ios_base&(*fn)(std::ios_base&)) &&
91  {
92  fn(stream_);
93  return std::move(*this);
94  }
95 
96 private:
97  std::ostringstream stream_;
98 };
formatter && operator<<(std::ios_base &(*fn)(std::ios_base &)) &&
Definition: formatter.hpp:90
std::ostringstream wrapper.
Definition: formatter.hpp:38
formatter & operator<<(const T &o) &
Definition: formatter.hpp:47
formatter && operator<<(const T &o) &&
Definition: formatter.hpp:54
std::ostringstream stream_
Definition: formatter.hpp:97
formatter && operator<<(std::ostream &(*fn)(std::ostream &)) &&
Definition: formatter.hpp:78
std::string str() const
Definition: formatter.hpp:60
formatter & operator<<(std::ostream &(*fn)(std::ostream &)) &
Definition: formatter.hpp:72
formatter & operator<<(std::ios_base &(*fn)(std::ios_base &)) &
Definition: formatter.hpp:84