Updated Copyright with information in bitkeeper changelogs
[gem5.git] / base / cprintf.hh
1 /*
2 * Copyright (c) 2002-2004 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
29 #ifndef __CPRINTF_HH__
30 #define __CPRINTF_HH__
31
32 #include <iostream>
33 #include <list>
34 #include <sstream>
35 #include <string>
36
37 namespace cp {
38
39 #include "base/cprintf_formats.hh"
40
41 class ArgList
42 {
43 private:
44 class Base
45 {
46 public:
47 virtual ~Base() {}
48 virtual void process(std::ostream &out, Format &fmt) = 0;
49 };
50
51 template <typename T>
52 class Node : public Base
53 {
54 public:
55 const T &data;
56
57 public:
58 Node(const T &d) : data(d) {}
59 virtual void process(std::ostream &out, Format &fmt) {
60 switch (fmt.format) {
61 case Format::character:
62 format_char(out, data, fmt);
63 break;
64
65 case Format::integer:
66 format_integer(out, data, fmt);
67 break;
68
69 case Format::floating:
70 format_float(out, data, fmt);
71 break;
72
73 case Format::string:
74 format_string(out, data, fmt);
75 break;
76
77 default:
78 out << "<bad format>";
79 break;
80 }
81 }
82 };
83
84 typedef std::list<Base *> list_t;
85
86 protected:
87 list_t objects;
88 std::ostream *stream;
89
90 public:
91 ArgList() : stream(&std::cout) {}
92 ~ArgList();
93
94 template<class T>
95 void append(const T &data) {
96 Base *obj = new ArgList::Node<T>(data);
97 objects.push_back(obj);
98 }
99
100 template<class T>
101 void prepend(const T &data) {
102 Base *obj = new ArgList::Node<T>(data);
103 objects.push_front(obj);
104 }
105
106 void dump(const std::string &format);
107 void dump(std::ostream &strm, const std::string &fmt)
108 { stream = &strm; dump(fmt); }
109
110 std::string dumpToString(const std::string &format);
111
112 friend ArgList &operator<<(std::ostream &str, ArgList &list);
113 };
114
115 template<class T>
116 inline ArgList &
117 operator,(ArgList &alist, const T &data)
118 {
119 alist.append(data);
120 return alist;
121 }
122
123 class ArgListNull {
124 };
125
126 inline ArgList &
127 operator,(ArgList &alist, ArgListNull)
128 { return alist; }
129
130 //
131 // cprintf(format, args, ...) prints to cout
132 // (analogous to printf())
133 //
134 inline void
135 __cprintf(const std::string &format, ArgList &args)
136 { args.dump(format); delete &args; }
137 #define __cprintf__(format, args...) \
138 cp::__cprintf(format, (*(new cp::ArgList), args))
139 #define cprintf(args...) \
140 __cprintf__(args, cp::ArgListNull())
141
142 //
143 // ccprintf(stream, format, args, ...) prints to the specified stream
144 // (analogous to fprintf())
145 //
146 inline void
147 __ccprintf(std::ostream &stream, const std::string &format, ArgList &args)
148 { args.dump(stream, format); delete &args; }
149 #define __ccprintf__(stream, format, args...) \
150 cp::__ccprintf(stream, format, (*(new cp::ArgList), args))
151 #define ccprintf(stream, args...) \
152 __ccprintf__(stream, args, cp::ArgListNull())
153
154 //
155 // csprintf(format, args, ...) returns a string
156 // (roughly analogous to sprintf())
157 //
158 inline std::string
159 __csprintf(const std::string &format, ArgList &args)
160 { std::string s = args.dumpToString(format); delete &args; return s; }
161 #define __csprintf__(format, args...) \
162 cp::__csprintf(format, (*(new cp::ArgList), args))
163 #define csprintf(args...) \
164 __csprintf__(args, cp::ArgListNull())
165
166 template<class T>
167 inline ArgList &
168 operator<<(ArgList &list, const T &data)
169 {
170 list.append(data);
171 return list;
172 }
173
174 inline ArgList &
175 operator<<(std::ostream &str, ArgList &list)
176 {
177 list.stream = &str;
178 return list;
179 }
180
181 class ArgListTemp
182 {
183 private:
184 std::string format;
185 ArgList *args;
186
187 public:
188 ArgListTemp(const std::string &f) : format(f) { args = new ArgList; }
189 ~ArgListTemp() { args->dump(format); delete args; }
190
191 operator ArgList *() { return args; }
192 };
193
194 #define cformat(format) \
195 (*((cp::ArgList *)cp::ArgListTemp(format)))
196 }
197
198 #endif // __CPRINTF_HH__