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-sim.org/
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);
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 
228 
229 
232  void lf() {
233  if (!myAmBinary) {
234  getOStream() << "\n";
235  }
236  }
237 
238 
242  bool isBinary() const {
243  return myAmBinary;
244  }
245 
246 
253  template <typename T>
254  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
255  if (myAmBinary) {
257  } else {
259  }
260  return *this;
261  }
262 
263 
270  template <typename T>
271  OutputDevice& writeAttr(const std::string& attr, const T& val) {
272  if (myAmBinary) {
274  } else {
276  }
277  return *this;
278  }
279 
280 
287  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
288  if (val != "" && val != "default") {
289  writeAttr(attr, val);
290  }
291  return *this;
292  }
293 
294 
301  void inform(const std::string& msg, const char progress = 0);
302 
303 
307  template <class T>
308  OutputDevice& operator<<(const T& t) {
309  getOStream() << t;
310  postWriteHook();
311  return *this;
312  }
313 
314 protected:
316  virtual std::ostream& getOStream() = 0;
317 
318 
323  virtual void postWriteHook();
324 
325 
326 private:
328  static std::map<std::string, OutputDevice*> myOutputDevices;
329 
330 
331 private:
334 
335  const bool myAmBinary;
336 
337 public:
339  OutputDevice(const OutputDevice&);
340 
341 private:
342 
345 
346 };
347 
348 
349 #endif
350 
351 /****************************************************************************/
352