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 template<typename T, typename... Tail>
35 Formatter &operator()(const T &a, const Tail &... tail)
37 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 template<typename... Args>
59 inline std::string format(const std::string &f, const Args &... args)
61 return Formatter(f)(args...).str();