blocxx
DelayedFormat.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. 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 met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Quest Software, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38#ifndef BLOCXX_DELAYED_FORMAT_HPP
39#define BLOCXX_DELAYED_FORMAT_HPP
40#include "blocxx/BLOCXX_config.h"
41#include <iosfwd>
42#include "blocxx/Format.hpp"
43#include "blocxx/Array.hpp"
44#include "blocxx/String.hpp"
45#include "blocxx/Reference.hpp"
46
47namespace BLOCXX_NAMESPACE
48{
49 // These "internals" define a base class interface for the templated derived
50 // classes to provide. By doing this, we can create an array of these
51 // objects for simplicity of storage.
52 namespace DelayedFormatInternals
53 {
54 // Provide an interface for a virtual << operator.
55 class BLOCXX_COMMON_API DelayedFormatReferenceBase
56 {
57 protected:
59 public:
61 std::ostream& dumpToStream(std::ostream& o) const;
62 private:
63 virtual std::ostream& doDumpToStream(std::ostream& o) const = 0;
64 };
65 std::ostream& operator<<(std::ostream&, const DelayedFormatReferenceBase& s);
66
67
68 // The templated version which any non-const reference (that defines a <<
69 // operator for std::ostream) can be used.
70 template<class T>
72 {
73 public:
75 {
76 }
80 private:
81 // Not implemented.
84
85 // Dump it to the stream.
86 virtual std::ostream& doDumpToStream(std::ostream& o) const
87 {
88 return o << ref_;
89 }
90 T& ref_;
91 };
92 }
93
104 class BLOCXX_COMMON_API DelayedFormat
105 {
106 public:
114 DelayedFormat(const String& format);
115 template <typename A>
116 DelayedFormat(const String& format, A& a);
117 template <typename A, typename B>
118 DelayedFormat(const String& format, A& a, B& b);
119 template <typename A, typename B, typename C>
120 DelayedFormat(const String& format, A& a, B& b, C& c);
121 template <typename A, typename B, typename C, typename D>
122 DelayedFormat(const String& format, A& a, B& b, C& c, D& d);
123 template <typename A, typename B, typename C, typename D, typename E>
124 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e);
125 template <typename A, typename B, typename C, typename D, typename E, typename F>
126 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f);
127 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
128 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g);
129 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
130 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h);
131 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
132 DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i);
133
135 Format format() const;
136
138 operator String() const;
139
141 Format formatWithString(const String& fs) const;
142 Format formatWithString(const char* fs) const;
143 private:
145 template <class T>
146 void append(T& t);
147
153 };
154
155 template <typename T>
160
161 // Everything below here is just for the mess of constructors.
162 template <typename A>
164 : formatString(format), formatParameters()
165 {
166 formatParameters.reserve(1);
167 append(a);
168 }
169
170 template <typename A, typename B>
171 DelayedFormat::DelayedFormat(const String& format, A& a, B& b)
172 : formatString(format), formatParameters()
173 {
174 formatParameters.reserve(2);
175 append(a); append(b);
176 }
177
178 template <typename A, typename B, typename C>
179 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c)
180 : formatString(format), formatParameters()
181 {
182 formatParameters.reserve(3);
183 append(a); append(b); append(c);
184 }
185
186 template <typename A, typename B, typename C, typename D>
187 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d)
188 : formatString(format), formatParameters()
189 {
190 formatParameters.reserve(4);
191 append(a); append(b); append(c); append(d);
192 }
193
194 template <typename A, typename B, typename C, typename D, typename E>
195 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e)
196 : formatString(format), formatParameters()
197 {
198 formatParameters.reserve(5);
199 append(a); append(b); append(c); append(d); append(e);
200 }
201
202 template <typename A, typename B, typename C, typename D, typename E, typename F>
203 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f)
204 : formatString(format), formatParameters()
205 {
206 formatParameters.reserve(6);
207 append(a); append(b); append(c); append(d); append(e); append(f);
208 }
209
210 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
211 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g)
212 : formatString(format), formatParameters()
213 {
214 formatParameters.reserve(7);
215 append(a); append(b); append(c); append(d); append(e); append(f); append(g);
216 }
217
218 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
219 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h)
220 : formatString(format), formatParameters()
221 {
222 formatParameters.reserve(8);
223 append(a); append(b); append(c); append(d); append(e); append(f); append(g); append(h);
224 }
225
226 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
227 DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i)
228 : formatString(format), formatParameters()
229 {
230 formatParameters.reserve(9);
231 append(a); append(b); append(c); append(d); append(e); append(f); append(g); append(h); append(i);
232 }
233} // end namespace BLOCXX_NAMESPACE
234
235#endif
#define F(x, y, z)
Definition MD5.cpp:209
#define I(x, y, z)
Definition MD5.cpp:212
#define G(x, y, z)
Definition MD5.cpp:210
#define H(x, y, z)
Definition MD5.cpp:211
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
Definition ArrayFwd.hpp:46
This is a class similar to the blocxx format, except that all arguments are bound by reference and th...
void append(T &t)
This is a helper function to keep the various constructors simple.
DelayedFormat(const String &format)
This constructor allows for simple text output without non-format arguments.
Reference< DelayedFormatInternals::DelayedFormatReferenceBase > paramEntry
Array< paramEntry > formatParameters
The parameters that will be passed to format.
String formatString
The format string which will be passed to Format() when formatting this object.
virtual std::ostream & doDumpToStream(std::ostream &o) const =0
DelayedFormatReference & operator=(const DelayedFormatReference &)
virtual std::ostream & doDumpToStream(std::ostream &o) const
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
std::ostream & operator<<(std::ostream &o, const DelayedFormatReferenceBase &s)
Taken from RFC 1321.