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; 5712979Sgabeblack@google.com int saved_width; 584039Sbinkertn@umich.edu 595756Snate@binkert.org Format fmt; 605756Snate@binkert.org void process(); 619331Schander.sudanthi@arm.com void process_flag(); 624039Sbinkertn@umich.edu 634039Sbinkertn@umich.edu public: 644039Sbinkertn@umich.edu Print(std::ostream &stream, const std::string &format); 654039Sbinkertn@umich.edu Print(std::ostream &stream, const char *format); 664039Sbinkertn@umich.edu ~Print(); 672SN/A 685756Snate@binkert.org int 695756Snate@binkert.org get_number(int data) 705756Snate@binkert.org { 715756Snate@binkert.org return data; 725756Snate@binkert.org } 7311320Ssteve.reinhardt@amd.com 745756Snate@binkert.org template <typename T> 755756Snate@binkert.org int 765756Snate@binkert.org get_number(const T& data) 775756Snate@binkert.org { 785756Snate@binkert.org return 0; 795756Snate@binkert.org } 805756Snate@binkert.org 812SN/A template <typename T> 824039Sbinkertn@umich.edu void 834039Sbinkertn@umich.edu add_arg(const T &data) 842SN/A { 855756Snate@binkert.org if (!cont) 865756Snate@binkert.org process(); 875756Snate@binkert.org 885756Snate@binkert.org if (fmt.get_width) { 895756Snate@binkert.org fmt.get_width = false; 905756Snate@binkert.org cont = true; 915756Snate@binkert.org fmt.width = get_number(data); 925756Snate@binkert.org return; 935756Snate@binkert.org } 9411320Ssteve.reinhardt@amd.com 955756Snate@binkert.org if (fmt.get_precision) { 965756Snate@binkert.org fmt.get_precision = false; 975756Snate@binkert.org cont = true; 985756Snate@binkert.org fmt.precision = get_number(data); 995756Snate@binkert.org return; 1005756Snate@binkert.org } 1012SN/A 1024039Sbinkertn@umich.edu switch (fmt.format) { 1034039Sbinkertn@umich.edu case Format::character: 1044039Sbinkertn@umich.edu format_char(stream, data, fmt); 1054039Sbinkertn@umich.edu break; 1062SN/A 1074039Sbinkertn@umich.edu case Format::integer: 1084039Sbinkertn@umich.edu format_integer(stream, data, fmt); 1094039Sbinkertn@umich.edu break; 1102SN/A 1114039Sbinkertn@umich.edu case Format::floating: 1124039Sbinkertn@umich.edu format_float(stream, data, fmt); 1134039Sbinkertn@umich.edu break; 1142SN/A 1154039Sbinkertn@umich.edu case Format::string: 1164039Sbinkertn@umich.edu format_string(stream, data, fmt); 1174039Sbinkertn@umich.edu break; 1182SN/A 1194039Sbinkertn@umich.edu default: 1204039Sbinkertn@umich.edu stream << "<bad format>"; 1214039Sbinkertn@umich.edu break; 1222SN/A } 1232SN/A } 1242SN/A 1254039Sbinkertn@umich.edu void end_args(); 1262SN/A}; 1272SN/A 1287811Ssteve.reinhardt@amd.com} // namespace cp 1294039Sbinkertn@umich.edu 13010292SAndreas.Sandberg@ARM.cominline void 13110292SAndreas.Sandberg@ARM.comccprintf(cp::Print &print) 13210292SAndreas.Sandberg@ARM.com{ 13310292SAndreas.Sandberg@ARM.com print.end_args(); 13410292SAndreas.Sandberg@ARM.com} 1354039Sbinkertn@umich.edu 13610292SAndreas.Sandberg@ARM.com 13710292SAndreas.Sandberg@ARM.comtemplate<typename T, typename ...Args> void 13810292SAndreas.Sandberg@ARM.comccprintf(cp::Print &print, const T &value, const Args &...args) 13910292SAndreas.Sandberg@ARM.com{ 14010292SAndreas.Sandberg@ARM.com print.add_arg(value); 14110292SAndreas.Sandberg@ARM.com 14210292SAndreas.Sandberg@ARM.com ccprintf(print, args...); 14310292SAndreas.Sandberg@ARM.com} 14410292SAndreas.Sandberg@ARM.com 14510292SAndreas.Sandberg@ARM.com 14610292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 14710292SAndreas.Sandberg@ARM.comccprintf(std::ostream &stream, const char *format, const Args &...args) 1482SN/A{ 1494039Sbinkertn@umich.edu cp::Print print(stream, format); 15010292SAndreas.Sandberg@ARM.com 15110292SAndreas.Sandberg@ARM.com ccprintf(print, args...); 1522SN/A} 1532SN/A 15410292SAndreas.Sandberg@ARM.com 15510292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 15610292SAndreas.Sandberg@ARM.comcprintf(const char *format, const Args &...args) 1574039Sbinkertn@umich.edu{ 15810292SAndreas.Sandberg@ARM.com ccprintf(std::cout, format, args...); 1594039Sbinkertn@umich.edu} 1602SN/A 16110292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> std::string 16210292SAndreas.Sandberg@ARM.comcsprintf(const char *format, const Args &...args) 1634039Sbinkertn@umich.edu{ 1644039Sbinkertn@umich.edu std::stringstream stream; 16510292SAndreas.Sandberg@ARM.com ccprintf(stream, format, args...); 1664039Sbinkertn@umich.edu return stream.str(); 1674039Sbinkertn@umich.edu} 1684039Sbinkertn@umich.edu 1694039Sbinkertn@umich.edu/* 1704039Sbinkertn@umich.edu * functions again with std::string. We have both so we don't waste 1714039Sbinkertn@umich.edu * time converting const char * to std::string since we don't take 1724039Sbinkertn@umich.edu * advantage of it. 1734039Sbinkertn@umich.edu */ 17410292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 17510292SAndreas.Sandberg@ARM.comccprintf(std::ostream &stream, const std::string &format, const Args &...args) 1764039Sbinkertn@umich.edu{ 17710292SAndreas.Sandberg@ARM.com ccprintf(stream, format.c_str(), args...); 1784039Sbinkertn@umich.edu} 1792SN/A 18010292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> void 18110292SAndreas.Sandberg@ARM.comcprintf(const std::string &format, const Args &...args) 1824039Sbinkertn@umich.edu{ 18310292SAndreas.Sandberg@ARM.com ccprintf(std::cout, format.c_str(), args...); 1844039Sbinkertn@umich.edu} 1852SN/A 18610292SAndreas.Sandberg@ARM.comtemplate<typename ...Args> std::string 18710292SAndreas.Sandberg@ARM.comcsprintf(const std::string &format, const Args &...args) 1884039Sbinkertn@umich.edu{ 18910292SAndreas.Sandberg@ARM.com return csprintf(format.c_str(), args...); 1902SN/A} 1912SN/A 1922SN/A#endif // __CPRINTF_HH__ 193