]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.h
Add move semantics to Variant
[libs/core.git] / source / strings / format.h
1 #ifndef MSP_STRINGS_FORMAT_H_
2 #define MSP_STRINGS_FORMAT_H_
3
4 #include <string>
5 #include <msp/core/mspcore_api.h>
6 #include "lexicalcast.h"
7
8 namespace Msp {
9
10 /**
11 Printf-like string formatter class.
12 */
13 class MSPCORE_API Formatter
14 {
15 private:
16         std::string fmt;
17         std::string::iterator pos;
18         std::string result;
19
20 public:
21         Formatter(const std::string &);
22         Formatter(const Formatter &);
23         Formatter &operator=(const Formatter &);
24
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. */
27         template<typename T>
28         Formatter &operator()(const T &a)
29         {
30                 result += lexical_cast<std::string>(a, get_conversion());
31                 advance();
32                 return *this;
33         }
34
35         template<typename T, typename... Tail>
36         Formatter &operator()(const T &a, const Tail &... tail)
37         {
38                 return (*this)(a)(tail...);
39         }
40
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;
44
45 private:
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 %). */
49         void advance();
50
51         /** Reads the next conversion from the format string and returns a
52         corresponding Fmt object. */
53         Fmt get_conversion();
54 };
55
56 inline Formatter format(const std::string &f)
57 { return Formatter(f); }
58
59 template<typename... Args>
60 inline std::string format(const std::string &f, const Args &... args)
61 {
62         return Formatter(f)(args...).str();
63 }
64
65 } // namespace Msp
66
67 #endif