X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fbase%2Fcprintf.hh;h=94a74728da6266407943259a89b70f078c68cfb5;hb=97887eb6dc9e548c9b5719727fd4783ef157917c;hp=cff73a228c80afd9fee717619b8a89d9bbcb9fc8;hpb=ff90b8c1aa99711eeb55c5eba29519f77cbc041c;p=gem5.git diff --git a/src/base/cprintf.hh b/src/base/cprintf.hh index cff73a228..94a74728d 100644 --- a/src/base/cprintf.hh +++ b/src/base/cprintf.hh @@ -1,4 +1,5 @@ /* + * Copyright (c) 2014 ARM Limited * Copyright (c) 2002-2006 The Regents of The University of Michigan * All rights reserved. * @@ -27,6 +28,7 @@ * * Authors: Nathan Binkert * Steve Reinhardt + * Andreas Sandberg */ #ifndef __BASE_CPRINTF_HH__ @@ -37,38 +39,64 @@ #include #include -#include "base/varargs.hh" #include "base/cprintf_formats.hh" namespace cp { -#define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print) -#define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print) - struct Print { protected: std::ostream &stream; const char *format; const char *ptr; + bool cont; std::ios::fmtflags saved_flags; char saved_fill; int saved_precision; - void process(Format &fmt); + Format fmt; + void process(); + void process_flag(); public: Print(std::ostream &stream, const std::string &format); Print(std::ostream &stream, const char *format); ~Print(); + int + get_number(int data) + { + return data; + } + + template + int + get_number(const T& data) + { + return 0; + } + template void add_arg(const T &data) { - Format fmt; - process(fmt); + if (!cont) + process(); + + if (fmt.get_width) { + fmt.get_width = false; + cont = true; + fmt.width = get_number(data); + return; + } + + if (fmt.get_precision) { + fmt.get_precision = false; + cont = true; + fmt.precision = get_number(data); + return; + } switch (fmt.format) { case Format::character: @@ -96,35 +124,44 @@ struct Print void end_args(); }; -/* end namespace cp */ } - -typedef VarArgs::List CPrintfArgsList; +} // namespace cp inline void -ccprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args) +ccprintf(cp::Print &print) { - cp::Print print(stream, format); - args.add_args(print); + print.end_args(); } -inline void -ccprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION) + +template void +ccprintf(cp::Print &print, const T &value, const Args &...args) +{ + print.add_arg(value); + + ccprintf(print, args...); +} + + +template void +ccprintf(std::ostream &stream, const char *format, const Args &...args) { cp::Print print(stream, format); - VARARGS_ADDARGS(print); + + ccprintf(print, args...); } -inline void -cprintf(const char *format, CPRINTF_DECLARATION) + +template void +cprintf(const char *format, const Args &...args) { - ccprintf(std::cout, format, VARARGS_ALLARGS); + ccprintf(std::cout, format, args...); } -inline std::string -csprintf(const char *format, CPRINTF_DECLARATION) +template std::string +csprintf(const char *format, const Args &...args) { std::stringstream stream; - ccprintf(stream, format, VARARGS_ALLARGS); + ccprintf(stream, format, args...); return stream.str(); } @@ -133,31 +170,22 @@ csprintf(const char *format, CPRINTF_DECLARATION) * time converting const char * to std::string since we don't take * advantage of it. */ -inline void -ccprintf(std::ostream &stream, const std::string &format, - const CPrintfArgsList &args) +template void +ccprintf(std::ostream &stream, const std::string &format, const Args &...args) { - ccprintf(stream, format.c_str(), args); + ccprintf(stream, format.c_str(), args...); } -inline void -ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION) +template void +cprintf(const std::string &format, const Args &...args) { - ccprintf(stream, format.c_str(), VARARGS_ALLARGS); + ccprintf(std::cout, format.c_str(), args...); } -inline void -cprintf(const std::string &format, CPRINTF_DECLARATION) +template std::string +csprintf(const std::string &format, const Args &...args) { - ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS); -} - -inline std::string -csprintf(const std::string &format, CPRINTF_DECLARATION) -{ - std::stringstream stream; - ccprintf(stream, format.c_str(), VARARGS_ALLARGS); - return stream.str(); + return csprintf(format.c_str(), args...); } #endif // __CPRINTF_HH__