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 
86  static OutputDevice& getDevice(const std::string& name);
87 
88 
105  static bool createDeviceByOption(const std::string& optionName,
106  const std::string& rootElement = "");
107 
108 
121  static OutputDevice& getDeviceByOption(const std::string& name) throw(IOError, InvalidArgument);
122 
123 
126  static void closeAll();
128 
129 
136  static std::string realString(const SUMOReal v, const int precision = OUTPUT_ACCURACY);
137 
138 
139 public:
142 
144  OutputDevice(const bool binary = false, const unsigned int defaultIndentation = 0);
145 
146 
148  virtual ~OutputDevice();
149 
150 
154  virtual bool ok();
155 
156 
159  void close();
160 
161 
165  void setPrecision(unsigned int precision = OUTPUT_ACCURACY);
166 
167 
180  bool writeXMLHeader(const std::string& rootElement,
181  const std::string& attrs = "",
182  const std::string& comment = "");
183 
184 
185  template <typename E>
186  bool writeHeader(const SumoXMLTag& rootElement) {
187  if (myAmBinary) {
188  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
189  }
190  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
191  }
192 
193 
203  OutputDevice& openTag(const std::string& xmlElement);
204 
205 
213  OutputDevice& openTag(const SumoXMLTag& xmlElement);
214 
215 
226  bool closeTag();
227 
234  OutputDevice& writeAttr(std::string attr, std::string val);
235 
236 
239  void lf() {
240  if (!myAmBinary) {
241  getOStream() << "\n";
242  }
243  }
244 
245 
249  bool isBinary() const {
250  return myAmBinary;
251  }
252 
253 
260  template <typename T>
261  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
262  if (myAmBinary) {
264  } else {
266  }
267  return *this;
268  }
269 
270 
277  void inform(const std::string& msg, const char progress = 0);
278 
279 
283  template <class T>
284  OutputDevice& operator<<(const T& t) {
285  getOStream() << t;
286  postWriteHook();
287  return *this;
288  }
289 
290 protected:
292  virtual std::ostream& getOStream() = 0;
293 
294 
299  virtual void postWriteHook();
300 
301 
302 private:
304  static std::map<std::string, OutputDevice*> myOutputDevices;
305 
306 
307 private:
310 
311  const bool myAmBinary;
312 
313 public:
315  OutputDevice(const OutputDevice&);
316 
317 private:
318 
321 
322 };
323 
324 
325 #endif
326 
327 /****************************************************************************/
328