gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cprintf_formats.hh
Go to the documentation of this file.
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 <cstdint>
33 #include <cstring>
34 #include <ostream>
35 #include <sstream>
36 
37 namespace gem5
38 {
39 
40 namespace cp
41 {
42 
43 struct Format
44 {
46  bool flushLeft;
47  bool printSign;
48  bool blankSpace;
49  bool fillZero;
50  bool uppercase;
51  enum
52  {
53  Dec,
54  Hex,
56  } base;
57  enum
58  {
64  } format;
65  enum
66  {
70  } floatFormat;
71  int precision;
72  int width;
74  bool getWidth;
75 
76  Format() { clear(); }
77 
78  void
80  {
81  alternateForm = false;
82  flushLeft = false;
83  printSign = false;
84  blankSpace = false;
85  fillZero = false;
86  uppercase = false;
87  base = Dec;
88  format = None;
89  floatFormat = Best;
90  precision = -1;
91  width = 0;
92  getPrecision = false;
93  getWidth = false;
94  }
95 };
96 
97 template <typename T>
98 static inline void
99 _formatChar(std::ostream &out, const T &data, Format &fmt)
100 {
101  out << data;
102 }
103 
104 template <typename T>
105 static inline void
106 _formatInteger(std::ostream &out, const T &data, Format &fmt)
107 {
108  std::ios::fmtflags flags(out.flags());
109 
110  switch (fmt.base) {
111  case Format::Hex:
112  out.setf(std::ios::hex, std::ios::basefield);
113  break;
114 
115  case Format::Oct:
116  out.setf(std::ios::oct, std::ios::basefield);
117  break;
118 
119  case Format::Dec:
120  out.setf(std::ios::dec, std::ios::basefield);
121  break;
122  }
123 
124  if (fmt.alternateForm) {
125  if (!fmt.fillZero) {
126  out.setf(std::ios::showbase);
127  } else {
128  switch (fmt.base) {
129  case Format::Hex:
130  out << "0x";
131  fmt.width -= 2;
132  break;
133  case Format::Oct:
134  out << "0";
135  fmt.width -= 1;
136  break;
137  case Format::Dec:
138  break;
139  }
140  }
141  }
142 
143  if (fmt.fillZero)
144  out.fill('0');
145 
146  if (fmt.width > 0)
147  out.width(fmt.width);
148 
149  if (fmt.flushLeft && !fmt.fillZero)
150  out.setf(std::ios::left);
151 
152  if (fmt.printSign)
153  out.setf(std::ios::showpos);
154 
155  if (fmt.uppercase)
156  out.setf(std::ios::uppercase);
157 
158  out << data;
159 
160  out.flags(flags);
161 }
162 
163 template <typename T>
164 static inline void
165 _formatFloat(std::ostream &out, const T &data, Format &fmt)
166 {
167  std::ios::fmtflags flags(out.flags());
168 
169  if (fmt.fillZero)
170  out.fill('0');
171 
172  switch (fmt.floatFormat) {
173  case Format::Scientific:
174  if (fmt.precision != -1) {
175  if (fmt.width > 0)
176  out.width(fmt.width);
177 
178  if (fmt.precision == 0)
179  fmt.precision = 1;
180  else
181  out.setf(std::ios::scientific);
182 
183  out.precision(fmt.precision);
184  } else if (fmt.width > 0) {
185  out.width(fmt.width);
186  }
187 
188  if (fmt.uppercase)
189  out.setf(std::ios::uppercase);
190  break;
191 
192  case Format::Fixed:
193  if (fmt.precision != -1) {
194  if (fmt.width > 0)
195  out.width(fmt.width);
196 
197  out.setf(std::ios::fixed);
198  out.precision(fmt.precision);
199  } else if (fmt.width > 0) {
200  out.width(fmt.width);
201  }
202 
203  break;
204 
205  default:
206  if (fmt.precision != -1)
207  out.precision(fmt.precision);
208 
209  if (fmt.width > 0)
210  out.width(fmt.width);
211 
212  break;
213  }
214 
215  out << data;
216 
217  out.flags(flags);
218 }
219 
220 template <typename T>
221 static inline void
222 _formatString(std::ostream &out, const T &data, Format &fmt)
223 {
224  if (fmt.width > 0) {
225  std::stringstream foo;
226  foo << data;
227  int flen = foo.str().size();
228 
229  if (fmt.width > flen) {
230  char spaces[fmt.width - flen + 1];
231  std::memset(spaces, ' ', fmt.width - flen);
232  spaces[fmt.width - flen] = 0;
233 
234  if (fmt.flushLeft)
235  out << foo.str() << spaces;
236  else
237  out << spaces << foo.str();
238  } else {
239  out << data;
240  }
241  } else {
242  out << data;
243  }
244 }
245 
247 //
248 // The code below controls the actual usage of formats for various types
249 //
250 
251 //
252 // character formats
253 //
254 template <typename T>
255 static inline void
256 formatChar(std::ostream &out, const T &data, Format &fmt)
257 {
258  out << "<bad arg type for char format>";
259 }
260 
261 static inline void
262 formatChar(std::ostream &out, char data, Format &fmt)
263 {
264  _formatChar(out, data, fmt);
265 }
266 
267 static inline void
268 formatChar(std::ostream &out, unsigned char data, Format &fmt)
269 {
270  _formatChar(out, data, fmt);
271 }
272 
273 static inline void
274 formatChar(std::ostream &out, signed char data, Format &fmt)
275 {
276  _formatChar(out, data, fmt);
277 }
278 
279 static inline void
280 formatChar(std::ostream &out, short data, Format &fmt)
281 {
282  _formatChar(out, (char)data, fmt);
283 }
284 
285 static inline void
286 formatChar(std::ostream &out, unsigned short data, Format &fmt)
287 {
288  _formatChar(out, (char)data, fmt);
289 }
290 
291 static inline void
292 formatChar(std::ostream &out, int data, Format &fmt)
293 {
294  _formatChar(out, (char)data, fmt);
295 }
296 
297 static inline void
298 formatChar(std::ostream &out, unsigned int data, Format &fmt)
299 {
300  _formatChar(out, (char)data, fmt);
301 }
302 
303 static inline void
304 formatChar(std::ostream &out, long data, Format &fmt)
305 {
306  _formatChar(out, (char)data, fmt);
307 }
308 
309 static inline void
310 formatChar(std::ostream &out, unsigned long data, Format &fmt)
311 {
312  _formatChar(out, (char)data, fmt);
313 }
314 
315 static inline void
316 formatChar(std::ostream &out, long long data, Format &fmt)
317 {
318  _formatChar(out, (char)data, fmt);
319 }
320 
321 static inline void
322 formatChar(std::ostream &out, unsigned long long data, Format &fmt)
323 {
324  _formatChar(out, (char)data, fmt);
325 }
326 
327 //
328 // integer formats
329 //
330 template <typename T>
331 static inline void
332 formatInteger(std::ostream &out, const T &data, Format &fmt)
333 {
334  _formatInteger(out, data, fmt);
335 }
336 static inline void
337 formatInteger(std::ostream &out, char data, Format &fmt)
338 {
339  _formatInteger(out, (int)data, fmt);
340 }
341 static inline void
342 formatInteger(std::ostream &out, unsigned char data, Format &fmt)
343 {
344  _formatInteger(out, (int)data, fmt);
345 }
346 static inline void
347 formatInteger(std::ostream &out, signed char data, Format &fmt)
348 {
349  _formatInteger(out, (int)data, fmt);
350 }
351 static inline void
352 formatInteger(std::ostream &out, const unsigned char *data, Format &fmt)
353 {
354  _formatInteger(out, (uintptr_t)data, fmt);
355 }
356 static inline void
357 formatInteger(std::ostream &out, const signed char *data, Format &fmt)
358 {
359  _formatInteger(out, (uintptr_t)data, fmt);
360 }
361 
362 //
363 // floating point formats
364 //
365 template <typename T>
366 static inline void
367 formatFloat(std::ostream &out, const T &data, Format &fmt)
368 {
369  out << "<bad arg type for float format>";
370 }
371 
372 static inline void
373 formatFloat(std::ostream &out, float data, Format &fmt)
374 {
375  _formatFloat(out, data, fmt);
376 }
377 
378 static inline void
379 formatFloat(std::ostream &out, double data, Format &fmt)
380 {
381  _formatFloat(out, data, fmt);
382 }
383 
384 //
385 // string formats
386 //
387 template <typename T>
388 static inline void
389 formatString(std::ostream &out, const T &data, Format &fmt)
390 {
391  _formatString(out, data, fmt);
392 }
393 
394 } // namespace cp
395 } // namespace gem5
396 
397 #endif // __CPRINTF_FORMATS_HH__
gem5::cp::_formatInteger
static void _formatInteger(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:106
gem5::cp::Format::floatFormat
enum gem5::cp::Format::@35 floatFormat
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::cp::Format::getWidth
bool getWidth
Definition: cprintf_formats.hh:74
gem5::cp::Format::Scientific
@ Scientific
Definition: cprintf_formats.hh:69
gem5::cp::Format::printSign
bool printSign
Definition: cprintf_formats.hh:47
gem5::cp::Format::Dec
@ Dec
Definition: cprintf_formats.hh:53
gem5::cp::Format::Floating
@ Floating
Definition: cprintf_formats.hh:63
gem5::cp::Format::clear
void clear()
Definition: cprintf_formats.hh:79
gem5::cp::Format::base
enum gem5::cp::Format::@33 base
gem5::cp::Format::Fixed
@ Fixed
Definition: cprintf_formats.hh:68
gem5::cp::Format::fillZero
bool fillZero
Definition: cprintf_formats.hh:49
gem5::cp::Format::String
@ String
Definition: cprintf_formats.hh:60
gem5::cp::Format::width
int width
Definition: cprintf_formats.hh:72
gem5::cp::Format::getPrecision
bool getPrecision
Definition: cprintf_formats.hh:73
gem5::cp::_formatFloat
static void _formatFloat(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:165
gem5::cp::_formatChar
static void _formatChar(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:99
gem5::cp::Format::Integer
@ Integer
Definition: cprintf_formats.hh:61
gem5::cp::_formatString
static void _formatString(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:222
gem5::cp::Format::Character
@ Character
Definition: cprintf_formats.hh:62
gem5::cp::Format::Hex
@ Hex
Definition: cprintf_formats.hh:54
flags
uint8_t flags
Definition: helpers.cc:66
gem5::cp::Format::uppercase
bool uppercase
Definition: cprintf_formats.hh:50
gem5::cp::Format::format
enum gem5::cp::Format::@34 format
gem5::cp::Format::Oct
@ Oct
Definition: cprintf_formats.hh:55
gem5::cp::Format::Best
@ Best
Definition: cprintf_formats.hh:67
gem5::cp::formatString
static void formatString(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:389
gem5::cp::formatFloat
static void formatFloat(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:367
gem5::cp::Format::Format
Format()
Definition: cprintf_formats.hh:76
gem5::cp::Format::blankSpace
bool blankSpace
Definition: cprintf_formats.hh:48
gem5::cp::Format
Definition: cprintf_formats.hh:43
gem5::cp::formatInteger
static void formatInteger(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:332
gem5::cp::formatChar
static void formatChar(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:256
gem5::cp::Format::None
@ None
Definition: cprintf_formats.hh:59
gem5::cp::Format::alternateForm
bool alternateForm
Definition: cprintf_formats.hh:45
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::cp::Format::precision
int precision
Definition: cprintf_formats.hh:71
gem5::cp::Format::flushLeft
bool flushLeft
Definition: cprintf_formats.hh:46

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17