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 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
void close()
Closes the device and removes it from the dictionary.
SumoXMLTag
Numbers representing SUMO-XML - element names.
Abstract base class for output formatters.
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:328
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
OutputDevice(const bool binary=false, const unsigned int defaultIndentation=0)
Constructor.
#define OUTPUT_ACCURACY
Definition: config.h:168
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:186
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
Output formatter for plain XML output.
OutputDevice & writeNonEmptyAttr(const SumoXMLAttr attr, const std::string &val)
writes a string attribute only if it is not the empty string and not the string &quot;default&quot; ...
Definition: OutputDevice.h:287
static void closeAll()
virtual bool ok()
returns the information whether one can write into the device
OutputDevice & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: OutputDevice.h:271
Output formatter for plain XML output.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:308
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
virtual ~OutputDevice()
Destructor.
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="")
Creates the device using the output definition stored in the named option.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY)
Helper method for string formatting.
const bool myAmBinary
Definition: OutputDevice.h:335
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:333
OutputDevice & operator=(const OutputDevice &)
Invalidated assignment operator.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:221
virtual std::ostream & getOStream()=0
Returns the associated ostream.
virtual void postWriteHook()
Called after every write access.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:232
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:242