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