-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat_string.hh
More file actions
47 lines (37 loc) · 930 Bytes
/
format_string.hh
File metadata and controls
47 lines (37 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef UTIL_FORMAT_STRING_HH
#define UTIL_FORMAT_STRING_HH
#include <string>
#include <sstream>
namespace Util
{
// class allows strings to be formatted like:
// Util::FormatString s("%1 there %2");
// s << "Hi" << ends
// << "Fred" << ends;
// std::cout << s << '\n';
class FormatString : public std::ostringstream
{
public:
explicit FormatString(const std::string& format)
: _format(format), _done_format(false)
{}
// get the formatted string
const std::string& get()
{
if( ! _done_format )
format();
return _output;
}
// other convenient ways to get the string
operator const std::string&() { return this->get(); }
//operator const char*() { return this->get().c_str(); }
private:
// do the formatting
void format();
private:
const std::string _format;
bool _done_format;
std::string _output;
};
}
#endif