00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00038 #ifndef BLOCXX_DELAYED_FORMAT_HPP
00039 #define BLOCXX_DELAYED_FORMAT_HPP
00040 #include "blocxx/BLOCXX_config.h"
00041 #include <iosfwd>
00042 #include "blocxx/Format.hpp"
00043 #include "blocxx/Array.hpp"
00044 #include "blocxx/String.hpp"
00045 #include "blocxx/Reference.hpp"
00046
00047 namespace BLOCXX_NAMESPACE
00048 {
00049
00050
00051
00052 namespace DelayedFormatInternals
00053 {
00054
00055 class BLOCXX_COMMON_API DelayedFormatReferenceBase
00056 {
00057 protected:
00058 DelayedFormatReferenceBase();
00059 public:
00060 virtual ~DelayedFormatReferenceBase();
00061 std::ostream& dumpToStream(std::ostream& o) const;
00062 private:
00063 virtual std::ostream& doDumpToStream(std::ostream& o) const = 0;
00064 };
00065 std::ostream& operator<<(std::ostream&, const DelayedFormatReferenceBase& s);
00066
00067
00068
00069
00070 template<class T>
00071 class DelayedFormatReference : public DelayedFormatReferenceBase
00072 {
00073 public:
00074 DelayedFormatReference(T& t): ref_(t)
00075 {
00076 }
00077 ~DelayedFormatReference()
00078 {
00079 }
00080 private:
00081
00082 DelayedFormatReference(const DelayedFormatReference&);
00083 DelayedFormatReference& operator=(const DelayedFormatReference&);
00084
00085
00086 virtual std::ostream& doDumpToStream(std::ostream& o) const
00087 {
00088 return o << ref_;
00089 }
00090 T& ref_;
00091 };
00092 }
00093
00104 class BLOCXX_COMMON_API DelayedFormat
00105 {
00106 public:
00114 DelayedFormat(const String& format);
00115 template <typename A>
00116 DelayedFormat(const String& format, A& a);
00117 template <typename A, typename B>
00118 DelayedFormat(const String& format, A& a, B& b);
00119 template <typename A, typename B, typename C>
00120 DelayedFormat(const String& format, A& a, B& b, C& c);
00121 template <typename A, typename B, typename C, typename D>
00122 DelayedFormat(const String& format, A& a, B& b, C& c, D& d);
00123 template <typename A, typename B, typename C, typename D, typename E>
00124 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e);
00125 template <typename A, typename B, typename C, typename D, typename E, typename F>
00126 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f);
00127 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
00128 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g);
00129 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
00130 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h);
00131 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
00132 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i);
00133
00135 Format format() const;
00136
00138 operator String() const;
00139
00141 Format formatWithString(const String& fs) const;
00142 Format formatWithString(const char* fs) const;
00143 private:
00145 template <class T>
00146 void append(T& t);
00147
00149 String formatString;
00150 typedef Reference<DelayedFormatInternals::DelayedFormatReferenceBase> paramEntry;
00152 Array<paramEntry> formatParameters;
00153 };
00154
00155 template <typename T>
00156 void DelayedFormat::append(T& t)
00157 {
00158 formatParameters.append(paramEntry(new DelayedFormatInternals::DelayedFormatReference<T>(t)));
00159 }
00160
00161
00162 template <typename A>
00163 DelayedFormat::DelayedFormat(const String& format, A& a)
00164 : formatString(format), formatParameters()
00165 {
00166 formatParameters.reserve(1);
00167 append(a);
00168 }
00169
00170 template <typename A, typename B>
00171 DelayedFormat::DelayedFormat(const String& format, A& a, B& b)
00172 : formatString(format), formatParameters()
00173 {
00174 formatParameters.reserve(2);
00175 append(a); append(b);
00176 }
00177
00178 template <typename A, typename B, typename C>
00179 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c)
00180 : formatString(format), formatParameters()
00181 {
00182 formatParameters.reserve(3);
00183 append(a); append(b); append(c);
00184 }
00185
00186 template <typename A, typename B, typename C, typename D>
00187 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d)
00188 : formatString(format), formatParameters()
00189 {
00190 formatParameters.reserve(4);
00191 append(a); append(b); append(c); append(d);
00192 }
00193
00194 template <typename A, typename B, typename C, typename D, typename E>
00195 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e)
00196 : formatString(format), formatParameters()
00197 {
00198 formatParameters.reserve(5);
00199 append(a); append(b); append(c); append(d); append(e);
00200 }
00201
00202 template <typename A, typename B, typename C, typename D, typename E, typename F>
00203 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f)
00204 : formatString(format), formatParameters()
00205 {
00206 formatParameters.reserve(6);
00207 append(a); append(b); append(c); append(d); append(e); append(f);
00208 }
00209
00210 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
00211 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g)
00212 : formatString(format), formatParameters()
00213 {
00214 formatParameters.reserve(7);
00215 append(a); append(b); append(c); append(d); append(e); append(f); append(g);
00216 }
00217
00218 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
00219 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h)
00220 : formatString(format), formatParameters()
00221 {
00222 formatParameters.reserve(8);
00223 append(a); append(b); append(c); append(d); append(e); append(f); append(g); append(h);
00224 }
00225
00226 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
00227 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i)
00228 : formatString(format), formatParameters()
00229 {
00230 formatParameters.reserve(9);
00231 append(a); append(b); append(c); append(d); append(e); append(f); append(g); append(h); append(i);
00232 }
00233 }
00234
00235 #endif