From 70176fecd1ff04f7b8957f3110497d758310b569 Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Tue, 26 Aug 2014 10:13:45 -0400 Subject: base: Replace the internal varargs stuff with C++11 constructs We currently use our own home-baked support for type-safe variadic functions. This is confusing and somewhat limited (e.g., cprintf only supports a limited number of arguments). This changeset converts all uses of our internal varargs support to use C++11 variadic macros. --- src/base/cprintf.hh | 70 ++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) (limited to 'src/base/cprintf.hh') diff --git a/src/base/cprintf.hh b/src/base/cprintf.hh index e702fa3a6..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__ @@ -38,13 +40,9 @@ #include #include "base/cprintf_formats.hh" -#include "base/varargs.hh" namespace cp { -#define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print) -#define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print) - struct Print { protected: @@ -128,33 +126,42 @@ struct Print } // namespace cp -typedef VarArgs::List CPrintfArgsList; - 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(); } @@ -163,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) -{ - ccprintf(stream, format.c_str(), args); -} - -inline void -ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION) +template void +ccprintf(std::ostream &stream, const std::string &format, const Args &...args) { - ccprintf(stream, format.c_str(), VARARGS_ALLARGS); + ccprintf(stream, format.c_str(), args...); } -inline void -cprintf(const std::string &format, CPRINTF_DECLARATION) +template void +cprintf(const std::string &format, const Args &...args) { - ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS); + ccprintf(std::cout, format.c_str(), args...); } -inline std::string -csprintf(const std::string &format, CPRINTF_DECLARATION) +template std::string +csprintf(const std::string &format, const Args &...args) { - std::stringstream stream; - ccprintf(stream, format.c_str(), VARARGS_ALLARGS); - return stream.str(); + return csprintf(format.c_str(), args...); } #endif // __CPRINTF_HH__ -- cgit v1.2.3