SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OutputDevice.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Static storage of an output device and its base (abstract) implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef OutputDevice_h
23 #define OutputDevice_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <map>
37 #include <utils/common/ToString.h>
39 #include "PlainXMLFormatter.h"
40 #include "BinaryFormatter.h"
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
70 class OutputDevice {
71 public:
74 
87  static OutputDevice& getDevice(const std::string& name,
88  const std::string& base = "");
89 
90 
107  static bool createDeviceByOption(const std::string& optionName,
108  const std::string& rootElement = "");
109 
110 
123  static OutputDevice& getDeviceByOption(const std::string& name) throw(IOError, InvalidArgument);
124 
125 
128  static void closeAll();
130 
131 
138  static std::string realString(const SUMOReal v, const int precision = OUTPUT_ACCURACY);
139 
140 
141 public:
144 
146  OutputDevice(const bool binary = false, const unsigned int defaultIndentation = 0);
147 
148 
150  virtual ~OutputDevice();
151 
152 
156  virtual bool ok();
157 
158 
161  void close();
162 
163 
167  void setPrecision(unsigned int precision = OUTPUT_ACCURACY);
168 
169 
182  bool writeXMLHeader(const std::string& rootElement,
183  const std::string& attrs = "",
184  const std::string& comment = "");
185 
186 
187  template <typename E>
188  bool writeHeader(const SumoXMLTag& rootElement) {
189  if (myAmBinary) {
190  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
191  }
192  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
193  }
194 
195 
205  OutputDevice& openTag(const std::string& xmlElement);
206 
207 
215  OutputDevice& openTag(const SumoXMLTag& xmlElement);
216 
217 
228  bool closeTag();
229 
236  OutputDevice& writeAttr(std::string attr, std::string val);
237 
238 
241  void lf() {
242  if (!myAmBinary) {
243  getOStream() << "\n";
244  }
245  }
246 
247 
251  bool isBinary() const {
252  return myAmBinary;
253  }
254 
255 
262  template <typename T>
263  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
264  if (myAmBinary) {
266  } else {
268  }
269  return *this;
270  }
271 
272 
279  void inform(const std::string& msg, const char progress = 0);
280 
281 
285  template <class T>
286  OutputDevice& operator<<(const T& t) {
287  getOStream() << t;
288  postWriteHook();
289  return *this;
290  }
291 
292 protected:
294  virtual std::ostream& getOStream() = 0;
295 
296 
301  virtual void postWriteHook();
302 
303 
304 private:
306  static std::map<std::string, OutputDevice*> myOutputDevices;
307 
308 
309 private:
312 
313  const bool myAmBinary;
314 
315 public:
317  OutputDevice(const OutputDevice&);
318 
319 private:
320 
323 
324 };
325 
326 
327 #endif
328 
329 /****************************************************************************/
330