blocxx
Format.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, 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* Vintela, 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_FORMAT_HPP
39#define BLOCXX_FORMAT_HPP
40#include "blocxx/BLOCXX_config.h"
41#include <iosfwd>
43#include "blocxx/String.hpp"
44
45namespace BLOCXX_NAMESPACE
46{
47
48// Format class declaration -----------------------------------------------//
49class BLOCXX_COMMON_API Format
50{
51public:
52
53 operator String() const;
54 String toString() const;
55 const char* c_str() const;
56 // generic templated constructors
57 template<typename A>
58 Format(const char* ca, const A& a);
59 template<typename A, typename B>
60 Format(const char* ca, const A& a, const B& b);
61 template<typename A, typename B, typename C>
62 Format(const char* ca, const A& a, const B& b, const C& c);
63 template<typename A, typename B, typename C, typename D>
64 Format(const char* ca, const A& a, const B& b, const C& c, const D& d);
65 template<typename A, typename B, typename C, typename D, typename E>
66 Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e);
67 template<typename A, typename B, typename C, typename D, typename E, typename F>
68 Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f);
69 template<typename A, typename B, typename C, typename D, typename E, typename F, typename G>
70 Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g);
71 template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
72 Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h);
73 template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
74 Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h, const I& i);
75 // These specific versions are to help prevent template bloat
76 Format(const char* ca, const String& a);
77 Format(const char* ca, const String& a, const String& b);
78 Format(const char* ca, const String& a, const String& b, const String& c);
79private:
81 char process(String& f, char c0);
82 template<typename T> void put(const T& t);
83 // These are to help prevent template bloat
84 void put (const String& t);
85 void put (char t);
86 void put (unsigned char t);
87 void put (short t);
88 void put (unsigned short t);
89 void put (int t);
90 void put (unsigned int t);
91 void put (long t);
92 void put (unsigned long t);
93 void put (long long t);
94 void put (unsigned long long t);
95public:
96 friend BLOCXX_COMMON_API std::ostream& operator<<(std::ostream& os, const Format& f);
97}; // class Format
98
99template<typename T>
100void Format::put(const T& t)
101{ // t is inserted into oss
102 if (!oss.good())
103 return;
104 oss << t;
105}
106
107template<typename A>
108Format::Format(const char* ca, const A& a) : oss()
109{
110 String fmt(ca);
111 while (!fmt.empty())
112 {
113 switch (process(fmt, '1'))
114 {
115 case '1': put(a); break;
116 }
117 }
118}
119template<typename A, typename B>
120Format::Format(const char* ca, const A& a, const B& b) : oss()
121{
122 String fmt(ca);
123 while (!fmt.empty())
124 {
125 switch (process(fmt, '2'))
126 {
127 case '1': put(a); break;
128 case '2': put(b); break;
129 }
130 }
131}
132template<typename A, typename B, typename C>
133Format::Format(const char* ca, const A& a, const B& b, const C& c) : oss()
134{
135 String fmt(ca);
136 while (!fmt.empty())
137 {
138 switch (process(fmt, '3'))
139 {
140 case '1': put(a); break;
141 case '2': put(b); break;
142 case '3': put(c); break;
143 }
144 }
145}
146template<typename A, typename B, typename C, typename D>
147Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d) : oss()
148{
149 String fmt(ca);
150 while (!fmt.empty())
151 {
152 switch (process(fmt, '4'))
153 {
154 case '1': put(a); break;
155 case '2': put(b); break;
156 case '3': put(c); break;
157 case '4': put(d); break;
158 }
159 }
160}
161template<typename A, typename B, typename C, typename D, typename E>
162Format:: Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e) : oss()
163{
164 String fmt(ca);
165 while (!fmt.empty())
166 {
167 switch (process(fmt, '5'))
168 {
169 case '1': put(a); break;
170 case '2': put(b); break;
171 case '3': put(c); break;
172 case '4': put(d); break;
173 case '5': put(e); break;
174 }
175 }
176}
177template<typename A, typename B, typename C, typename D, typename E, typename F>
178Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f) : oss()
179{
180 String fmt(ca);
181 while (!fmt.empty())
182 {
183 switch (process(fmt, '6'))
184 {
185 case '1': put(a); break;
186 case '2': put(b); break;
187 case '3': put(c); break;
188 case '4': put(d); break;
189 case '5': put(e); break;
190 case '6': put(f); break;
191 }
192 }
193}
194template<typename A, typename B, typename C, typename D, typename E, typename F, typename G>
195Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g) : oss()
196{
197 String fmt(ca);
198 while (!fmt.empty())
199 {
200 switch (process(fmt, '7'))
201 {
202 case '1': put(a); break;
203 case '2': put(b); break;
204 case '3': put(c); break;
205 case '4': put(d); break;
206 case '5': put(e); break;
207 case '6': put(f); break;
208 case '7': put(g); break;
209 }
210 }
211}
212template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
213Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h) : oss()
214{
215 String fmt(ca);
216 while (!fmt.empty())
217 {
218 switch (process(fmt, '8'))
219 {
220 case '1': put(a); break;
221 case '2': put(b); break;
222 case '3': put(c); break;
223 case '4': put(d); break;
224 case '5': put(e); break;
225 case '6': put(f); break;
226 case '7': put(g); break;
227 case '8': put(h); break;
228 }
229 }
230}
231template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
232Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h, const I& i) : oss()
233{
234 String fmt(ca);
235 while (!fmt.empty())
236 {
237 switch (process(fmt, '9'))
238 {
239 case '1': put(a); break;
240 case '2': put(b); break;
241 case '3': put(c); break;
242 case '4': put(d); break;
243 case '5': put(e); break;
244 case '6': put(f); break;
245 case '7': put(g); break;
246 case '8': put(h); break;
247 case '9': put(i); break;
248 }
249 }
250}
251
252} // end namespace BLOCXX_NAMESPACE
253
254#endif
255
#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
OStringStream oss
Definition Format.hpp:80
char process(String &f, char c0)
Definition Format.cpp:60
void put(const T &t)
Definition Format.hpp:100
Format(const char *ca, const A &a)
Definition Format.hpp:108
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
Taken from RFC 1321.
ostream & operator<<(ostream &ostrm, const Bool &arg)
Definition Bool.cpp:77