cprintf.hh (9331:6630b3ffe7c0) | cprintf.hh (10292:933dfb9d8279) |
---|---|
1/* | 1/* |
2 * Copyright (c) 2014 ARM Limited |
|
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 --- 12 unchanged lines hidden (view full) --- 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 | 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 --- 12 unchanged lines hidden (view full) --- 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 |
|
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" | 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" |
41#include "base/varargs.hh" | |
42 43namespace cp { 44 | 43 44namespace cp { 45 |
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 --- 67 unchanged lines hidden (view full) --- 123 } 124 } 125 126 void end_args(); 127}; 128 129} // namespace cp 130 | 46struct Print 47{ 48 protected: 49 std::ostream &stream; 50 const char *format; 51 const char *ptr; 52 bool cont; 53 --- 67 unchanged lines hidden (view full) --- 121 } 122 } 123 124 void end_args(); 125}; 126 127} // namespace cp 128 |
131typedef VarArgs::List<cp::Print> CPrintfArgsList; 132 | |
133inline void | 129inline void |
134ccprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args) | 130ccprintf(cp::Print &print) |
135{ | 131{ |
136 cp::Print print(stream, format); 137 args.add_args(print); | 132 print.end_args(); |
138} 139 | 133} 134 |
140inline void 141ccprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION) | 135 136template<typename T, typename ...Args> void 137ccprintf(cp::Print &print, const T &value, const Args &...args) |
142{ | 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{ |
|
143 cp::Print print(stream, format); | 148 cp::Print print(stream, format); |
144 VARARGS_ADDARGS(print); | 149 150 ccprintf(print, args...); |
145} 146 | 151} 152 |
147inline void 148cprintf(const char *format, CPRINTF_DECLARATION) | 153 154template<typename ...Args> void 155cprintf(const char *format, const Args &...args) |
149{ | 156{ |
150 ccprintf(std::cout, format, VARARGS_ALLARGS); | 157 ccprintf(std::cout, format, args...); |
151} 152 | 158} 159 |
153inline std::string 154csprintf(const char *format, CPRINTF_DECLARATION) | 160template<typename ...Args> std::string 161csprintf(const char *format, const Args &...args) |
155{ 156 std::stringstream stream; | 162{ 163 std::stringstream stream; |
157 ccprintf(stream, format, VARARGS_ALLARGS); | 164 ccprintf(stream, format, args...); |
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 */ | 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 */ |
166inline void 167ccprintf(std::ostream &stream, const std::string &format, 168 const CPrintfArgsList &args) | 173template<typename ...Args> void 174ccprintf(std::ostream &stream, const std::string &format, const Args &...args) |
169{ | 175{ |
170 ccprintf(stream, format.c_str(), args); | 176 ccprintf(stream, format.c_str(), args...); |
171} 172 | 177} 178 |
173inline void 174ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION) | 179template<typename ...Args> void 180cprintf(const std::string &format, const Args &...args) |
175{ | 181{ |
176 ccprintf(stream, format.c_str(), VARARGS_ALLARGS); | 182 ccprintf(std::cout, format.c_str(), args...); |
177} 178 | 183} 184 |
179inline void 180cprintf(const std::string &format, CPRINTF_DECLARATION) | 185template<typename ...Args> std::string 186csprintf(const std::string &format, const Args &...args) |
181{ | 187{ |
182 ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS); | 188 return csprintf(format.c_str(), args...); |
183} 184 | 189} 190 |
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__ | 191#endif // __CPRINTF_HH__ |