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-2012 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 
183  bool writeXMLHeader(const std::string& rootElement,
184  const std::string xmlParams = "",
185  const std::string& attrs = "",
186  const std::string& comment = "");
187 
188 
189  template <typename E>
190  bool writeHeader(const SumoXMLTag& rootElement) {
191  if (myAmBinary) {
192  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
193  }
194  if (rootElement == SUMO_TAG_ROUTES) {
195  return myFormatter->writeXMLHeader(getOStream(), "routes", "", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/routes_file.xsd\"");
196  }
197  return myFormatter->writeXMLHeader(getOStream(), toString(rootElement));
198  }
199 
200 
210  OutputDevice& openTag(const std::string& xmlElement);
211 
212 
220  OutputDevice& openTag(const SumoXMLTag& xmlElement);
221 
222 
227  void closeOpener();
228 
239  bool closeTag(bool abbreviated = false);
240 
247  OutputDevice& writeAttr(std::string attr, std::string val);
248 
249 
252  void lf() {
253  if (!myAmBinary) {
254  getOStream() << "\n";
255  }
256  }
257 
258 
262  bool isBinary() const {
263  return myAmBinary;
264  }
265 
266 
273  template <typename T>
274  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
275  if (myAmBinary) {
277  } else {
279  }
280  return *this;
281  }
282 
283 
290  void inform(const std::string& msg, const char progress = 0);
291 
292 
296  template <class T>
297  OutputDevice& operator<<(const T& t) {
298  getOStream() << t;
299  postWriteHook();
300  return *this;
301  }
302 
303 protected:
305  virtual std::ostream& getOStream() = 0;
306 
307 
312  virtual void postWriteHook();
313 
314 
315 private:
317  static std::map<std::string, OutputDevice*> myOutputDevices;
318 
319 
320 private:
323 
324  const bool myAmBinary;
325 
326 public:
328  OutputDevice(const OutputDevice&);
329 
330 private:
331 
334 
335 };
336 
337 
338 #endif
339 
340 /****************************************************************************/
341