cprintf.hh revision 4039
12SN/A/* 24039Sbinkertn@umich.edu * Copyright (c) 2002-2006 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 324039Sbinkertn@umich.edu#ifndef __BASE_CPRINTF_HH__ 334039Sbinkertn@umich.edu#define __BASE_CPRINTF_HH__ 342SN/A 354039Sbinkertn@umich.edu#include <ios> 362SN/A#include <iostream> 372SN/A#include <list> 382SN/A#include <string> 392SN/A 404039Sbinkertn@umich.edu#include "base/varargs.hh" 412621SN/A#include "base/cprintf_formats.hh" 422621SN/A 432SN/Anamespace cp { 442SN/A 454039Sbinkertn@umich.edu#define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print) 464039Sbinkertn@umich.edu#define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print) 474039Sbinkertn@umich.edu 484039Sbinkertn@umich.edustruct Print 492SN/A{ 504039Sbinkertn@umich.edu protected: 514039Sbinkertn@umich.edu std::ostream &stream; 524039Sbinkertn@umich.edu const char *format; 534039Sbinkertn@umich.edu const char *ptr; 544039Sbinkertn@umich.edu 554039Sbinkertn@umich.edu std::ios::fmtflags saved_flags; 564039Sbinkertn@umich.edu char saved_fill; 574039Sbinkertn@umich.edu int saved_precision; 584039Sbinkertn@umich.edu 594039Sbinkertn@umich.edu void process(Format &fmt); 604039Sbinkertn@umich.edu 614039Sbinkertn@umich.edu public: 624039Sbinkertn@umich.edu Print(std::ostream &stream, const std::string &format); 634039Sbinkertn@umich.edu Print(std::ostream &stream, const char *format); 644039Sbinkertn@umich.edu ~Print(); 652SN/A 662SN/A template <typename T> 674039Sbinkertn@umich.edu void 684039Sbinkertn@umich.edu add_arg(const T &data) 692SN/A { 704039Sbinkertn@umich.edu Format fmt; 714039Sbinkertn@umich.edu process(fmt); 722SN/A 734039Sbinkertn@umich.edu switch (fmt.format) { 744039Sbinkertn@umich.edu case Format::character: 754039Sbinkertn@umich.edu format_char(stream, data, fmt); 764039Sbinkertn@umich.edu break; 772SN/A 784039Sbinkertn@umich.edu case Format::integer: 794039Sbinkertn@umich.edu format_integer(stream, data, fmt); 804039Sbinkertn@umich.edu break; 812SN/A 824039Sbinkertn@umich.edu case Format::floating: 834039Sbinkertn@umich.edu format_float(stream, data, fmt); 844039Sbinkertn@umich.edu break; 852SN/A 864039Sbinkertn@umich.edu case Format::string: 874039Sbinkertn@umich.edu format_string(stream, data, fmt); 884039Sbinkertn@umich.edu break; 892SN/A 904039Sbinkertn@umich.edu default: 914039Sbinkertn@umich.edu stream << "<bad format>"; 924039Sbinkertn@umich.edu break; 932SN/A } 942SN/A } 952SN/A 964039Sbinkertn@umich.edu void end_args(); 972SN/A}; 982SN/A 994039Sbinkertn@umich.edu/* end namespace cp */ } 1004039Sbinkertn@umich.edu 1014039Sbinkertn@umich.edutypedef VarArgs::List<cp::Print> CPrintfArgsList; 1024039Sbinkertn@umich.edu 1034039Sbinkertn@umich.eduinline void 1044039Sbinkertn@umich.educcprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args) 1052SN/A{ 1064039Sbinkertn@umich.edu cp::Print print(stream, format); 1074039Sbinkertn@umich.edu args.add_args(print); 1082SN/A} 1092SN/A 1104039Sbinkertn@umich.eduinline void 1114039Sbinkertn@umich.educcprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION) 1124039Sbinkertn@umich.edu{ 1134039Sbinkertn@umich.edu cp::Print print(stream, format); 1144039Sbinkertn@umich.edu VARARGS_ADDARGS(print); 1154039Sbinkertn@umich.edu} 1162SN/A 1174039Sbinkertn@umich.eduinline void 1184039Sbinkertn@umich.educprintf(const char *format, CPRINTF_DECLARATION) 1194039Sbinkertn@umich.edu{ 1204039Sbinkertn@umich.edu ccprintf(std::cout, format, VARARGS_ALLARGS); 1214039Sbinkertn@umich.edu} 1222SN/A 1234039Sbinkertn@umich.eduinline std::string 1244039Sbinkertn@umich.educsprintf(const char *format, CPRINTF_DECLARATION) 1254039Sbinkertn@umich.edu{ 1264039Sbinkertn@umich.edu std::stringstream stream; 1274039Sbinkertn@umich.edu ccprintf(stream, format, VARARGS_ALLARGS); 1284039Sbinkertn@umich.edu return stream.str(); 1294039Sbinkertn@umich.edu} 1304039Sbinkertn@umich.edu 1314039Sbinkertn@umich.edu/* 1324039Sbinkertn@umich.edu * functions again with std::string. We have both so we don't waste 1334039Sbinkertn@umich.edu * time converting const char * to std::string since we don't take 1344039Sbinkertn@umich.edu * advantage of it. 1354039Sbinkertn@umich.edu */ 1362SN/Ainline void 1374039Sbinkertn@umich.educcprintf(std::ostream &stream, const std::string &format, 1384039Sbinkertn@umich.edu const CPrintfArgsList &args) 1394039Sbinkertn@umich.edu{ 1404039Sbinkertn@umich.edu ccprintf(stream, format.c_str(), args); 1414039Sbinkertn@umich.edu} 1422SN/A 1432SN/Ainline void 1444039Sbinkertn@umich.educcprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION) 1454039Sbinkertn@umich.edu{ 1464039Sbinkertn@umich.edu ccprintf(stream, format, VARARGS_ALLARGS); 1474039Sbinkertn@umich.edu} 1482SN/A 1494039Sbinkertn@umich.eduinline void 1504039Sbinkertn@umich.educprintf(const std::string &format, CPRINTF_DECLARATION) 1514039Sbinkertn@umich.edu{ 1524039Sbinkertn@umich.edu ccprintf(std::cout, format, VARARGS_ALLARGS); 1534039Sbinkertn@umich.edu} 1544039Sbinkertn@umich.edu 1552SN/Ainline std::string 1564039Sbinkertn@umich.educsprintf(const std::string &format, CPRINTF_DECLARATION) 1574039Sbinkertn@umich.edu{ 1584039Sbinkertn@umich.edu std::stringstream stream; 1594039Sbinkertn@umich.edu ccprintf(stream, format, VARARGS_ALLARGS); 1604039Sbinkertn@umich.edu return stream.str(); 1612SN/A} 1622SN/A 1632SN/A#endif // __CPRINTF_HH__ 164