1/*
| 1/*
|
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
| 2 * Copyright (c) 2002-2006 The Regents of The University of Michigan
|
3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 * Steve Reinhardt 30 */ 31
| 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 * Steve Reinhardt 30 */ 31
|
32#ifndef __CPRINTF_HH__ 33#define __CPRINTF_HH__
| 32#ifndef __BASE_CPRINTF_HH__ 33#define __BASE_CPRINTF_HH__
|
34
| 34
|
| 35#include <ios>
|
35#include <iostream> 36#include <list> 37#include <string> 38
| 36#include <iostream> 37#include <list> 38#include <string> 39
|
| 40#include "base/varargs.hh"
|
39#include "base/cprintf_formats.hh" 40 41namespace cp { 42
| 41#include "base/cprintf_formats.hh" 42 43namespace cp { 44
|
43class ArgList
| 45#define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print) 46#define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print) 47 48struct Print
|
44{
| 49{
|
45 private: 46 class Base 47 { 48 public: 49 virtual ~Base() {} 50 virtual void process(std::ostream &out, Format &fmt) = 0; 51 };
| 50 protected: 51 std::ostream &stream; 52 const char *format; 53 const char *ptr;
|
52
| 54
|
53 template <typename T> 54 class Node : public Base 55 { 56 public: 57 const T &data;
| 55 std::ios::fmtflags saved_flags; 56 char saved_fill; 57 int saved_precision;
|
58
| 58
|
59 public: 60 Node(const T &d) : data(d) {} 61 virtual void process(std::ostream &out, Format &fmt) { 62 switch (fmt.format) { 63 case Format::character: 64 format_char(out, data, fmt); 65 break;
| 59 void process(Format &fmt);
|
66
| 60
|
67 case Format::integer: 68 format_integer(out, data, fmt); 69 break;
| 61 public: 62 Print(std::ostream &stream, const std::string &format); 63 Print(std::ostream &stream, const char *format); 64 ~Print();
|
70
| 65
|
71 case Format::floating: 72 format_float(out, data, fmt); 73 break;
| 66 template <typename T> 67 void 68 add_arg(const T &data) 69 { 70 Format fmt; 71 process(fmt);
|
74
| 72
|
75 case Format::string: 76 format_string(out, data, fmt); 77 break;
| 73 switch (fmt.format) { 74 case Format::character: 75 format_char(stream, data, fmt); 76 break;
|
78
| 77
|
79 default: 80 out << "<bad format>"; 81 break; 82 } 83 } 84 };
| 78 case Format::integer: 79 format_integer(stream, data, fmt); 80 break;
|
85
| 81
|
86 typedef std::list<Base *> list_t;
| 82 case Format::floating: 83 format_float(stream, data, fmt); 84 break;
|
87
| 85
|
88 protected: 89 list_t objects; 90 std::ostream *stream;
| 86 case Format::string: 87 format_string(stream, data, fmt); 88 break;
|
91
| 89
|
92 public: 93 ArgList() : stream(&std::cout) {} 94 ~ArgList(); 95 96 template<class T> 97 void append(const T &data) { 98 Base *obj = new ArgList::Node<T>(data); 99 objects.push_back(obj);
| 90 default: 91 stream << "<bad format>"; 92 break; 93 }
|
100 } 101
| 94 } 95
|
102 template<class T> 103 void prepend(const T &data) { 104 Base *obj = new ArgList::Node<T>(data); 105 objects.push_front(obj); 106 }
| 96 void end_args(); 97};
|
107
| 98
|
108 void dump(const std::string &format); 109 void dump(std::ostream &strm, const std::string &fmt) 110 { stream = &strm; dump(fmt); }
| 99/* end namespace cp */ }
|
111
| 100
|
112 std::string dumpToString(const std::string &format);
| 101typedef VarArgs::List<cp::Print> CPrintfArgsList;
|
113
| 102
|
114 friend ArgList &operator<<(std::ostream &str, ArgList &list); 115};
| 103inline void 104ccprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args) 105{ 106 cp::Print print(stream, format); 107 args.add_args(print); 108}
|
116
| 109
|
117template<class T> 118inline ArgList & 119operator,(ArgList &alist, const T &data)
| 110inline void 111ccprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION)
|
120{
| 112{
|
121 alist.append(data); 122 return alist;
| 113 cp::Print print(stream, format); 114 VARARGS_ADDARGS(print);
|
123} 124
| 115} 116
|
125class ArgListNull { 126};
| 117inline void 118cprintf(const char *format, CPRINTF_DECLARATION) 119{ 120 ccprintf(std::cout, format, VARARGS_ALLARGS); 121}
|
127
| 122
|
128inline ArgList & 129operator,(ArgList &alist, ArgListNull) 130{ return alist; }
| 123inline std::string 124csprintf(const char *format, CPRINTF_DECLARATION) 125{ 126 std::stringstream stream; 127 ccprintf(stream, format, VARARGS_ALLARGS); 128 return stream.str(); 129}
|
131
| 130
|
132// 133// cprintf(format, args, ...) prints to cout 134// (analogous to printf()) 135//
| 131/* 132 * functions again with std::string. We have both so we don't waste 133 * time converting const char * to std::string since we don't take 134 * advantage of it. 135 */
|
136inline void
| 136inline void
|
137__cprintf(const std::string &format, ArgList &args) 138{ args.dump(format); delete &args; } 139#define __cprintf__(format, ...) \ 140 cp::__cprintf(format, (*(new cp::ArgList), __VA_ARGS__)) 141#define cprintf(...) \ 142 __cprintf__(__VA_ARGS__, cp::ArgListNull())
| 137ccprintf(std::ostream &stream, const std::string &format, 138 const CPrintfArgsList &args) 139{ 140 ccprintf(stream, format.c_str(), args); 141}
|
143
| 142
|
144// 145// ccprintf(stream, format, args, ...) prints to the specified stream 146// (analogous to fprintf()) 147//
| |
148inline void
| 143inline void
|
149__ccprintf(std::ostream &stream, const std::string &format, ArgList &args) 150{ args.dump(stream, format); delete &args; } 151#define __ccprintf__(stream, format, ...) \ 152 cp::__ccprintf(stream, format, (*(new cp::ArgList), __VA_ARGS__)) 153#define ccprintf(stream, ...) \ 154 __ccprintf__(stream, __VA_ARGS__, cp::ArgListNull())
| 144ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION) 145{ 146 ccprintf(stream, format, VARARGS_ALLARGS); 147}
|
155
| 148
|
156// 157// csprintf(format, args, ...) returns a string 158// (roughly analogous to sprintf()) 159// 160inline std::string 161__csprintf(const std::string &format, ArgList &args) 162{ std::string s = args.dumpToString(format); delete &args; return s; } 163#define __csprintf__(format, ...) \ 164 cp::__csprintf(format, (*(new cp::ArgList), __VA_ARGS__)) 165#define csprintf(...) \ 166 __csprintf__(__VA_ARGS__, cp::ArgListNull())
| 149inline void 150cprintf(const std::string &format, CPRINTF_DECLARATION) 151{ 152 ccprintf(std::cout, format, VARARGS_ALLARGS); 153}
|
167
| 154
|
| 155inline std::string 156csprintf(const std::string &format, CPRINTF_DECLARATION) 157{ 158 std::stringstream stream; 159 ccprintf(stream, format, VARARGS_ALLARGS); 160 return stream.str();
|
168} 169 170#endif // __CPRINTF_HH__
| 161} 162 163#endif // __CPRINTF_HH__
|