]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.h
Add a NonCopyable utility base class
[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
22         /** Extracts the next conversion from the format string and formats the
23         given value with it.  Will throw if no more conversions are found. */
24         template<typename T>
25         Formatter &operator()(const T &a)
26         {
27                 result += lexical_cast<std::string>(a, get_conversion());
28                 advance();
29                 return *this;
30         }
31
32 #if __cplusplus>=201103L
33         template<typename T, typename... Tail>
34         Formatter &operator()(const T &a, const Tail &... tail)
35         {
36                 return (*this)(a)(tail...);
37         }
38 #endif
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 #if __cplusplus >= 201103L
59 template<typename... Args>
60 inline std::string format(const std::string &f, const Args &... args)
61 {
62         return Formatter(f)(args...).str();
63 }
64
65 #else
66 template<typename A1>
67 inline std::string format(const std::string &f, const A1 &a1)
68 { return Formatter(f)(a1).str(); }
69
70 template<typename A1, typename A2>
71 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2)
72 { return Formatter(f)(a1)(a2).str(); }
73
74 template<typename A1, typename A2, typename A3>
75 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3)
76 { return Formatter(f)(a1)(a2)(a3).str(); }
77
78 template<typename A1, typename A2, typename A3, typename A4>
79 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
80 { return Formatter(f)(a1)(a2)(a3)(a4).str(); }
81
82 template<typename A1, typename A2, typename A3, typename A4, typename A5>
83 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5)
84 { return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); }
85 #endif
86
87 } // namespace Msp
88
89 #endif