2c2
< * Copyright (c) 2002-2005 The Regents of The University of Michigan
---
> * Copyright (c) 2002-2006 The Regents of The University of Michigan
32,33c32,33
< #ifndef __CPRINTF_HH__
< #define __CPRINTF_HH__
---
> #ifndef __BASE_CPRINTF_HH__
> #define __BASE_CPRINTF_HH__
34a35
> #include <ios>
38a40
> #include "base/varargs.hh"
43c45,48
< class ArgList
---
> #define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print)
> #define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print)
>
> struct Print
45,51c50,53
< private:
< class Base
< {
< public:
< virtual ~Base() {}
< virtual void process(std::ostream &out, Format &fmt) = 0;
< };
---
> protected:
> std::ostream &stream;
> const char *format;
> const char *ptr;
53,57c55,57
< template <typename T>
< class Node : public Base
< {
< public:
< const T &data;
---
> std::ios::fmtflags saved_flags;
> char saved_fill;
> int saved_precision;
59,65c59
< public:
< Node(const T &d) : data(d) {}
< virtual void process(std::ostream &out, Format &fmt) {
< switch (fmt.format) {
< case Format::character:
< format_char(out, data, fmt);
< break;
---
> void process(Format &fmt);
67,69c61,64
< case Format::integer:
< format_integer(out, data, fmt);
< break;
---
> public:
> Print(std::ostream &stream, const std::string &format);
> Print(std::ostream &stream, const char *format);
> ~Print();
71,73c66,71
< case Format::floating:
< format_float(out, data, fmt);
< break;
---
> template <typename T>
> void
> add_arg(const T &data)
> {
> Format fmt;
> process(fmt);
75,77c73,76
< case Format::string:
< format_string(out, data, fmt);
< break;
---
> switch (fmt.format) {
> case Format::character:
> format_char(stream, data, fmt);
> break;
79,84c78,80
< default:
< out << "<bad format>";
< break;
< }
< }
< };
---
> case Format::integer:
> format_integer(stream, data, fmt);
> break;
86c82,84
< typedef std::list<Base *> list_t;
---
> case Format::floating:
> format_float(stream, data, fmt);
> break;
88,90c86,88
< protected:
< list_t objects;
< std::ostream *stream;
---
> case Format::string:
> format_string(stream, data, fmt);
> break;
92,99c90,93
< public:
< ArgList() : stream(&std::cout) {}
< ~ArgList();
<
< template<class T>
< void append(const T &data) {
< Base *obj = new ArgList::Node<T>(data);
< objects.push_back(obj);
---
> default:
> stream << "<bad format>";
> break;
> }
102,106c96,97
< template<class T>
< void prepend(const T &data) {
< Base *obj = new ArgList::Node<T>(data);
< objects.push_front(obj);
< }
---
> void end_args();
> };
108,110c99
< void dump(const std::string &format);
< void dump(std::ostream &strm, const std::string &fmt)
< { stream = &strm; dump(fmt); }
---
> /* end namespace cp */ }
112c101
< std::string dumpToString(const std::string &format);
---
> typedef VarArgs::List<cp::Print> CPrintfArgsList;
114,115c103,108
< friend ArgList &operator<<(std::ostream &str, ArgList &list);
< };
---
> inline void
> ccprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args)
> {
> cp::Print print(stream, format);
> args.add_args(print);
> }
117,119c110,111
< template<class T>
< inline ArgList &
< operator,(ArgList &alist, const T &data)
---
> inline void
> ccprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION)
121,122c113,114
< alist.append(data);
< return alist;
---
> cp::Print print(stream, format);
> VARARGS_ADDARGS(print);
125,126c117,121
< class ArgListNull {
< };
---
> inline void
> cprintf(const char *format, CPRINTF_DECLARATION)
> {
> ccprintf(std::cout, format, VARARGS_ALLARGS);
> }
128,130c123,129
< inline ArgList &
< operator,(ArgList &alist, ArgListNull)
< { return alist; }
---
> inline std::string
> csprintf(const char *format, CPRINTF_DECLARATION)
> {
> std::stringstream stream;
> ccprintf(stream, format, VARARGS_ALLARGS);
> return stream.str();
> }
132,135c131,135
< //
< // cprintf(format, args, ...) prints to cout
< // (analogous to printf())
< //
---
> /*
> * functions again with std::string. We have both so we don't waste
> * time converting const char * to std::string since we don't take
> * advantage of it.
> */
137,142c137,141
< __cprintf(const std::string &format, ArgList &args)
< { args.dump(format); delete &args; }
< #define __cprintf__(format, ...) \
< cp::__cprintf(format, (*(new cp::ArgList), __VA_ARGS__))
< #define cprintf(...) \
< __cprintf__(__VA_ARGS__, cp::ArgListNull())
---
> ccprintf(std::ostream &stream, const std::string &format,
> const CPrintfArgsList &args)
> {
> ccprintf(stream, format.c_str(), args);
> }
144,147d142
< //
< // ccprintf(stream, format, args, ...) prints to the specified stream
< // (analogous to fprintf())
< //
149,154c144,147
< __ccprintf(std::ostream &stream, const std::string &format, ArgList &args)
< { args.dump(stream, format); delete &args; }
< #define __ccprintf__(stream, format, ...) \
< cp::__ccprintf(stream, format, (*(new cp::ArgList), __VA_ARGS__))
< #define ccprintf(stream, ...) \
< __ccprintf__(stream, __VA_ARGS__, cp::ArgListNull())
---
> ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION)
> {
> ccprintf(stream, format, VARARGS_ALLARGS);
> }
156,166c149,153
< //
< // csprintf(format, args, ...) returns a string
< // (roughly analogous to sprintf())
< //
< inline std::string
< __csprintf(const std::string &format, ArgList &args)
< { std::string s = args.dumpToString(format); delete &args; return s; }
< #define __csprintf__(format, ...) \
< cp::__csprintf(format, (*(new cp::ArgList), __VA_ARGS__))
< #define csprintf(...) \
< __csprintf__(__VA_ARGS__, cp::ArgListNull())
---
> inline void
> cprintf(const std::string &format, CPRINTF_DECLARATION)
> {
> ccprintf(std::cout, format, VARARGS_ALLARGS);
> }
167a155,160
> inline std::string
> csprintf(const std::string &format, CPRINTF_DECLARATION)
> {
> std::stringstream stream;
> ccprintf(stream, format, VARARGS_ALLARGS);
> return stream.str();