Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Output.h
1 /*
2  * $Id$
3  * Output class implementation
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-12, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_IO_OUTPUT_H
23 #define ELM_IO_OUTPUT_H
24 
25 #include <elm/types.h>
26 #include <elm/sys/SystemIO.h>
27 #include <elm/util/VarArg.h>
28 #include <elm/string/String.h>
29 #include <elm/string/CString.h>
30 #include <elm/enum_info.h>
31 #include <elm/meta.h>
32 
33 namespace elm { namespace io {
34 
35 // alignment_t enum
36 typedef enum alignment_t {
37  NONE = 0,
41 } alignment_t;
42 
43 
44 // IntFormat class
45 class IntFormat {
46  inline void init(bool s, int size) {
47  _base = 10;
48  _width = 0;
49  _align = LEFT;
50  _pad = ' ';
51  _upper = false;
52  _sign = s;
53  _displaySign = false;
54  _size = size;
55  }
56 public:
57  inline IntFormat(void ) : _val(0) { init(true, 8); }
58  inline IntFormat(signed char value) : _val(value) { init(true, 1); }
59  inline IntFormat(unsigned char value) : _val(t::uint64(value)) { init(false, 1); }
60  inline IntFormat(signed short value) : _val(value) { init(true, 2); }
61  inline IntFormat(unsigned short value) : _val(t::uint64(value)) { init(false, 2); }
62  inline IntFormat(signed int value) : _val(value) { init(true, 4); }
63  inline IntFormat(unsigned int value) : _val(t::uint64(value)) { init(false, 4); }
64 #ifndef __LP64__
65  inline IntFormat(signed long value) : _val(value) { init(true, 4); }
66  inline IntFormat(unsigned long value) : _val(t::uint64(value)) { init(false, 4); }
67 #else
68  inline IntFormat(signed long value) : _val(value) { init(true, 8); }
69  inline IntFormat(unsigned long value) : _val(t::uint64(value)) { init(false, 8); }
70 #endif
71  inline IntFormat(signed long long value) : _val(value) { init(true, 8); }
72  inline IntFormat(unsigned long long value) : _val(value) { init(false, 8); }
73 
74  inline IntFormat operator()(t::int8 value) { IntFormat f = *this; f._val = value; return f; }
75  inline IntFormat operator()(t::uint8 value) { IntFormat f = *this; f._val = value; return f; }
76  inline IntFormat operator()(t::int16 value) { IntFormat f = *this; f._val = value; return f; }
77  inline IntFormat operator()(t::uint16 value) { IntFormat f = *this; f._val = value; return f; }
78  inline IntFormat operator()(t::int32 value) { IntFormat f = *this; f._val = value; return f; }
79  inline IntFormat operator()(t::uint32 value) { IntFormat f = *this; f._val = value; return f; }
80  inline IntFormat operator()(t::int64 value) { IntFormat f = *this; f._val = value; return f; }
81  inline IntFormat operator()(t::uint64 value) { IntFormat f = *this; f._val = value; return f; }
82 
83  inline IntFormat base(int b) { _base = b; return *this; }
84  inline IntFormat bin(void) { _base = 2; _sign = false; return *this; }
85  inline IntFormat oct(void) { _base = 8; _sign = false; return *this; }
86  inline IntFormat dec(void) { _base = 10; return *this; }
87  inline IntFormat hex(void) { _base = 16; _sign = false; return *this; }
88  inline IntFormat width(int w) { _width = w; return *this; }
89  inline IntFormat align(alignment_t a) { _align = a; return *this; }
90  inline IntFormat left(void) { _align = LEFT; return *this; }
91  inline IntFormat center(void) { _align = CENTER; return *this; }
92  inline IntFormat right(void) { _align = RIGHT; return *this; }
93  inline IntFormat upper(void) { _upper = true; return *this; }
94  inline IntFormat lower(void) { _upper = false; return *this; }
95  inline IntFormat sign(void) { _displaySign = true; return *this; }
96  inline IntFormat pad(char p) { _pad = p; return *this; }
97 
99  unsigned char _base;
100  unsigned char _width;
101  unsigned _align : 5;
102  unsigned _upper : 1;
103  unsigned _sign : 1;
104  unsigned _displaySign : 1;
105  char _pad;
106  char _size;
107 };
108 
109 
110 // FloatFormat class
111 class FloatFormat {
112 public:
113  typedef enum {
114  SHORTEST = 0,
115  DECIMAL = 1,
117  } style_t;
118 
119  inline FloatFormat(void): _val(0) { init(); }
120  inline FloatFormat(float val): _val(val) { init(); }
121  inline FloatFormat(double val): _val(val) { init(); }
122 
123  inline FloatFormat operator()(float val) { FloatFormat f = *this; f._val = val; return f; }
124  inline FloatFormat operator()(double val) { FloatFormat f = *this; f._val = val; return f; }
125 
126  inline FloatFormat width(int w) { _width = w; return *this; }
127  inline FloatFormat width(int w, int d) { _width = w; _decw = d; return *this; }
128  inline FloatFormat style(style_t s) { _style = s; return *this; }
129  inline FloatFormat shortest(void) { _style = SHORTEST; return *this; }
130  inline FloatFormat decimal(void) { _style = DECIMAL; return *this; }
131  inline FloatFormat scientific(void) { _style = SCIENTIFIC; return *this; }
132  inline FloatFormat align(alignment_t a) { _align = a; return *this; }
133  inline FloatFormat left(void) { _align = LEFT; return *this; }
134  inline FloatFormat center(void) { _align = CENTER; return *this; }
135  inline FloatFormat right(void) { _align = RIGHT; return *this; }
136  inline FloatFormat upper(void) { _upper = true; return *this; }
137  inline FloatFormat lower(void) { _upper = false; return *this; }
138  inline FloatFormat pad(char p) { _pad = p; return *this; }
139 
140  double _val;
141  unsigned char _width;
142  unsigned char _decw;
143  unsigned char _style;
144  unsigned char _align: 2;
145  unsigned char _upper: 1;
146  unsigned char _pad;
147 
148 private:
149  void init(void) {
150  _width = 0;
151  _decw = 5;
152  _style = SHORTEST;
153  _align = LEFT;
154  _upper = true;
155  _pad = ' ';
156  }
157 };
158 
159 
160 // Output class
161 class Output {
162  OutStream *strm;
163  char *horner(char *p, t::uint64 val, int base, char enc = 'a');
164 public:
165  inline Output(void): strm(&out) { };
166  inline Output(OutStream& stream): strm(&stream) { };
167  inline OutStream& stream(void) const { return *strm; };
168  inline void setStream(OutStream& stream) { strm = &stream; };
169  void flush(void);
170 
171  void print(bool value);
172  void print(char chr);
173  void print(double value);
174  void print(void *value);
175  inline void print(const char *str) { print(CString(str)); };
176  void print(const CString str);
177  void print(const String& str);
178  void print(const IntFormat& fmt);
179  void print(const FloatFormat& fmt);
180  void format(CString fmt, ...);
181  void format(CString fmt, VarArg& args);
182 
183  // deprecated
184  void print(t::int32 value);
185  void print(t::uint32 value);
186  void print(t::int64 value);
187  void print(t::uint64 value);
188 };
189 
190 
191 // operators accesses
192 template <class T> struct def_printer { static inline void print(Output& out, const T& v) { out.print("<not printable>"); } };
193 template <class T> struct enum_printer { static inline void print(Output& out, const T& v) { out.print(enum_info<T>::toString(v)); } };
194 template <class T> inline Output& operator<<(Output& out, const T& v)
195  { _if<type_info<T>::is_defined_enum, enum_printer<T>, def_printer<T> >::_::print(out, v); return out; }
196 template <class T> inline Output& operator<<(Output& out, T *v)
197  { out.print((void *)v); return out; }
198 inline Output& operator<<(Output& out, bool value) { out.print(value); return out; };
199 inline Output& operator<<(Output& out, char value) { out.print(value); return out; };
200 inline Output& operator<<(Output& out, unsigned char value) { out.print(IntFormat(value)); return out; };
201 inline Output& operator<<(Output& out, signed char value) { out.print(IntFormat(value)); return out; };
202 inline Output& operator<<(Output& out, short value) { out.print(IntFormat(value)); return out; };
203 inline Output& operator<<(Output& out, unsigned short value) { out.print(IntFormat(value)); return out; };
204 inline Output& operator<<(Output& out, int value) { out.print(IntFormat(value)); return out; };
205 inline Output& operator<<(Output& out, unsigned int value) { out.print(IntFormat(value)); return out; };
206 inline Output& operator<<(Output& out, long value) { out.print(IntFormat(value)); return out; };
207 inline Output& operator<<(Output& out, unsigned long value) { out.print(IntFormat(value)); return out; };
208 inline Output& operator<<(Output& out, long long value) { out.print(IntFormat(value)); return out; };
209 inline Output& operator<<(Output& out, unsigned long long value) { out.print(IntFormat(value)); return out; };
210 inline Output& operator<<(Output& out, float value) { out.print(value); return out; };
211 inline Output& operator<<(Output& out, double value) { out.print(value); return out; };
212 inline Output& operator<<(Output& out, const char *value) { out.print(value); return out; };
213 inline Output& operator<<(Output& out, char *value) { out.print(value); return out; };
214 inline Output& operator<<(Output& out, const CString value) { out.print(value); return out; };
215 inline Output& operator<<(Output& out, const string& value) { out.print(value); return out; };
216 inline Output& operator<<(Output& out, const IntFormat& value) { out.print(value); return out; };
217 inline Output& operator<<(Output& out, const FloatFormat& value) { out.print(value); return out; }
218 
219 
220 // End-of-line
221 const char endl = '\n';
222 
223 // starter macro
224 inline IntFormat f(signed char value) { return IntFormat(value); }
225 inline IntFormat f(unsigned char value) { return IntFormat(value); }
226 inline IntFormat f(signed short value) { return IntFormat(value); }
227 inline IntFormat f(unsigned short value) { return IntFormat(value); }
228 inline IntFormat f(signed int value) { return IntFormat(value); }
229 inline IntFormat f(unsigned int value) { return IntFormat(value); }
230 inline IntFormat f(signed long value) { return IntFormat(value); }
231 inline IntFormat f(unsigned long value) { return IntFormat(value); }
232 inline IntFormat f(signed long long value) { return IntFormat(value); }
233 inline IntFormat f(unsigned long long value) { return IntFormat(value); }
234 inline FloatFormat f(float value) { return FloatFormat(value); }
235 inline FloatFormat f(double value) { return FloatFormat(value); }
236 
237 // predefined styles
238 IntFormat pointer(const void *p);
240 
241 // set style macros (deprecated)
242 inline IntFormat base(int base, IntFormat fmt) { return fmt.base(base); }
243 inline IntFormat bin(IntFormat fmt) { return fmt.bin(); }
244 inline IntFormat oct(IntFormat fmt) { return fmt.oct(); }
245 inline IntFormat hex(IntFormat fmt) { return fmt.hex(); }
246 inline IntFormat sign(IntFormat fmt) { return fmt.sign(); }
247 inline IntFormat width(int width, IntFormat fmt) { return fmt.width(width); }
248 inline IntFormat align(alignment_t align, IntFormat fmt) { return fmt.align(align); }
249 inline IntFormat left(IntFormat fmt) { return fmt.left(); }
250 inline IntFormat right(IntFormat fmt) { return fmt.right(); }
251 inline IntFormat center(IntFormat fmt) { return fmt.center(); }
252 inline IntFormat pad(char pad, IntFormat fmt) { return fmt.pad(pad); }
253 inline IntFormat uppercase(IntFormat fmt) { return fmt.upper(); }
254 inline IntFormat lowercase(IntFormat fmt) { return fmt.lower(); }
255 
256 } } // elm::io
257 
258 #endif // ELM_IO_OUTPUT_H
IntFormat operator()(t::int16 value)
Definition: Output.h:76
unsigned _upper
Definition: Output.h:102
FloatFormat width(int w)
Definition: Output.h:126
IntFormat(signed char value)
Definition: Output.h:58
Definition: Output.h:111
IntFormat byte(t::uint8 b)
Definition: io_Output.cpp:873
Definition: CString.h:17
unsigned char _style
Definition: Output.h:143
Output(void)
Definition: Output.h:165
IntFormat pad(char pad, IntFormat fmt)
Definition: Output.h:252
Output(OutStream &stream)
Definition: Output.h:166
Definition: enum_info.h:30
void format(CString fmt,...)
Definition: io_Output.cpp:279
const char endl
Definition: Output.h:221
IntFormat width(int w)
Definition: Output.h:88
IntFormat uppercase(IntFormat fmt)
Definition: Output.h:253
FloatFormat shortest(void)
Definition: Output.h:129
IntFormat(signed int value)
Definition: Output.h:62
unsigned char _decw
Definition: Output.h:142
FloatFormat right(void)
Definition: Output.h:135
Definition: Output.h:161
IntFormat left(void)
Definition: Output.h:90
IntFormat oct(void)
Definition: Output.h:85
IntFormat hex(IntFormat fmt)
Definition: Output.h:245
unsigned _displaySign
Definition: Output.h:104
Definition: OutStream.h:30
int32_t int32
Definition: int.h:34
FloatFormat width(int w, int d)
Definition: Output.h:127
Definition: VarArg.h:25
Definition: Output.h:115
IntFormat bin(IntFormat fmt)
Definition: Output.h:243
Output & operator<<(Output &out, const T &v)
Definition: Output.h:194
Definition: Output.h:40
IntFormat(signed long value)
Definition: Output.h:65
IntFormat align(alignment_t align, IntFormat fmt)
Definition: Output.h:248
IntFormat(void)
Definition: Output.h:57
void setStream(OutStream &stream)
Definition: Output.h:168
IntFormat sign(IntFormat fmt)
Definition: Output.h:246
IntFormat operator()(t::uint16 value)
Definition: Output.h:77
IntFormat operator()(t::uint64 value)
Definition: Output.h:81
Definition: Output.h:116
FloatFormat(double val)
Definition: Output.h:121
IntFormat operator()(t::int64 value)
Definition: Output.h:80
FloatFormat align(alignment_t a)
Definition: Output.h:132
IntFormat right(IntFormat fmt)
Definition: Output.h:250
IntFormat lower(void)
Definition: Output.h:94
IntFormat operator()(t::uint32 value)
Definition: Output.h:79
FloatFormat decimal(void)
Definition: Output.h:130
OutStream & stream(void) const
Definition: Output.h:167
IntFormat base(int b)
Definition: Output.h:83
char _size
Definition: Output.h:106
void flush(void)
Definition: io_Output.cpp:268
uint32 size
Definition: int.h:41
char _pad
Definition: Output.h:105
int16_t int16
Definition: int.h:32
Definition: Output.h:38
Definition: Output.h:37
value_t value(CString name, int value)
Definition: rtti.h:40
void print(const char *str)
Definition: Output.h:175
IntFormat(signed short value)
Definition: Output.h:60
IntFormat align(alignment_t a)
Definition: Output.h:89
IntFormat pad(char p)
Definition: Output.h:96
void print(bool value)
Definition: io_Output.cpp:108
unsigned char _base
Definition: Output.h:99
IntFormat center(IntFormat fmt)
Definition: Output.h:251
Definition: Output.h:45
FloatFormat(void)
Definition: Output.h:119
unsigned _sign
Definition: Output.h:103
Definition: meta.h:18
Definition: Output.h:192
IntFormat(signed long long value)
Definition: Output.h:71
IntFormat(unsigned long value)
Definition: Output.h:66
uint8_t uint8
Definition: int.h:31
FloatFormat scientific(void)
Definition: Output.h:131
int64_t int64
Definition: int.h:36
Definition: Output.h:114
sys::SystemOutStream & out
Definition: system_SystemIO.cpp:101
IntFormat f(signed char value)
Definition: Output.h:224
Definition: Output.h:39
IntFormat hex(void)
Definition: Output.h:87
FloatFormat upper(void)
Definition: Output.h:136
IntFormat operator()(t::uint8 value)
Definition: Output.h:75
static void print(Output &out, const T &v)
Definition: Output.h:193
FloatFormat center(void)
Definition: Output.h:134
IntFormat(unsigned long long value)
Definition: Output.h:72
unsigned char _width
Definition: Output.h:100
double _val
Definition: Output.h:140
IntFormat(unsigned char value)
Definition: Output.h:59
FloatFormat style(style_t s)
Definition: Output.h:128
FloatFormat pad(char p)
Definition: Output.h:138
FloatFormat left(void)
Definition: Output.h:133
Definition: String.h:31
IntFormat lowercase(IntFormat fmt)
Definition: Output.h:254
Definition: Output.h:193
IntFormat oct(IntFormat fmt)
Definition: Output.h:244
IntFormat left(IntFormat fmt)
Definition: Output.h:249
unsigned _align
Definition: Output.h:101
unsigned char _align
Definition: Output.h:144
IntFormat center(void)
Definition: Output.h:91
IntFormat right(void)
Definition: Output.h:92
int8_t int8
Definition: int.h:30
unsigned char _width
Definition: Output.h:141
FloatFormat lower(void)
Definition: Output.h:137
alignment_t
Definition: Output.h:36
FloatFormat(float val)
Definition: Output.h:120
t::int64 _val
Definition: Output.h:98
style_t
Definition: Output.h:113
FloatFormat operator()(double val)
Definition: Output.h:124
IntFormat(unsigned int value)
Definition: Output.h:63
IntFormat sign(void)
Definition: Output.h:95
FloatFormat operator()(float val)
Definition: Output.h:123
IntFormat width(int width, IntFormat fmt)
Definition: Output.h:247
IntFormat bin(void)
Definition: Output.h:84
IntFormat operator()(t::int32 value)
Definition: Output.h:78
static void print(Output &out, const T &v)
Definition: Output.h:192
unsigned char _upper
Definition: Output.h:145
unsigned char _pad
Definition: Output.h:146
IntFormat pointer(const void *p)
Definition: io_Output.cpp:863
IntFormat operator()(t::int8 value)
Definition: Output.h:74
IntFormat upper(void)
Definition: Output.h:93
IntFormat base(int base, IntFormat fmt)
Definition: Output.h:242
IntFormat(unsigned short value)
Definition: Output.h:61
IntFormat dec(void)
Definition: Output.h:86
uint16_t uint16
Definition: int.h:33
uint32_t uint32
Definition: int.h:35
uint64_t uint64
Definition: int.h:37