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 &);
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. */
25 Formatter &operator()(const T &a)
27 result += lexical_cast<std::string>(a, get_conversion());
32 #if __cplusplus>=201103L
33 template<typename T, typename... Tail>
34 Formatter &operator()(const T &a, const Tail &... tail)
36 return (*this)(a)(tail...);
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;
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 %). */
50 /** Reads the next conversion from the format string and returns a
51 corresponding Fmt object. */
55 inline Formatter format(const std::string &f)
56 { return Formatter(f); }
58 #if __cplusplus >= 201103L
59 template<typename... Args>
60 inline std::string format(const std::string &f, const Args &... args)
62 return Formatter(f)(args...).str();
67 inline std::string format(const std::string &f, const A1 &a1)
68 { return Formatter(f)(a1).str(); }
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(); }
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(); }
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(); }
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(); }