2 * Copyright (c) 2014 ARM Limited
3 * Copyright (c) 2002-2006 The Regents of The University of Michigan
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.
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.
29 * Authors: Nathan Binkert
34 #ifndef __BASE_CPRINTF_HH__
35 #define __BASE_CPRINTF_HH__
42 #include "base/cprintf_formats.hh"
54 std::ios::fmtflags saved_flags;
63 Print(std::ostream &stream, const std::string &format);
64 Print(std::ostream &stream, const char *format);
75 get_number(const T& data)
82 add_arg(const T &data)
88 fmt.get_width = false;
90 fmt.width = get_number(data);
94 if (fmt.get_precision) {
95 fmt.get_precision = false;
97 fmt.precision = get_number(data);
101 switch (fmt.format) {
102 case Format::character:
103 format_char(stream, data, fmt);
106 case Format::integer:
107 format_integer(stream, data, fmt);
110 case Format::floating:
111 format_float(stream, data, fmt);
115 format_string(stream, data, fmt);
119 stream << "<bad format>";
130 ccprintf(cp::Print &print)
136 template<typename T, typename ...Args> void
137 ccprintf(cp::Print &print, const T &value, const Args &...args)
139 print.add_arg(value);
141 ccprintf(print, args...);
145 template<typename ...Args> void
146 ccprintf(std::ostream &stream, const char *format, const Args &...args)
148 cp::Print print(stream, format);
150 ccprintf(print, args...);
154 template<typename ...Args> void
155 cprintf(const char *format, const Args &...args)
157 ccprintf(std::cout, format, args...);
160 template<typename ...Args> std::string
161 csprintf(const char *format, const Args &...args)
163 std::stringstream stream;
164 ccprintf(stream, format, args...);
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
173 template<typename ...Args> void
174 ccprintf(std::ostream &stream, const std::string &format, const Args &...args)
176 ccprintf(stream, format.c_str(), args...);
179 template<typename ...Args> void
180 cprintf(const std::string &format, const Args &...args)
182 ccprintf(std::cout, format.c_str(), args...);
185 template<typename ...Args> std::string
186 csprintf(const std::string &format, const Args &...args)
188 return csprintf(format.c_str(), args...);
191 #endif // __CPRINTF_HH__