Ice 3.7 C++98 API Reference
Loading...
Searching...
No Matches
OutputUtil.h
Go to the documentation of this file.
1//
2// Copyright (c) ZeroC, Inc. All rights reserved.
3//
4
5#ifndef ICE_UTIL_OUTPUT_UTIL_H
6#define ICE_UTIL_OUTPUT_UTIL_H
7
8#include <IceUtil/Config.h>
9#include <fstream>
10#include <stack>
11#include <vector>
12
13namespace IceUtilInternal
14{
15
16ICE_API std::string int64ToString(IceUtil::Int64);
17
18// ----------------------------------------------------------------------
19// OutputBase
20// ----------------------------------------------------------------------
21
22//
23// Technically it's not necessary to have print() & newline() as virtual
24// since the opeator<< functions are specific to each OutputBase
25// derivative. However, since it's possible to call print() & newline()
26// manually I've decided to leave them as virtual.
27//
28
29class ICE_API OutputBase : private ::IceUtil::noncopyable
30{
31public:
32
33 OutputBase();
34 OutputBase(std::ostream&);
35 OutputBase(const std::string&);
36 virtual ~OutputBase();
37
38 void setIndent(int); // What is the indent level?
39 void setUseTab(bool); // Should we output tabs?
40
41 void open(const std::string&); // Open output stream.
42 void close(); // Close output stream.
43 bool isOpen(); // Is a file stream open?
44
45 virtual void print(const std::string&); // Print a string.
46
47 void inc(); // Increment indentation level.
48 void dec(); // Decrement indentation level.
49
50 void useCurrentPosAsIndent(); // Save the current position as indentation.
51 void zeroIndent(); // Use zero identation.
52 void restoreIndent(); // Restore indentation.
53 int currIndent(); // Return current indent value.
54
55 virtual void newline(); // Print newline.
56 void separator(); // Print separator.
57
58 bool operator!() const; // Check whether the output state is ok.
59
60protected:
61
62 std::ofstream _fout;
63 std::ostream& _out;
64 int _pos;
65 int _indent;
66 int _indentSize;
67 std::stack<int> _indentSave;
68 bool _useTab;
69 bool _separator;
70};
71
72class ICE_API NextLine
73{
74};
75extern ICE_API NextLine nl ICE_GLOBAL_VAR_SUFFIX;
76
77class ICE_API Separator
78{
79};
80extern ICE_API Separator sp ICE_GLOBAL_VAR_SUFFIX;
81
82// ----------------------------------------------------------------------
83// Output
84// ----------------------------------------------------------------------
85
86class ICE_API Output : public OutputBase
87{
88public:
89
90 Output(bool breakBeforeBlock = true, bool shortEmptyBlock = false);
91 Output(std::ostream&, bool = true, bool = false);
92 Output(const char*, bool = true, bool = false);
93
94 virtual void print(const std::string&); // Print a string.
95
96 void sb(); // Start a block.
97 void eb(); // End a block.
98
99 void spar(char = '('); // Start a paramater list.
100 void epar(char = ')'); // End a paramater list.
101
102private:
103
104 std::string _blockStart;
105 std::string _blockEnd;
106 int _par; // If >= 0, we are writing a parameter list.
107 const bool _breakBeforeBlock; // If true break before starting a new block.
108 const bool _shortEmptyBlock; // If true, an empty block is written <sb><eb>.
109 bool _emptyBlock;
110};
111
112template<typename T>
113inline Output&
114operator<<(Output& out, const T& val)
115{
116 std::ostringstream s;
117 s << val;
118 out.print(s.str());
119 return out;
120}
121
122template<typename T>
123inline Output&
124operator<<(Output& out, const std::vector<T>& val)
125{
126 for(typename std::vector<T>::const_iterator p = val.begin(); p != val.end(); ++p)
127 {
128 out << *p;
129 }
130 return out;
131}
132
133template<>
134inline Output&
135operator<<(Output& o, const NextLine&)
136{
137 o.newline();
138 return o;
139}
140
141template<>
142inline Output&
143operator<<(Output& o, const Separator&)
144{
145 o.separator();
146 return o;
147}
148
149class ICE_API StartBlock
150{
151};
152extern ICE_API StartBlock sb ICE_GLOBAL_VAR_SUFFIX;
153
154template<>
155inline Output&
156operator<<(Output& o, const StartBlock&)
157{
158 o.sb();
159 return o;
160}
161
162class ICE_API EndBlock
163{
164};
165extern ICE_API EndBlock eb ICE_GLOBAL_VAR_SUFFIX;
166
167template<>
168inline Output&
169operator<<(Output& o, const EndBlock&)
170{
171 o.eb();
172 return o;
173}
174
175class ICE_API StartPar
176{
177};
178extern ICE_API StartPar spar ICE_GLOBAL_VAR_SUFFIX;
179
180template<>
181inline Output&
182operator<<(Output& o, const StartPar&)
183{
184 o.spar();
185 return o;
186}
187
188class ICE_API EndPar
189{
190};
191extern ICE_API EndPar epar ICE_GLOBAL_VAR_SUFFIX;
192
193template<>
194inline Output&
195operator<<(Output& o, const EndPar&)
196{
197 o.epar();
198 return o;
199}
200
201class ICE_API StartAbrk
202{
203};
204extern ICE_API StartAbrk sabrk ICE_GLOBAL_VAR_SUFFIX;
205
206template<>
207inline Output&
208operator<<(Output& o, const StartAbrk&)
209{
210 o.spar('<');
211 return o;
212}
213
214class ICE_API EndAbrk
215{
216};
217extern ICE_API EndAbrk eabrk ICE_GLOBAL_VAR_SUFFIX;
218
219template<>
220inline Output&
221operator<<(Output& o, const EndAbrk&)
222{
223 o.epar('>');
224 return o;
225}
226
227ICE_API Output& operator<<(Output&, std::ios_base& (*)(std::ios_base&));
228
229// ----------------------------------------------------------------------
230// XMLOutput
231// ----------------------------------------------------------------------
232
233class ICE_API XMLOutput : public OutputBase
234{
235public:
236
237 XMLOutput();
238 XMLOutput(std::ostream&);
239 XMLOutput(const char*);
240
241 virtual void print(const std::string&); // Print a string.
242
243 virtual void newline(); // Print newline.
244
245 void startElement(const std::string&); // Start an element.
246 void endElement(); // End an element.
247 void attr(const std::string&, const std::string&); // Add an attribute to an element.
248
249 void startEscapes();
250 void endEscapes();
251
252 std::string currentElement() const;
253
254private:
255
256 ::std::string escape(const ::std::string&) const;
257
258 std::stack<std::string> _elementStack;
259
260 bool _se;
261 bool _text;
262
263 bool _escape;
264};
265
266template<typename T>
267inline XMLOutput&
268operator<<(XMLOutput& out, const T& val)
269{
270 std::ostringstream s;
271 s << val;
272 out.print(s.str());
273 return out;
274}
275
276template<>
277inline XMLOutput&
278operator<<(XMLOutput& o, const NextLine&)
279{
280 o.newline();
281 return o;
282}
283
284template<>
285inline XMLOutput&
286operator<<(XMLOutput& o, const Separator&)
287{
288 o.separator();
289 return o;
290}
291
292class ICE_API EndElement
293{
294};
295extern ICE_API EndElement ee ICE_GLOBAL_VAR_SUFFIX;
296
297template<>
298inline XMLOutput&
299operator<<(XMLOutput& o, const EndElement&)
300{
301 o.endElement();
302 return o;
303}
304
305class ICE_API StartElement
306{
307public:
308
309 StartElement(const std::string&);
310
311 const std::string& getName() const;
312
313private:
314
315 const std::string _name;
316};
317
318typedef StartElement se;
319
320template<>
321inline XMLOutput&
322operator<<(XMLOutput& o, const StartElement& e)
323{
324 o.startElement(e.getName());
325 return o;
326}
327
328class ICE_API Attribute
329{
330public:
331
332 Attribute(const ::std::string&, const ::std::string&);
333
334 const ::std::string& getName() const;
335 const ::std::string& getValue() const;
336
337private:
338
339 const ::std::string _name;
340 const ::std::string _value;
341};
342
343typedef Attribute attr;
344
345template<>
346inline XMLOutput&
347operator<<(XMLOutput& o, const Attribute& e)
348{
349 o.attr(e.getName(), e.getValue());
350 return o;
351}
352
353class ICE_API StartEscapes
354{
355};
356extern ICE_API StartEscapes startEscapes ICE_GLOBAL_VAR_SUFFIX;
357
358class ICE_API EndEscapes
359{
360};
361extern ICE_API EndEscapes endEscapes ICE_GLOBAL_VAR_SUFFIX;
362
363template<>
364inline XMLOutput&
365operator<<(XMLOutput& o, const StartEscapes&)
366{
367 o.startEscapes();
368 return o;
369}
370
371template<>
372inline XMLOutput&
373operator<<(XMLOutput& o, const EndEscapes&)
374{
375 o.endEscapes();
376 return o;
377}
378
379ICE_API XMLOutput& operator<<(XMLOutput&, std::ios_base& (*)(std::ios_base&));
380
381}
382
383#endif
#define ICE_API
Definition Config.h:197
#define ICE_GLOBAL_VAR_SUFFIX
Definition Config.h:185
long long Int64
Definition Config.h:342