gem5  v21.1.0.2
cprintf.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 ARM Limited
3  * Copyright (c) 2002-2006 The Regents of The University of Michigan
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __BASE_CPRINTF_HH__
31 #define __BASE_CPRINTF_HH__
32 
33 #include <ios>
34 #include <iostream>
35 #include <list>
36 #include <string>
37 
38 #include "base/cprintf_formats.hh"
39 
40 namespace gem5
41 {
42 
43 namespace cp {
44 
45 struct Print
46 {
47  protected:
48  std::ostream &stream;
49  const char *format;
50  const char *ptr;
51  bool cont;
52 
53  std::ios::fmtflags savedFlags;
54  char savedFill;
57 
59  void process();
60  void processFlag();
61 
62  public:
63  Print(std::ostream &stream, const std::string &format);
64  Print(std::ostream &stream, const char *format);
65  ~Print();
66 
67  int
69  {
70  return data;
71  }
72 
73  template <typename T>
74  int
75  getNumber(const T& data)
76  {
77  return 0;
78  }
79 
80  template <typename T>
81  void
82  addArg(const T &data)
83  {
84  if (!cont)
85  process();
86 
87  if (fmt.getWidth) {
88  fmt.getWidth = false;
89  cont = true;
91  return;
92  }
93 
94  if (fmt.getPrecision) {
95  fmt.getPrecision = false;
96  cont = true;
98  return;
99  }
100 
101  switch (fmt.format) {
102  case Format::Character:
104  break;
105 
106  case Format::Integer:
108  break;
109 
110  case Format::Floating:
112  break;
113 
114  case Format::String:
116  break;
117 
118  default:
119  stream << "<bad format>";
120  break;
121  }
122  }
123 
124  void endArgs();
125 };
126 
127 } // namespace cp
128 
129 inline void
131 {
132  print.endArgs();
133 }
134 
135 
136 template<typename T, typename ...Args> void
137 ccprintf(cp::Print &print, const T &value, const Args &...args)
138 {
139  print.addArg(value);
140 
141  ccprintf(print, args...);
142 }
143 
144 
145 template<typename ...Args> void
146 ccprintf(std::ostream &stream, const char *format, const Args &...args)
147 {
148  cp::Print print(stream, format);
149 
150  ccprintf(print, args...);
151 }
152 
153 
154 template<typename ...Args> void
155 cprintf(const char *format, const Args &...args)
156 {
157  ccprintf(std::cout, format, args...);
158 }
159 
160 template<typename ...Args> std::string
161 csprintf(const char *format, const Args &...args)
162 {
163  std::stringstream stream;
164  ccprintf(stream, format, args...);
165  return stream.str();
166 }
167 
168 /*
169  * functions again with std::string. We have both so we don't waste
170  * time converting const char * to std::string since we don't take
171  * advantage of it.
172  */
173 template<typename ...Args> void
174 ccprintf(std::ostream &stream, const std::string &format, const Args &...args)
175 {
176  ccprintf(stream, format.c_str(), args...);
177 }
178 
179 template<typename ...Args> void
180 cprintf(const std::string &format, const Args &...args)
181 {
182  ccprintf(std::cout, format.c_str(), args...);
183 }
184 
185 template<typename ...Args> std::string
186 csprintf(const std::string &format, const Args &...args)
187 {
188  return csprintf(format.c_str(), args...);
189 }
190 
191 } // namespace gem5
192 
193 #endif // __CPRINTF_HH__
gem5::cp::Print::savedFlags
std::ios::fmtflags savedFlags
Definition: cprintf.hh:53
gem5::cp::Print::savedPrecision
int savedPrecision
Definition: cprintf.hh:55
gem5::cp::Print::fmt
Format fmt
Definition: cprintf.hh:58
gem5::ArmISA::format
Bitfield< 31, 29 > format
Definition: misc_types.hh:646
gem5::cprintf
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:155
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::cp::Format::getWidth
bool getWidth
Definition: cprintf_formats.hh:73
gem5::cp::Format::Floating
@ Floating
Definition: cprintf_formats.hh:62
gem5::cp::Format::Character
@ Character
Definition: cprintf_formats.hh:61
gem5::csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:161
gem5::cp::Format::width
int width
Definition: cprintf_formats.hh:71
gem5::cp::Format::getPrecision
bool getPrecision
Definition: cprintf_formats.hh:72
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::cp::Format::Integer
@ Integer
Definition: cprintf_formats.hh:60
gem5::cp::Print::processFlag
void processFlag()
Definition: cprintf.cc:103
gem5::cp::Print::ptr
const char * ptr
Definition: cprintf.hh:50
gem5::cp::Print::savedFill
char savedFill
Definition: cprintf.hh:54
gem5::cp::Print::getNumber
int getNumber(const T &data)
Definition: cprintf.hh:75
gem5::cp::Print::addArg
void addArg(const T &data)
Definition: cprintf.hh:82
gem5::cp::Print::stream
std::ostream & stream
Definition: cprintf.hh:48
gem5::cp::Format::String
@ String
Definition: cprintf_formats.hh:59
gem5::cp::Print::Print
Print(std::ostream &stream, const std::string &format)
Definition: cprintf.cc:43
gem5::cp::Print::savedWidth
int savedWidth
Definition: cprintf.hh:56
gem5::cp::Print::endArgs
void endArgs()
Definition: cprintf.cc:280
gem5::cp::formatString
static void formatString(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:388
gem5::cp::Print::getNumber
int getNumber(int data)
Definition: cprintf.hh:68
gem5::cp::Print::process
void process()
Definition: cprintf.cc:66
gem5::cp::formatFloat
static void formatFloat(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:366
gem5::cp::Format
Definition: cprintf_formats.hh:42
gem5::cp::Print::cont
bool cont
Definition: cprintf.hh:51
gem5::cp::Print::~Print
~Print()
Definition: cprintf.cc:61
gem5::cp::formatInteger
static void formatInteger(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:331
gem5::cp::formatChar
static void formatChar(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:255
gem5::cp::Print
Definition: cprintf.hh:45
gem5::cp::Print::format
const char * format
Definition: cprintf.hh:49
gem5::cp::Format::format
enum gem5::cp::Format::@21 format
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::cp::Format::precision
int precision
Definition: cprintf_formats.hh:70
cprintf_formats.hh

Generated on Tue Sep 21 2021 12:24:56 for gem5 by doxygen 1.8.17