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(a, get_conversion());
32 /** Returns the result of the formatting operation. Will throw if not
33 enough values have been fed to the formatter. */
34 const std::string &str() const;
37 /** Advances the iterator to the next conversion, adding literal characters
38 to the result. The iterator is left at the second character of the
39 conversion (i.e. after the %). */
42 /** Reads the next conversion from the format string and returns a
43 corresponding Fmt object. */
47 inline Formatter format(const std::string &f)
48 { return Formatter(f); }
51 inline std::string format(const std::string &f, const A1 &a1)
52 { return Formatter(f)(a1).str(); }
54 template<typename A1, typename A2>
55 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2)
56 { return Formatter(f)(a1)(a2).str(); }
58 template<typename A1, typename A2, typename A3>
59 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3)
60 { return Formatter(f)(a1)(a2)(a3).str(); }
62 template<typename A1, typename A2, typename A3, typename A4>
63 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
64 { return Formatter(f)(a1)(a2)(a3)(a4).str(); }
66 template<typename A1, typename A2, typename A3, typename A4, typename A5>
67 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5)
68 { return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); }