1 #ifndef MSP_STRINGS_FMT_H_
2 #define MSP_STRINGS_FMT_H_
10 Stores formatting information for converting variables into strings. Can be
11 applied to an std::ostream or fed to lexical_cast. Also used internally by
14 Formats can be constructed from printf-style conversion specifications, by
15 chaining calls to the various setter functions, or with a mixture of both.
17 Since type information for conversions is acquired through templates, the
18 meaning of the conversion specifier character is reduced to only specifying
19 what the conversion should look like. In particular, the d, i, u, c and s
20 conversions are no-ops.
26 Fmt(const char *f) { reset(); parse(f); }
27 Fmt(const std::string &f) { reset(); parse(f.c_str()); }
28 Fmt &width(unsigned w) { wd=w; return *this; }
29 Fmt &precision(unsigned p) { prec=p; return *this; }
30 Fmt &showpos(bool s=true) { spos=s; return *this; }
31 Fmt &fill(wchar_t f) { fillc=f; return *this; }
32 Fmt &fixed() { fmode=FIXED; return *this; }
33 Fmt &scientific() { fmode=SCI; return *this; }
34 Fmt &showpoint(bool s=true) { spoint=s; return *this; }
35 Fmt &showbase(bool s=true) { sbase=s; return *this; }
36 Fmt &left() { align=LEFT; return *this; }
37 Fmt &right() { align=RIGHT; return *this; }
38 Fmt &dec() { base=DEC; return *this; }
39 Fmt &hex() { base=HEX; return *this; }
40 Fmt &oct() { base=OCT; return *this; }
41 Fmt &uppercase(bool u=true) { ucase=u; return *this; }
43 void apply(std::ostream &) const;
76 void parse(const char *);
79 inline std::ostream &operator<<(std::ostream &o, const Fmt &f)
80 { f.apply(o); return o; }