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 
172  int getPrecision() {
173  return (int)getOStream().precision();
174  }
175 
187  bool writeXMLHeader(const std::string& rootElement,
188  const std::string& schemaFile,
189  std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>());
190 
191 
192  template <typename E>
193  bool writeHeader(const SumoXMLTag& rootElement) {
194  if (myAmBinary) {
195  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
196  }
197  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
198  }
199 
200 
210  OutputDevice& openTag(const std::string& xmlElement);
211 
212 
220  OutputDevice& openTag(const SumoXMLTag& xmlElement);
221 
222 
233  bool closeTag();
234 
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  template <typename T>
278  OutputDevice& writeAttr(const std::string& attr, const T& val) {
279  if (myAmBinary) {
281  } else {
283  }
284  return *this;
285  }
286 
287 
294  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
295  if (val != "" && val != "default") {
296  writeAttr(attr, val);
297  }
298  return *this;
299  }
300 
301 
307  OutputDevice& writePreformattedTag(const std::string& val) {
309  return *this;
310  }
311 
312 
319  void inform(const std::string& msg, const char progress = 0);
320 
321 
325  template <class T>
326  OutputDevice& operator<<(const T& t) {
327  getOStream() << t;
328  postWriteHook();
329  return *this;
330  }
331 
332 protected:
334  virtual std::ostream& getOStream() = 0;
335 
336 
341  virtual void postWriteHook();
342 
343 
344 private:
346  static std::map<std::string, OutputDevice*> myOutputDevices;
347 
348 
349 private:
352 
353  const bool myAmBinary;
354 
355 public:
357  OutputDevice(const OutputDevice&);
358 
359 private:
360 
363 
364 };
365 
366 
367 #endif
368 
369 /****************************************************************************/
370 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:261
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:346
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:30
int getPrecision()
Returns the precison of the underlying stream.
Definition: OutputDevice.h:172
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...
Definition: OutputDevice.h:307
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:193
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:294
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:278
Output formatter for plain XML output.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:326
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:353
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:351
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.
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:239
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:249