cprintf.hh revision 11320
12SN/A/* 210292SAndreas.Sandberg@ARM.com * Copyright (c) 2014 ARM Limited 34039Sbinkertn@umich.edu * Copyright (c) 2002-2006 The Regents of The University of Michigan 42SN/A * All rights reserved. 52SN/A * 62SN/A * Redistribution and use in source and binary forms, with or without 72SN/A * modification, are permitted provided that the following conditions are 82SN/A * met: redistributions of source code must retain the above copyright 92SN/A * notice, this list of conditions and the following disclaimer; 102SN/A * redistributions in binary form must reproduce the above copyright 112SN/A * notice, this list of conditions and the following disclaimer in the 122SN/A * documentation and/or other materials provided with the distribution; 132SN/A * neither the name of the copyright holders nor the names of its 142SN/A * contributors may be used to endorse or promote products derived from 152SN/A * this software without specific prior written permission. 162SN/A * 172SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 182SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 192SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 202SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 222SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 232SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 242SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 252SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 262SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 272SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 282665Ssaidi@eecs.umich.edu * 292665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 302665Ssaidi@eecs.umich.edu * Steve Reinhardt 3110292SAndreas.Sandberg@ARM.com * Andreas Sandberg 322SN/A */ 332SN/A 344039Sbinkertn@umich.edu#ifndef __BASE_CPRINTF_HH__ 354039Sbinkertn@umich.edu#define __BASE_CPRINTF_HH__ 362SN/A 374039Sbinkertn@umich.edu#include <ios> 382SN/A#include <iostream> 392SN/A#include <list> 402SN/A#include <string> 412SN/A 428229Snate@binkert.org#include "base/cprintf_formats.hh" 432621SN/A 442SN/Anamespace cp { 452SN/A 464039Sbinkertn@umich.edustruct Print 472SN/A{ 484039Sbinkertn@umich.edu protected: 494039Sbinkertn@umich.edu std::ostream &stream; 504039Sbinkertn@umich.edu const char *format; 514039Sbinkertn@umich.edu const char *ptr; 525756Snate@binkert.org bool cont; 534039Sbinkertn@umich.edu 544039Sbinkertn@umich.edu std::ios::fmtflags saved_flags; 554039Sbinkertn@umich.edu char saved_fill; 564039Sbinkertn@umich.edu int saved_precision; 574039Sbinkertn@umich.edu 585756Snate@binkert.org Format fmt; 595756Snate@binkert.org void process(); 609331Schander.sudanthi@arm.com void process_flag(); 614039Sbinkertn@umich.edu 624039Sbinkertn@umich.edu public: 634039Sbinkertn@umich.edu Print(std::ostream &stream, const std::string &format); 644039Sbinkertn@umich.edu Print(std::ostream &stream, const char *format); 654039Sbinkertn@umich.edu ~Print(); 662SN/A 675756Snate@binkert.org int 685756Snate@binkert.org get_number(int data) 695756Snate@binkert.org { 705756Snate@binkert.org return data; 715756Snate@binkert.org } 7211320Ssteve.reinhardt@amd.com 735756Snate@binkert.org template <typename T> 745756Snate@binkert.org int 755756Snate@binkert.org get_number(const T& data) 765756Snate@binkert.org { 775756Snate@binkert.org return 0; 785756Snate@binkert.org } 795756Snate@binkert.org 802SN/A template <typename T> 814039Sbinkertn@umich.edu void 824039Sbinkertn@umich.edu add_arg(const T &data) 832SN/A { 845756Snate@binkert.org if (!cont) 855756Snate@binkert.org process(); 865756Snate@binkert.org 875756Snate@binkert.org if (fmt.get_width) { 885756Snate@binkert.org fmt.get_width = false; 895756Snate@binkert.org cont = true; 905756Snate@binkert.org fmt.width = get_number(data); 915756Snate@binkert.org return; 925756Snate@binkert.org } 9311320Ssteve.reinhardt@amd.com 945756Snate@binkert.org if (fmt.get_precision) { 955756Snate@binkert.org fmt.get_precision = false; 965756Snate@binkert.org cont = true; 975756Snate@binkert.org fmt.precision = get_number(data); 985756Snate@binkert.org return; 995756Snate@binkert.org } 1002SN/A 1014039Sbinkertn@umich.edu switch (fmt.format) { 1024039Sbinkertn@umich.edu case Format::character: 1034039Sbinkertn@umich.edu format_char(stream, data, fmt); 1044039Sbinkertn@umich.edu break; 1052SN/A 1064039Sbinkertn@umich.edu case Format::integer: 1074039Sbinkertn@umich.edu format_integer(stream, data, fmt); 1084039Sbinkertn@umich.edu break; 1092SN/A 1104039Sbinkertn@umich.edu case Format::floating: 1114039Sbinkertn@umich.edu format_float(stream, data, fmt); 1124039Sbinkertn@umich.edu break; 1132SN/A 1144039Sbinkertn@umich.edu case Format::string: 1154039Sbinkertn@umich.edu format_string(stream, data, fmt); 1164039Sbinkertn@umich.edu break; 1172SN/A 1184039Sbinkertn@umich.edu default: 1194039Sbinkertn@umich.edu stream << "<bad format>"; 1204039Sbinkertn@umich.edu break; 1212SN/A } 1222SN/A } 1232SN/A 1244039Sbinkertn@umich.edu void end_args(); 1252SN/A}; 1262SN/A 1277811Ssteve.reinhardt@amd.com} // namespace cp 1284039Sbinkertn@umich.edu 12910292SAndreas.Sandberg@ARM.cominline void 13010292SAndreas.Sandberg@ARM.comccprintf(cp::Print &print) 13110292SAndreas.Sandberg@ARM.com{ 13210292SAndreas.Sandberg@ARM.com print.end_args(); 13310292SAndreas.Sandberg@ARM.com} 1344039Sbinkertn@umich.edu 13510292SAndreas.Sandberg@ARM.com 13610292SAndreas.Sandberg@ARM.comtemplate<typename T, typename ...Args> void 13710292SAndreas.Sandberg@ARM.comccprintf(cp::Print &print, const T &value, const Args &...args) 13810292SAndreas.Sandberg@ARM.com{ 13910292SAndreas.Sandberg@ARM.com print.add_arg(value); 14010292SAndreas.Sandberg@ARM.com 14110292SAndreas.Sandberg@ARM.com ccprintf(print, args...); 14210292SAndreas.Sandberg@ARM.com} 14310292SAndreas.Sandberg@ARM.com 14410292SAndreas.Sandberg@ARM.com 14510292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 14610292SAndreas.Sandberg@ARM.comccprintf(std::ostream &stream, const char *format, const Args &...args) 1472SN/A{ 1484039Sbinkertn@umich.edu cp::Print print(stream, format); 14910292SAndreas.Sandberg@ARM.com 15010292SAndreas.Sandberg@ARM.com ccprintf(print, args...); 1512SN/A} 1522SN/A 15310292SAndreas.Sandberg@ARM.com 15410292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 15510292SAndreas.Sandberg@ARM.comcprintf(const char *format, const Args &...args) 1564039Sbinkertn@umich.edu{ 15710292SAndreas.Sandberg@ARM.com ccprintf(std::cout, format, args...); 1584039Sbinkertn@umich.edu} 1592SN/A 16010292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> std::string 16110292SAndreas.Sandberg@ARM.comcsprintf(const char *format, const Args &...args) 1624039Sbinkertn@umich.edu{ 1634039Sbinkertn@umich.edu std::stringstream stream; 16410292SAndreas.Sandberg@ARM.com ccprintf(stream, format, args...); 1654039Sbinkertn@umich.edu return stream.str(); 1664039Sbinkertn@umich.edu} 1674039Sbinkertn@umich.edu 1684039Sbinkertn@umich.edu/* 1694039Sbinkertn@umich.edu * functions again with std::string. We have both so we don't waste 1704039Sbinkertn@umich.edu * time converting const char * to std::string since we don't take 1714039Sbinkertn@umich.edu * advantage of it. 1724039Sbinkertn@umich.edu */ 17310292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 17410292SAndreas.Sandberg@ARM.comccprintf(std::ostream &stream, const std::string &format, const Args &...args) 1754039Sbinkertn@umich.edu{ 17610292SAndreas.Sandberg@ARM.com ccprintf(stream, format.c_str(), args...); 1774039Sbinkertn@umich.edu} 1782SN/A 17910292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 18010292SAndreas.Sandberg@ARM.comcprintf(const std::string &format, const Args &...args) 1814039Sbinkertn@umich.edu{ 18210292SAndreas.Sandberg@ARM.com ccprintf(std::cout, format.c_str(), args...); 1834039Sbinkertn@umich.edu} 1842SN/A 18510292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> std::string 18610292SAndreas.Sandberg@ARM.comcsprintf(const std::string &format, const Args &...args) 1874039Sbinkertn@umich.edu{ 18810292SAndreas.Sandberg@ARM.com return csprintf(format.c_str(), args...); 1892SN/A} 1902SN/A 1912SN/A#endif // __CPRINTF_HH__ 192