2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
32 #ifndef __CPRINTF_HH__
33 #define __CPRINTF_HH__
39 #include "base/cprintf_formats.hh"
50 virtual void process(std::ostream &out, Format &fmt) = 0;
54 class Node : public Base
60 Node(const T &d) : data(d) {}
61 virtual void process(std::ostream &out, Format &fmt) {
63 case Format::character:
64 format_char(out, data, fmt);
68 format_integer(out, data, fmt);
71 case Format::floating:
72 format_float(out, data, fmt);
76 format_string(out, data, fmt);
80 out << "<bad format>";
86 typedef std::list<Base *> list_t;
93 ArgList() : stream(&std::cout) {}
97 void append(const T &data) {
98 Base *obj = new ArgList::Node<T>(data);
99 objects.push_back(obj);
103 void prepend(const T &data) {
104 Base *obj = new ArgList::Node<T>(data);
105 objects.push_front(obj);
108 void dump(const std::string &format);
109 void dump(std::ostream &strm, const std::string &fmt)
110 { stream = &strm; dump(fmt); }
112 std::string dumpToString(const std::string &format);
114 friend ArgList &operator<<(std::ostream &str, ArgList &list);
119 operator,(ArgList &alist, const T &data)
129 operator,(ArgList &alist, ArgListNull)
133 // cprintf(format, args, ...) prints to cout
134 // (analogous to printf())
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())
145 // ccprintf(stream, format, args, ...) prints to the specified stream
146 // (analogous to fprintf())
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())
157 // csprintf(format, args, ...) returns a string
158 // (roughly analogous to sprintf())
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())
170 #endif // __CPRINTF_HH__