]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.h
Require C++11 for building
[libs/core.git] / source / strings / format.h
1 #ifndef MSP_STRINGS_FORMAT_H_
2 #define MSP_STRINGS_FORMAT_H_
3
4 #include <string>
5 #include "lexicalcast.h"
6
7 namespace Msp {
8
9 /**
10 Printf-like string formatter class.
11 */
12 class Formatter
13 {
14 private:
15         std::string fmt;
16         std::string::iterator pos;
17         std::string result;
18
19 public:
20         Formatter(const std::string &);
21         Formatter(const Formatter &);
22         Formatter &operator=(const Formatter &);
23
24         /** Extracts the next conversion from the format string and formats the
25         given value with it.  Will throw if no more conversions are found. */
26         template<typename T>
27         Formatter &operator()(const T &a)
28         {
29                 result += lexical_cast<std::string>(a, get_conversion());
30                 advance();
31                 return *this;
32         }
33
34         template<typename T, typename... Tail>
35         Formatter &operator()(const T &a, const Tail &... tail)
36         {
37                 return (*this)(a)(tail...);
38         }
39
40         /** Returns the result of the formatting operation.  Will throw if not
41         enough values have been fed to the formatter. */
42         const std::string &str() const;
43
44 private:
45         /** Advances the iterator to the next conversion, adding literal characters
46         to the result.  The iterator is left at the second character of the
47         conversion (i.e. after the %). */
48         void advance();
49
50         /** Reads the next conversion from the format string and returns a
51         corresponding Fmt object. */
52         Fmt get_conversion();
53 };
54
55 inline Formatter format(const std::string &f)
56 { return Formatter(f); }
57
58 template<typename... Args>
59 inline std::string format(const std::string &f, const Args &... args)
60 {
61         return Formatter(f)(args...).str();
62 }
63
64 } // namespace Msp
65
66 #endif