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