1 #ifndef MSP_STRINGS_FORMAT_H_
2 #define MSP_STRINGS_FORMAT_H_
5 #include <msp/core/mspcore_api.h>
6 #include "lexicalcast.h"
11 Printf-like string formatter class.
13 class MSPCORE_API Formatter
17 std::string::iterator pos;
21 Formatter(const std::string &);
22 Formatter(const Formatter &);
23 Formatter &operator=(const Formatter &);
25 /** Extracts the next conversion from the format string and formats the
26 given value with it. Will throw if no more conversions are found. */
28 Formatter &operator()(const T &a)
30 result += lexical_cast<std::string>(a, get_conversion());
35 template<typename T, typename... Tail>
36 Formatter &operator()(const T &a, const Tail &... tail)
38 return (*this)(a)(tail...);
41 /** Returns the result of the formatting operation. Will throw if not
42 enough values have been fed to the formatter. */
43 const std::string &str() const;
46 /** Advances the iterator to the next conversion, adding literal characters
47 to the result. The iterator is left at the second character of the
48 conversion (i.e. after the %). */
51 /** Reads the next conversion from the format string and returns a
52 corresponding Fmt object. */
56 inline Formatter format(const std::string &f)
57 { return Formatter(f); }
59 template<typename... Args>
60 inline std::string format(const std::string &f, const Args &... args)
62 return Formatter(f)(args...).str();