Ice 3.7 C++11 API Reference
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 
13 namespace IceUtilInternal
14 {
15 
16 ICE_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 
29 class ICE_API OutputBase : private ::IceUtil::noncopyable
30 {
31 public:
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 
60 protected:
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 
72 class ICE_API NextLine
73 {
74 };
75 extern ICE_API NextLine nl ICE_GLOBAL_VAR_SUFFIX;
76 
77 class ICE_API Separator
78 {
79 };
80 extern ICE_API Separator sp ICE_GLOBAL_VAR_SUFFIX;
81 
82 // ----------------------------------------------------------------------
83 // Output
84 // ----------------------------------------------------------------------
85 
86 class ICE_API Output : public OutputBase
87 {
88 public:
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 
102 private:
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 
112 template<typename T>
113 inline Output&
114 operator<<(Output& out, const T& val)
115 {
116  std::ostringstream s;
117  s << val;
118  out.print(s.str());
119  return out;
120 }
121 
122 template<typename T>
123 inline Output&
124 operator<<(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 
133 template<>
134 inline Output&
135 operator<<(Output& o, const NextLine&)
136 {
137  o.newline();
138  return o;
139 }
140 
141 template<>
142 inline Output&
143 operator<<(Output& o, const Separator&)
144 {
145  o.separator();
146  return o;
147 }
148 
149 class ICE_API StartBlock
150 {
151 };
152 extern ICE_API StartBlock sb ICE_GLOBAL_VAR_SUFFIX;
153 
154 template<>
155 inline Output&
156 operator<<(Output& o, const StartBlock&)
157 {
158  o.sb();
159  return o;
160 }
161 
162 class ICE_API EndBlock
163 {
164 };
165 extern ICE_API EndBlock eb ICE_GLOBAL_VAR_SUFFIX;
166 
167 template<>
168 inline Output&
169 operator<<(Output& o, const EndBlock&)
170 {
171  o.eb();
172  return o;
173 }
174 
175 class ICE_API StartPar
176 {
177 };
178 extern ICE_API StartPar spar ICE_GLOBAL_VAR_SUFFIX;
179 
180 template<>
181 inline Output&
182 operator<<(Output& o, const StartPar&)
183 {
184  o.spar();
185  return o;
186 }
187 
188 class ICE_API EndPar
189 {
190 };
191 extern ICE_API EndPar epar ICE_GLOBAL_VAR_SUFFIX;
192 
193 template<>
194 inline Output&
195 operator<<(Output& o, const EndPar&)
196 {
197  o.epar();
198  return o;
199 }
200 
201 class ICE_API StartAbrk
202 {
203 };
204 extern ICE_API StartAbrk sabrk ICE_GLOBAL_VAR_SUFFIX;
205 
206 template<>
207 inline Output&
208 operator<<(Output& o, const StartAbrk&)
209 {
210  o.spar('<');
211  return o;
212 }
213 
214 class ICE_API EndAbrk
215 {
216 };
217 extern ICE_API EndAbrk eabrk ICE_GLOBAL_VAR_SUFFIX;
218 
219 template<>
220 inline Output&
221 operator<<(Output& o, const EndAbrk&)
222 {
223  o.epar('>');
224  return o;
225 }
226 
227 ICE_API Output& operator<<(Output&, std::ios_base& (*)(std::ios_base&));
228 
229 // ----------------------------------------------------------------------
230 // XMLOutput
231 // ----------------------------------------------------------------------
232 
233 class ICE_API XMLOutput : public OutputBase
234 {
235 public:
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 
254 private:
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 
266 template<typename T>
267 inline XMLOutput&
268 operator<<(XMLOutput& out, const T& val)
269 {
270  std::ostringstream s;
271  s << val;
272  out.print(s.str());
273  return out;
274 }
275 
276 template<>
277 inline XMLOutput&
278 operator<<(XMLOutput& o, const NextLine&)
279 {
280  o.newline();
281  return o;
282 }
283 
284 template<>
285 inline XMLOutput&
286 operator<<(XMLOutput& o, const Separator&)
287 {
288  o.separator();
289  return o;
290 }
291 
292 class ICE_API EndElement
293 {
294 };
295 extern ICE_API EndElement ee ICE_GLOBAL_VAR_SUFFIX;
296 
297 template<>
298 inline XMLOutput&
299 operator<<(XMLOutput& o, const EndElement&)
300 {
301  o.endElement();
302  return o;
303 }
304 
305 class ICE_API StartElement
306 {
307 public:
308 
309  StartElement(const std::string&);
310 
311  const std::string& getName() const;
312 
313 private:
314 
315  const std::string _name;
316 };
317 
318 typedef StartElement se;
319 
320 template<>
321 inline XMLOutput&
322 operator<<(XMLOutput& o, const StartElement& e)
323 {
324  o.startElement(e.getName());
325  return o;
326 }
327 
328 class ICE_API Attribute
329 {
330 public:
331 
332  Attribute(const ::std::string&, const ::std::string&);
333 
334  const ::std::string& getName() const;
335  const ::std::string& getValue() const;
336 
337 private:
338 
339  const ::std::string _name;
340  const ::std::string _value;
341 };
342 
343 typedef Attribute attr;
344 
345 template<>
346 inline XMLOutput&
347 operator<<(XMLOutput& o, const Attribute& e)
348 {
349  o.attr(e.getName(), e.getValue());
350  return o;
351 }
352 
353 class ICE_API StartEscapes
354 {
355 };
356 extern ICE_API StartEscapes startEscapes ICE_GLOBAL_VAR_SUFFIX;
357 
358 class ICE_API EndEscapes
359 {
360 };
361 extern ICE_API EndEscapes endEscapes ICE_GLOBAL_VAR_SUFFIX;
362 
363 template<>
364 inline XMLOutput&
365 operator<<(XMLOutput& o, const StartEscapes&)
366 {
367  o.startEscapes();
368  return o;
369 }
370 
371 template<>
372 inline XMLOutput&
373 operator<<(XMLOutput& o, const EndEscapes&)
374 {
375  o.endEscapes();
376  return o;
377 }
378 
379 ICE_API XMLOutput& operator<<(XMLOutput&, std::ios_base& (*)(std::ios_base&));
380 
381 }
382 
383 #endif
ICE_GLOBAL_VAR_SUFFIX
#define ICE_GLOBAL_VAR_SUFFIX
Definition: Config.h:185
Ice::operator<<
std::ostream & operator<<(std::ostream &out, const ProtocolVersion &version)
Definition: Protocol.h:179
ICE_API
#define ICE_API
Definition: Config.h:197
IceUtil::noncopyable
Definition: Config.h:313
Config.h
IceUtil::Int64
long long Int64
Definition: Config.h:342