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 
198  OutputDevice& openTag(const std::string& xmlElement);
199 
200 
208  OutputDevice& openTag(const SumoXMLTag& xmlElement);
209 
210 
215  void closeOpener();
216 
227  bool closeTag(bool abbreviated=false);
228 
235  OutputDevice& writeAttr(std::string attr, std::string val);
236 
237 
240  void lf() {
241  if (!myAmBinary) {
242  getOStream() << "\n";
243  }
244  }
245 
246 
250  bool isBinary() const {
251  return myAmBinary;
252  }
253 
254 
261  template <typename T>
262  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
263  if (myAmBinary) {
265  } else {
267  }
268  return *this;
269  }
270 
271 
278  void inform(const std::string& msg, const char progress=0);
279 
280 
284  template <class T>
285  OutputDevice& operator<<(const T& t) {
286  getOStream() << t;
287  postWriteHook();
288  return *this;
289  }
290 
291 protected:
293  virtual std::ostream& getOStream() = 0;
294 
295 
300  virtual void postWriteHook() ;
301 
302 
303 private:
305  static std::map<std::string, OutputDevice*> myOutputDevices;
306 
307 
308 private:
311 
312  const bool myAmBinary;
313 
314 private:
316  OutputDevice(const OutputDevice&);
317 
320 
321 };
322 
323 
324 #endif
325 
326 /****************************************************************************/
327