fastmodel: Use a shared pointer to track PC events.
[gem5.git] / src / base / cprintf_formats.hh
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef __BASE_CPRINTF_FORMATS_HH__
30 #define __BASE_CPRINTF_FORMATS_HH__
31
32 #include <cstring>
33 #include <ostream>
34 #include <sstream>
35
36 namespace cp {
37
38 struct Format
39 {
40 bool alternate_form;
41 bool flush_left;
42 bool print_sign;
43 bool blank_space;
44 bool fill_zero;
45 bool uppercase;
46 enum { dec, hex, oct } base;
47 enum { none, string, integer, character, floating } format;
48 enum { best, fixed, scientific } float_format;
49 int precision;
50 int width;
51 bool get_precision;
52 bool get_width;
53
54 Format() { clear(); }
55
56 void clear()
57 {
58 alternate_form = false;
59 flush_left = false;
60 print_sign = false;
61 blank_space = false;
62 fill_zero = false;
63 uppercase = false;
64 base = dec;
65 format = none;
66 float_format = best;
67 precision = -1;
68 width = 0;
69 get_precision = false;
70 get_width = false;
71 }
72 };
73
74 template <typename T>
75 inline void
76 _format_char(std::ostream &out, const T &data, Format &fmt)
77 {
78 using namespace std;
79
80 out << data;
81 }
82
83 template <typename T>
84 inline void
85 _format_integer(std::ostream &out, const T &data, Format &fmt)
86 {
87 using namespace std;
88
89 ios::fmtflags flags(out.flags());
90
91 switch (fmt.base) {
92 case Format::hex:
93 out.setf(std::ios::hex, std::ios::basefield);
94 break;
95
96 case Format::oct:
97 out.setf(std::ios::oct, std::ios::basefield);
98 break;
99
100 case Format::dec:
101 out.setf(std::ios::dec, std::ios::basefield);
102 break;
103 }
104
105 if (fmt.alternate_form) {
106 if (!fmt.fill_zero)
107 out.setf(std::ios::showbase);
108 else {
109 switch (fmt.base) {
110 case Format::hex:
111 out << "0x";
112 fmt.width -= 2;
113 break;
114 case Format::oct:
115 out << "0";
116 fmt.width -= 1;
117 break;
118 case Format::dec:
119 break;
120 }
121 }
122 }
123
124 if (fmt.fill_zero)
125 out.fill('0');
126
127 if (fmt.width > 0)
128 out.width(fmt.width);
129
130 if (fmt.flush_left && !fmt.fill_zero)
131 out.setf(std::ios::left);
132
133 if (fmt.print_sign)
134 out.setf(std::ios::showpos);
135
136 if (fmt.uppercase)
137 out.setf(std::ios::uppercase);
138
139 out << data;
140
141 out.flags(flags);
142 }
143
144 template <typename T>
145 inline void
146 _format_float(std::ostream &out, const T &data, Format &fmt)
147 {
148 using namespace std;
149
150 ios::fmtflags flags(out.flags());
151
152 if (fmt.fill_zero)
153 out.fill('0');
154
155 switch (fmt.float_format) {
156 case Format::scientific:
157 if (fmt.precision != -1) {
158 if (fmt.width > 0)
159 out.width(fmt.width);
160
161 if (fmt.precision == 0)
162 fmt.precision = 1;
163 else
164 out.setf(std::ios::scientific);
165
166 out.precision(fmt.precision);
167 } else
168 if (fmt.width > 0)
169 out.width(fmt.width);
170
171 if (fmt.uppercase)
172 out.setf(std::ios::uppercase);
173 break;
174
175 case Format::fixed:
176 if (fmt.precision != -1) {
177 if (fmt.width > 0)
178 out.width(fmt.width);
179
180 out.setf(std::ios::fixed);
181 out.precision(fmt.precision);
182 } else
183 if (fmt.width > 0)
184 out.width(fmt.width);
185
186 break;
187
188 default:
189 if (fmt.precision != -1)
190 out.precision(fmt.precision);
191
192 if (fmt.width > 0)
193 out.width(fmt.width);
194
195 break;
196 }
197
198 out << data;
199
200 out.flags(flags);
201 }
202
203 template <typename T>
204 inline void
205 _format_string(std::ostream &out, const T &data, Format &fmt)
206 {
207 using namespace std;
208
209 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
210 if (fmt.width > 0) {
211 std::stringstream foo;
212 foo << data;
213 int flen = foo.str().size();
214
215 if (fmt.width > flen) {
216 char *spaces = new char[fmt.width - flen + 1];
217 memset(spaces, ' ', fmt.width - flen);
218 spaces[fmt.width - flen] = 0;
219
220 if (fmt.flush_left)
221 out << foo.str() << spaces;
222 else
223 out << spaces << foo.str();
224
225 delete [] spaces;
226 } else
227 out << data;
228 } else
229 out << data;
230 #else
231 if (fmt.width > 0)
232 out.width(fmt.width);
233 if (fmt.flush_left)
234 out.setf(std::ios::left);
235
236 out << data;
237 #endif
238 }
239
240 /////////////////////////////////////////////////////////////////////////////
241 //
242 // The code below controls the actual usage of formats for various types
243 //
244
245 //
246 // character formats
247 //
248 template <typename T>
249 inline void
250 format_char(std::ostream &out, const T &data, Format &fmt)
251 { out << "<bad arg type for char format>"; }
252
253 inline void
254 format_char(std::ostream &out, char data, Format &fmt)
255 { _format_char(out, data, fmt); }
256
257 inline void
258 format_char(std::ostream &out, unsigned char data, Format &fmt)
259 { _format_char(out, data, fmt); }
260
261 inline void
262 format_char(std::ostream &out, signed char data, Format &fmt)
263 { _format_char(out, data, fmt); }
264
265 inline void
266 format_char(std::ostream &out, short data, Format &fmt)
267 { _format_char(out, (char)data, fmt); }
268
269 inline void
270 format_char(std::ostream &out, unsigned short data, Format &fmt)
271 { _format_char(out, (char)data, fmt); }
272
273 inline void
274 format_char(std::ostream &out, int data, Format &fmt)
275 { _format_char(out, (char)data, fmt); }
276
277 inline void
278 format_char(std::ostream &out, unsigned int data, Format &fmt)
279 { _format_char(out, (char)data, fmt); }
280
281 inline void
282 format_char(std::ostream &out, long data, Format &fmt)
283 { _format_char(out, (char)data, fmt); }
284
285 inline void
286 format_char(std::ostream &out, unsigned long data, Format &fmt)
287 { _format_char(out, (char)data, fmt); }
288
289 inline void
290 format_char(std::ostream &out, long long data, Format &fmt)
291 { _format_char(out, (char)data, fmt); }
292
293 inline void
294 format_char(std::ostream &out, unsigned long long data, Format &fmt)
295 { _format_char(out, (char)data, fmt); }
296
297 //
298 // integer formats
299 //
300 template <typename T>
301 inline void
302 format_integer(std::ostream &out, const T &data, Format &fmt)
303 { _format_integer(out, data, fmt); }
304 inline void
305 format_integer(std::ostream &out, char data, Format &fmt)
306 { _format_integer(out, (int)data, fmt); }
307 inline void
308 format_integer(std::ostream &out, unsigned char data, Format &fmt)
309 { _format_integer(out, (int)data, fmt); }
310 inline void
311 format_integer(std::ostream &out, signed char data, Format &fmt)
312 { _format_integer(out, (int)data, fmt); }
313 inline void
314 format_integer(std::ostream &out, const unsigned char *data, Format &fmt)
315 { _format_integer(out, (uintptr_t)data, fmt); }
316 inline void
317 format_integer(std::ostream &out, const signed char *data, Format &fmt)
318 { _format_integer(out, (uintptr_t)data, fmt); }
319
320 //
321 // floating point formats
322 //
323 template <typename T>
324 inline void
325 format_float(std::ostream &out, const T &data, Format &fmt)
326 { out << "<bad arg type for float format>"; }
327
328 inline void
329 format_float(std::ostream &out, float data, Format &fmt)
330 { _format_float(out, data, fmt); }
331
332 inline void
333 format_float(std::ostream &out, double data, Format &fmt)
334 { _format_float(out, data, fmt); }
335
336 //
337 // string formats
338 //
339 template <typename T>
340 inline void
341 format_string(std::ostream &out, const T &data, Format &fmt)
342 { _format_string(out, data, fmt); }
343
344 } // namespace cp
345
346 #endif // __CPRINTF_FORMATS_HH__