1 #ifndef MSP_STRINGS_FORMAT_H_
2 #define MSP_STRINGS_FORMAT_H_
5 #include "lexicalcast.h"
10 Printf-like string formatter class.
16 std::string::iterator pos;
20 Formatter(const std::string &);
21 Formatter(const Formatter &);
22 Formatter &operator=(const Formatter &);
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. */
27 Formatter &operator()(const T &a)
29 result += lexical_cast<std::string>(a, get_conversion());
34 #if __cplusplus>=201103L
35 template<typename T, typename... Tail>
36 Formatter &operator()(const T &a, const Tail &... tail)
38 return (*this)(a)(tail...);
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;
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 %). */
52 /** Reads the next conversion from the format string and returns a
53 corresponding Fmt object. */
57 inline Formatter format(const std::string &f)
58 { return Formatter(f); }
60 #if __cplusplus >= 201103L
61 template<typename... Args>
62 inline std::string format(const std::string &f, const Args &... args)
64 return Formatter(f)(args...).str();
69 inline std::string format(const std::string &f, const A1 &a1)
70 { return Formatter(f)(a1).str(); }
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(); }
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(); }
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(); }
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(); }