cprintf.hh revision 492
111723Sar4jc@virginia.edu/* 211723Sar4jc@virginia.edu * Copyright (c) 2003 The Regents of The University of Michigan 311723Sar4jc@virginia.edu * All rights reserved. 411723Sar4jc@virginia.edu * 511723Sar4jc@virginia.edu * Redistribution and use in source and binary forms, with or without 611723Sar4jc@virginia.edu * modification, are permitted provided that the following conditions are 711723Sar4jc@virginia.edu * met: redistributions of source code must retain the above copyright 811723Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer; 911723Sar4jc@virginia.edu * redistributions in binary form must reproduce the above copyright 1011723Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer in the 1111723Sar4jc@virginia.edu * documentation and/or other materials provided with the distribution; 1211723Sar4jc@virginia.edu * neither the name of the copyright holders nor the names of its 1311723Sar4jc@virginia.edu * contributors may be used to endorse or promote products derived from 1411723Sar4jc@virginia.edu * this software without specific prior written permission. 1511723Sar4jc@virginia.edu * 1611723Sar4jc@virginia.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1711723Sar4jc@virginia.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1811723Sar4jc@virginia.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1911723Sar4jc@virginia.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2011723Sar4jc@virginia.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2111723Sar4jc@virginia.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2211723Sar4jc@virginia.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2311723Sar4jc@virginia.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2411723Sar4jc@virginia.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2511723Sar4jc@virginia.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2611723Sar4jc@virginia.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2711723Sar4jc@virginia.edu */ 2811723Sar4jc@virginia.edu 2911723Sar4jc@virginia.edu#ifndef __CPRINTF_HH__ 3011723Sar4jc@virginia.edu#define __CPRINTF_HH__ 3111723Sar4jc@virginia.edu 3211723Sar4jc@virginia.edu#include <iostream> 3311723Sar4jc@virginia.edu#include <list> 3411723Sar4jc@virginia.edu#include <sstream> 3511723Sar4jc@virginia.edu#include <string> 3611723Sar4jc@virginia.edu 3711723Sar4jc@virginia.edunamespace cp { 3811723Sar4jc@virginia.edu 3911723Sar4jc@virginia.edu#include "base/cprintf_formats.hh" 4011723Sar4jc@virginia.edu 4111723Sar4jc@virginia.educlass ArgList 4211723Sar4jc@virginia.edu{ 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 115template<class T> 116inline ArgList & 117operator,(ArgList &alist, const T &data) 118{ 119 alist.append(data); 120 return alist; 121} 122 123class ArgListNull { 124}; 125 126inline ArgList & 127operator,(ArgList &alist, ArgListNull) 128{ return alist; } 129 130// 131// cprintf(format, args, ...) prints to cout 132// (analogous to printf()) 133// 134inline 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// 146inline 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// 158inline 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 166template<class T> 167inline ArgList & 168operator<<(ArgList &list, const T &data) 169{ 170 list.append(data); 171 return list; 172} 173 174inline ArgList & 175operator<<(std::ostream &str, ArgList &list) 176{ 177 list.stream = &str; 178 return list; 179} 180 181class 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__ 199