misc: Replace enable_if<>::type with enable_if_t<>.
[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 if (fmt.width > 0) {
208 std::stringstream foo;
209 foo << data;
210 int flen = foo.str().size();
211
212 if (fmt.width > flen) {
213 char spaces[fmt.width - flen + 1];
214 std::memset(spaces, ' ', fmt.width - flen);
215 spaces[fmt.width - flen] = 0;
216
217 if (fmt.flush_left)
218 out << foo.str() << spaces;
219 else
220 out << spaces << foo.str();
221 } else {
222 out << data;
223 }
224 } else {
225 out << data;
226 }
227 }
228
229 /////////////////////////////////////////////////////////////////////////////
230 //
231 // The code below controls the actual usage of formats for various types
232 //
233
234 //
235 // character formats
236 //
237 template <typename T>
238 inline void
239 format_char(std::ostream &out, const T &data, Format &fmt)
240 { out << "<bad arg type for char format>"; }
241
242 inline void
243 format_char(std::ostream &out, char data, Format &fmt)
244 { _format_char(out, data, fmt); }
245
246 inline void
247 format_char(std::ostream &out, unsigned char data, Format &fmt)
248 { _format_char(out, data, fmt); }
249
250 inline void
251 format_char(std::ostream &out, signed char data, Format &fmt)
252 { _format_char(out, data, fmt); }
253
254 inline void
255 format_char(std::ostream &out, short data, Format &fmt)
256 { _format_char(out, (char)data, fmt); }
257
258 inline void
259 format_char(std::ostream &out, unsigned short data, Format &fmt)
260 { _format_char(out, (char)data, fmt); }
261
262 inline void
263 format_char(std::ostream &out, int data, Format &fmt)
264 { _format_char(out, (char)data, fmt); }
265
266 inline void
267 format_char(std::ostream &out, unsigned int data, Format &fmt)
268 { _format_char(out, (char)data, fmt); }
269
270 inline void
271 format_char(std::ostream &out, long data, Format &fmt)
272 { _format_char(out, (char)data, fmt); }
273
274 inline void
275 format_char(std::ostream &out, unsigned long data, Format &fmt)
276 { _format_char(out, (char)data, fmt); }
277
278 inline void
279 format_char(std::ostream &out, long long data, Format &fmt)
280 { _format_char(out, (char)data, fmt); }
281
282 inline void
283 format_char(std::ostream &out, unsigned long long data, Format &fmt)
284 { _format_char(out, (char)data, fmt); }
285
286 //
287 // integer formats
288 //
289 template <typename T>
290 inline void
291 format_integer(std::ostream &out, const T &data, Format &fmt)
292 { _format_integer(out, data, fmt); }
293 inline void
294 format_integer(std::ostream &out, char data, Format &fmt)
295 { _format_integer(out, (int)data, fmt); }
296 inline void
297 format_integer(std::ostream &out, unsigned char data, Format &fmt)
298 { _format_integer(out, (int)data, fmt); }
299 inline void
300 format_integer(std::ostream &out, signed char data, Format &fmt)
301 { _format_integer(out, (int)data, fmt); }
302 inline void
303 format_integer(std::ostream &out, const unsigned char *data, Format &fmt)
304 { _format_integer(out, (uintptr_t)data, fmt); }
305 inline void
306 format_integer(std::ostream &out, const signed char *data, Format &fmt)
307 { _format_integer(out, (uintptr_t)data, fmt); }
308
309 //
310 // floating point formats
311 //
312 template <typename T>
313 inline void
314 format_float(std::ostream &out, const T &data, Format &fmt)
315 { out << "<bad arg type for float format>"; }
316
317 inline void
318 format_float(std::ostream &out, float data, Format &fmt)
319 { _format_float(out, data, fmt); }
320
321 inline void
322 format_float(std::ostream &out, double data, Format &fmt)
323 { _format_float(out, data, fmt); }
324
325 //
326 // string formats
327 //
328 template <typename T>
329 inline void
330 format_string(std::ostream &out, const T &data, Format &fmt)
331 { _format_string(out, data, fmt); }
332
333 } // namespace cp
334
335 #endif // __CPRINTF_FORMATS_HH__