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