]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.h
Remove deprecated things
[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 #if __cplusplus>=201103L
35         template<typename T, typename... Tail>
36         Formatter &operator()(const T &a, const Tail &... tail)
37         {
38                 return (*this)(a)(tail...);
39         }
40 #endif
41
42         /** Returns the result of the formatting operation.  Will throw if not
43         enough values have been fed to the formatter. */
44         const std::string &str() const;
45
46 private:
47         /** Advances the iterator to the next conversion, adding literal characters
48         to the result.  The iterator is left at the second character of the
49         conversion (i.e. after the %). */
50         void advance();
51
52         /** Reads the next conversion from the format string and returns a
53         corresponding Fmt object. */
54         Fmt get_conversion();
55 };
56
57 inline Formatter format(const std::string &f)
58 { return Formatter(f); }
59
60 #if __cplusplus >= 201103L
61 template<typename... Args>
62 inline std::string format(const std::string &f, const Args &... args)
63 {
64         return Formatter(f)(args...).str();
65 }
66
67 #else
68 template<typename A1>
69 inline std::string format(const std::string &f, const A1 &a1)
70 { return Formatter(f)(a1).str(); }
71
72 template<typename A1, typename A2>
73 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2)
74 { return Formatter(f)(a1)(a2).str(); }
75
76 template<typename A1, typename A2, typename A3>
77 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3)
78 { return Formatter(f)(a1)(a2)(a3).str(); }
79
80 template<typename A1, typename A2, typename A3, typename A4>
81 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
82 { return Formatter(f)(a1)(a2)(a3)(a4).str(); }
83
84 template<typename A1, typename A2, typename A3, typename A4, typename A5>
85 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5)
86 { return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); }
87 #endif
88
89 } // namespace Msp
90
91 #endif