gem5  v20.1.0.0
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 cp {
41 
42 struct Print
43 {
44  protected:
45  std::ostream &stream;
46  const char *format;
47  const char *ptr;
48  bool cont;
49 
50  std::ios::fmtflags saved_flags;
51  char saved_fill;
54 
56  void process();
57  void process_flag();
58 
59  public:
60  Print(std::ostream &stream, const std::string &format);
61  Print(std::ostream &stream, const char *format);
62  ~Print();
63 
64  int
66  {
67  return data;
68  }
69 
70  template <typename T>
71  int
72  get_number(const T& data)
73  {
74  return 0;
75  }
76 
77  template <typename T>
78  void
79  add_arg(const T &data)
80  {
81  if (!cont)
82  process();
83 
84  if (fmt.get_width) {
85  fmt.get_width = false;
86  cont = true;
88  return;
89  }
90 
91  if (fmt.get_precision) {
92  fmt.get_precision = false;
93  cont = true;
95  return;
96  }
97 
98  switch (fmt.format) {
99  case Format::character:
101  break;
102 
103  case Format::integer:
105  break;
106 
107  case Format::floating:
109  break;
110 
111  case Format::string:
113  break;
114 
115  default:
116  stream << "<bad format>";
117  break;
118  }
119  }
120 
121  void end_args();
122 };
123 
124 } // namespace cp
125 
126 inline void
128 {
129  print.end_args();
130 }
131 
132 
133 template<typename T, typename ...Args> void
134 ccprintf(cp::Print &print, const T &value, const Args &...args)
135 {
136  print.add_arg(value);
137 
138  ccprintf(print, args...);
139 }
140 
141 
142 template<typename ...Args> void
143 ccprintf(std::ostream &stream, const char *format, const Args &...args)
144 {
145  cp::Print print(stream, format);
146 
147  ccprintf(print, args...);
148 }
149 
150 
151 template<typename ...Args> void
152 cprintf(const char *format, const Args &...args)
153 {
154  ccprintf(std::cout, format, args...);
155 }
156 
157 template<typename ...Args> std::string
158 csprintf(const char *format, const Args &...args)
159 {
160  std::stringstream stream;
161  ccprintf(stream, format, args...);
162  return stream.str();
163 }
164 
165 /*
166  * functions again with std::string. We have both so we don't waste
167  * time converting const char * to std::string since we don't take
168  * advantage of it.
169  */
170 template<typename ...Args> void
171 ccprintf(std::ostream &stream, const std::string &format, const Args &...args)
172 {
173  ccprintf(stream, format.c_str(), args...);
174 }
175 
176 template<typename ...Args> void
177 cprintf(const std::string &format, const Args &...args)
178 {
179  ccprintf(std::cout, format.c_str(), args...);
180 }
181 
182 template<typename ...Args> std::string
183 csprintf(const std::string &format, const Args &...args)
184 {
185  return csprintf(format.c_str(), args...);
186 }
187 
188 #endif // __CPRINTF_HH__
cp::Format::string
@ string
Definition: cprintf_formats.hh:47
cp::format_float
void format_float(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:325
cp::Print::ptr
const char * ptr
Definition: cprintf.hh:47
cp::Format::character
@ character
Definition: cprintf_formats.hh:47
cp::Format::format
enum cp::Format::@24 format
data
const char data[]
Definition: circlebuf.test.cc:42
cp::Print::get_number
int get_number(const T &data)
Definition: cprintf.hh:72
cp::Print::get_number
int get_number(int data)
Definition: cprintf.hh:65
cp::Format::floating
@ floating
Definition: cprintf_formats.hh:47
cp::Print::stream
std::ostream & stream
Definition: cprintf.hh:45
cp::Print::process
void process()
Definition: cprintf.cc:65
cp::Format::get_width
bool get_width
Definition: cprintf_formats.hh:52
cp::Print::~Print
~Print()
Definition: cprintf.cc:60
cp::format_char
void format_char(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:250
cp::Print::end_args
void end_args()
Definition: cprintf.cc:278
cp::Format::get_precision
bool get_precision
Definition: cprintf_formats.hh:51
cp::Print::cont
bool cont
Definition: cprintf.hh:48
cp::Format
Definition: cprintf_formats.hh:38
cp::Print::format
const char * format
Definition: cprintf.hh:46
cp::Format::precision
int precision
Definition: cprintf_formats.hh:49
cp::Format::integer
@ integer
Definition: cprintf_formats.hh:47
cp
Definition: cprintf.cc:40
cp::Print::saved_fill
char saved_fill
Definition: cprintf.hh:51
cprintf
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:152
cp::Print
Definition: cprintf.hh:42
cp::format_string
void format_string(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:341
cp::Print::saved_flags
std::ios::fmtflags saved_flags
Definition: cprintf.hh:50
cp::Format::width
int width
Definition: cprintf_formats.hh:50
cp::Print::saved_precision
int saved_precision
Definition: cprintf.hh:52
cp::Print::process_flag
void process_flag()
Definition: cprintf.cc:102
cp::Print::saved_width
int saved_width
Definition: cprintf.hh:53
cp::Print::Print
Print(std::ostream &stream, const std::string &format)
Definition: cprintf.cc:42
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
ArmISA::format
Bitfield< 31, 29 > format
Definition: miscregs_types.hh:640
cp::Print::fmt
Format fmt
Definition: cprintf.hh:55
cp::Print::add_arg
void add_arg(const T &data)
Definition: cprintf.hh:79
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
cprintf_formats.hh
cp::format_integer
void format_integer(std::ostream &out, const T &data, Format &fmt)
Definition: cprintf_formats.hh:302

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17