]> git.tdb.fi Git - libs/core.git/blob - source/strings/fmt.h
Exception changes for Fmt and Formatter
[libs/core.git] / source / strings / fmt.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2008 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_STRINGS_FMT_H_
9 #define MSP_STRINGS_FMT_H_
10
11 #include <ostream>
12 #include <stdexcept>
13 #include <string>
14
15 namespace Msp {
16
17 class format_error: public std::logic_error
18 {
19 public:
20         format_error(const std::string &w): std::logic_error(w) { }
21         virtual ~format_error() throw() { }
22 };
23
24
25 /**
26 Stores formatting information for converting variables into strings.  Can be
27 applied to an std::ostream or fed to lexical_cast.  Also used internally by
28 Formatter.
29
30 Formats can be constructed from printf-style conversion specifications, by
31 chaining calls to the various setter functions, or with a mixture of both.
32
33 Since type information for conversions is acquired through templates, the
34 meaning of the conversion specifier character is reduced to only specifying
35 what the conversion should look like.  Of special note is the s conversion,
36 which will result in a default conversion for any data type.  Size modifiers
37 are not supported and there is no difference between signed and unsigned
38 conversions.
39
40 Some new conversions are supported:
41
42   b/B  Binary integer conversion
43   P    Uppercase pointer conversion (like %#X)
44 */
45 class Fmt
46 {
47 public:
48         enum Type
49         {
50                 NUM,
51                 CHAR,
52                 STR
53         };
54
55         enum Base
56         {
57                 AUTOBASE = 0,
58                 DEC = 10,
59                 HEX = 16,
60                 OCT = 8,
61                 BIN = 2
62         };
63
64         enum FloatMode
65         {
66                 FIXED,
67                 AUTOFLT,
68                 SCI
69         };
70
71         enum Align
72         {
73                 LEFT,
74                 RIGHT
75         };
76
77 private:
78         unsigned wd;
79         unsigned prec;
80         bool spos;
81         wchar_t fillc;
82         Base base;
83         bool sbase;
84         FloatMode fmode;
85         bool spoint;
86         Align align;
87         bool ucase;
88         Type type;
89
90 public:
91         Fmt() { reset(); }
92         Fmt(const char *f) { reset(); parse(f); }
93         Fmt(const std::string &f) { reset(); parse(f.c_str()); }
94
95 private:
96         void parse(const char *);
97
98 public:
99         Fmt &width(unsigned w) { wd = w; return *this; }
100         Fmt &precision(unsigned p) { prec = p; return *this; }
101         Fmt &showpos(bool s=true) { spos = s; return *this; }
102         Fmt &fill(wchar_t f) { fillc = f; return *this; }
103         Fmt &fixed() { fmode = FIXED; return *this; }
104         Fmt &scientific() { fmode = SCI; return *this; }
105         Fmt &showpoint(bool s=true) { spoint = s; return *this; }
106         Fmt &showbase(bool s=true) { sbase = s; return *this; }
107         Fmt &left() { align = LEFT; return *this; }
108         Fmt &right() { align = RIGHT; return *this; }
109         Fmt &dec() { base = DEC; return *this; }
110         Fmt &hex() { base = HEX; return *this; }
111         Fmt &oct() { base = OCT; return *this; }
112         Fmt &bin() { base = BIN; return *this; }
113         Fmt &uppercase(bool u=true) { ucase = u; return *this; }
114         Fmt &numeric() { type = NUM; return *this; }
115         Fmt &character() { type = CHAR; return *this; }
116         Fmt &string() { type = STR; return *this; }
117         Fmt &reset();
118
119         unsigned get_width() const { return wd; }
120         unsigned get_precision() const { return prec; }
121         bool get_showpos() const { return spos; }
122         wchar_t get_fill() const { return fillc; }
123         Base get_base() const { return base; }
124         bool get_showbase() const { return sbase; }
125         FloatMode get_floatmode() const { return fmode; }
126         bool get_showpoint() const { return spoint; }
127         Align get_align() const { return align; }
128         bool get_uppercase() const { return ucase; }
129         Type get_type() const { return type; }
130
131         void apply(std::ostream &) const;
132 };
133
134 inline std::ostream &operator<<(std::ostream &o, const Fmt &f)
135 { f.apply(o); return o; }
136
137 } // namespace Msp
138
139 #endif