SUMO - Simulation of Urban MObility
OutputDevice.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // Static storage of an output device and its base (abstract) implementation
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2004-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 #ifndef OutputDevice_h
24 #define OutputDevice_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <string>
37 #include <map>
38 #include <utils/common/ToString.h>
40 #include "PlainXMLFormatter.h"
41 #include "BinaryFormatter.h"
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
71 class OutputDevice {
72 public:
75 
87  static OutputDevice& getDevice(const std::string& name);
88 
89 
107  static bool createDeviceByOption(const std::string& optionName,
108  const std::string& rootElement = "",
109  const std::string& schemaFile = "");
110 
111 
124  static OutputDevice& getDeviceByOption(const std::string& name);
125 
126 
129  static void closeAll();
131 
132 
139  static std::string realString(const double v, const int precision = gPrecision);
140 
141 
142 public:
145 
147  OutputDevice(const bool binary = false, const int defaultIndentation = 0);
148 
149 
151  virtual ~OutputDevice();
152 
153 
157  virtual bool ok();
158 
159 
162  void close();
163 
164 
168  void setPrecision(int precision = gPrecision);
169 
170 
182  bool writeXMLHeader(const std::string& rootElement,
183  const std::string& schemaFile,
184  std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>());
185 
186 
187  template <typename E>
188  bool writeHeader(const SumoXMLTag& rootElement) {
189  if (myAmBinary) {
190  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
191  }
192  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
193  }
194 
195 
205  OutputDevice& openTag(const std::string& xmlElement);
206 
207 
215  OutputDevice& openTag(const SumoXMLTag& xmlElement);
216 
217 
228  bool closeTag();
229 
230 
231 
234  void lf() {
235  if (!myAmBinary) {
236  getOStream() << "\n";
237  }
238  }
239 
240 
244  bool isBinary() const {
245  return myAmBinary;
246  }
247 
248 
255  template <typename T>
256  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
257  if (myAmBinary) {
259  } else {
261  }
262  return *this;
263  }
264 
265 
272  template <typename T>
273  OutputDevice& writeAttr(const std::string& attr, const T& val) {
274  if (myAmBinary) {
276  } else {
278  }
279  return *this;
280  }
281 
282 
289  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
290  if (val != "" && val != "default") {
291  writeAttr(attr, val);
292  }
293  return *this;
294  }
295 
296 
302  OutputDevice& writePreformattedTag(const std::string& val) {
304  return *this;
305  }
306 
307 
314  void inform(const std::string& msg, const char progress = 0);
315 
316 
320  template <class T>
321  OutputDevice& operator<<(const T& t) {
322  getOStream() << t;
323  postWriteHook();
324  return *this;
325  }
326 
327 protected:
329  virtual std::ostream& getOStream() = 0;
330 
331 
336  virtual void postWriteHook();
337 
338 
339 private:
341  static std::map<std::string, OutputDevice*> myOutputDevices;
342 
343 
344 private:
347 
348  const bool myAmBinary;
349 
350 public:
352  OutputDevice(const OutputDevice&);
353 
354 private:
355 
358 
359 };
360 
361 
362 #endif
363 
364 /****************************************************************************/
365 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:341
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:30
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...
Definition: OutputDevice.h:302
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:188
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
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 "default" ...
Definition: OutputDevice.h:289
static void closeAll()
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
OutputDevice(const bool binary=false, const int defaultIndentation=0)
Constructor.
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:273
Output formatter for plain XML output.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:321
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
virtual ~OutputDevice()
Destructor.
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.
const bool myAmBinary
Definition: OutputDevice.h:348
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:346
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
OutputDevice & operator=(const OutputDevice &)
Invalidated assignment operator.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:244
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:234