]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.h
Drop copyright and license notices from source files
[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 "lexicalcast.h"
6
7 namespace Msp {
8
9 /**
10 Printf-like string formatter class.
11 */
12 class Formatter
13 {
14 private:
15         std::string fmt;
16         std::string::iterator pos;
17         std::string result;
18
19 public:
20         Formatter(const std::string &);
21
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. */
24         template<typename T>
25         Formatter &operator()(const T &a)
26         {
27                 result += lexical_cast(a, get_conversion());
28                 advance();
29                 return *this;
30         }
31
32         const std::string &str() const;
33 private:
34         void advance();
35         Fmt get_conversion();
36 };
37
38 inline Formatter format(const std::string &f)
39 { return Formatter(f); }
40
41 template<typename A1>
42 inline std::string format(const std::string &f, const A1 &a1)
43 { return Formatter(f)(a1).str(); }
44
45 template<typename A1, typename A2>
46 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2)
47 { return Formatter(f)(a1)(a2).str(); }
48
49 template<typename A1, typename A2, typename A3>
50 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3)
51 { return Formatter(f)(a1)(a2)(a3).str(); }
52
53 template<typename A1, typename A2, typename A3, typename A4>
54 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
55 { return Formatter(f)(a1)(a2)(a3)(a4).str(); }
56
57 template<typename A1, typename A2, typename A3, typename A4, typename A5>
58 inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5)
59 { return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); }
60
61 } // namespace Msp
62
63 #endif