3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
8 #ifndef MSP_STRINGS_FMT_H_
9 #define MSP_STRINGS_FMT_H_
17 Stores formatting information for converting variables into strings. Can be
18 applied to an std::ostream or fed to lexical_cast. Also used internally by
21 Formats can be constructed from printf-style conversion specifications, by
22 chaining calls to the various setter functions, or with a mixture of both.
24 Since type information for conversions is acquired through templates, the
25 meaning of the conversion specifier character is reduced to only specifying
26 what the conversion should look like. In particular, the d, i, u, c and s
27 conversions are no-ops.
33 Fmt(const char *f) { reset(); parse(f); }
34 Fmt(const std::string &f) { reset(); parse(f.c_str()); }
35 Fmt &width(unsigned w) { wd=w; return *this; }
36 Fmt &precision(unsigned p) { prec=p; return *this; }
37 Fmt &showpos(bool s=true) { spos=s; return *this; }
38 Fmt &fill(wchar_t f) { fillc=f; return *this; }
39 Fmt &fixed() { fmode=FIXED; return *this; }
40 Fmt &scientific() { fmode=SCI; return *this; }
41 Fmt &showpoint(bool s=true) { spoint=s; return *this; }
42 Fmt &showbase(bool s=true) { sbase=s; return *this; }
43 Fmt &left() { align=LEFT; return *this; }
44 Fmt &right() { align=RIGHT; return *this; }
45 Fmt &dec() { base=DEC; return *this; }
46 Fmt &hex() { base=HEX; return *this; }
47 Fmt &oct() { base=OCT; return *this; }
48 Fmt &uppercase(bool u=true) { ucase=u; return *this; }
50 void apply(std::ostream &) const;
83 void parse(const char *);
86 inline std::ostream &operator<<(std::ostream &o, const Fmt &f)
87 { f.apply(o); return o; }