Eclipse SUMO - Simulation of Urban MObility
OutputDevice.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Static storage of an output device and its base (abstract) implementation
19 /****************************************************************************/
20 #ifndef OutputDevice_h
21 #define OutputDevice_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <string>
30 #include <map>
31 #include <utils/common/ToString.h>
33 #include "PlainXMLFormatter.h"
34 #include "BinaryFormatter.h"
35 
36 
37 // ===========================================================================
38 // class definitions
39 // ===========================================================================
64 class OutputDevice {
65 public:
68 
80  static OutputDevice& getDevice(const std::string& name);
81 
82 
100  static bool createDeviceByOption(const std::string& optionName,
101  const std::string& rootElement = "",
102  const std::string& schemaFile = "");
103 
104 
117  static OutputDevice& getDeviceByOption(const std::string& name);
118 
119 
122  static void closeAll(bool keepErrorRetrievers = false);
124 
125 
132  static std::string realString(const double v, const int precision = gPrecision);
133 
134 
135 public:
138 
140  OutputDevice(const bool binary = false, const int defaultIndentation = 0, const std::string& filename = "");
141 
142 
144  virtual ~OutputDevice();
145 
146 
150  virtual bool ok();
151 
153  const std::string& getFilename();
154 
157  void close();
158 
159 
163  void setPrecision(int precision = gPrecision);
164 
167  int getPrecision() {
168  return (int)getOStream().precision();
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(const std::string& comment = "");
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 
308  OutputDevice& writePadding(const std::string& val) {
310  return *this;
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  void flush() {
333  getOStream().flush();
334  }
335 
336 protected:
338  virtual std::ostream& getOStream() = 0;
339 
340 
345  virtual void postWriteHook();
346 
347 
348 private:
350  static std::map<std::string, OutputDevice*> myOutputDevices;
351 
352 
353 private:
356 
357  const bool myAmBinary;
358 
359 protected:
360  std::string myFilename;
361 
362 public:
364  OutputDevice(const OutputDevice&);
365 
366 private:
367 
370 
371 };
372 
373 
374 #endif
375 
376 /****************************************************************************/
377 
OutputDevice::createDeviceByOption
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.
Definition: OutputDevice.cpp:102
ToString.h
OutputDevice::writePreformattedTag
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
OutputFormatter::writePreformattedTag
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
OutputDevice::closeAll
static void closeAll(bool keepErrorRetrievers=false)
Definition: OutputDevice.cpp:127
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
OutputDevice::myFormatter
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:355
OutputDevice::operator<<
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:326
OutputDevice::writeAttr
OutputDevice & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: OutputDevice.h:273
OutputDevice::isBinary
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:244
OutputDevice::setPrecision
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
Definition: OutputDevice.cpp:222
OutputDevice::getOStream
virtual std::ostream & getOStream()=0
Returns the associated ostream.
OutputDevice::OutputDevice
OutputDevice(const bool binary=false, const int defaultIndentation=0, const std::string &filename="")
Constructor.
Definition: OutputDevice.cpp:180
OutputDevice::close
void close()
Closes the device and removes it from the dictionary.
Definition: OutputDevice.cpp:208
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:42
OutputDevice::myAmBinary
const bool myAmBinary
Definition: OutputDevice.h:357
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:254
OutputDevice::postWriteHook
virtual void postWriteHook()
Called after every write access.
Definition: OutputDevice.cpp:264
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
OutputDevice::myFilename
std::string myFilename
Definition: OutputDevice.h:360
OutputFormatter
Abstract base class for output formatters.
Definition: OutputFormatter.h:52
OutputDevice::flush
void flush()
Definition: OutputDevice.h:332
OutputDevice::inform
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
Definition: OutputDevice.cpp:268
OutputFormatter::writePadding
virtual void writePadding(std::ostream &into, const std::string &val)=0
PlainXMLFormatter.h
OutputDevice::writeNonEmptyAttr
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
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:240
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:55
OutputDevice::writeHeader
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:188
OutputDevice::lf
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:234
OutputDevice::myOutputDevices
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:350
config.h
OutputDevice::realString
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
Definition: OutputDevice.cpp:160
gPrecision
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:27
OutputDevice::ok
virtual bool ok()
returns the information whether one can write into the device
Definition: OutputDevice.cpp:197
BinaryFormatter::writeAttr
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
Definition: BinaryFormatter.h:291
OutputDevice::writeXMLHeader
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.
Definition: OutputDevice.cpp:228
OutputDevice::getFilename
const std::string & getFilename()
get filename or suitable description of this device
Definition: OutputDevice.cpp:203
OutputDevice::operator=
OutputDevice & operator=(const OutputDevice &)
Invalidated assignment operator.
SumoXMLAttr
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
Definition: SUMOXMLDefinitions.h:373
OutputDevice::~OutputDevice
virtual ~OutputDevice()
Destructor.
Definition: OutputDevice.cpp:191
BinaryFormatter.h
OutputDevice::getDeviceByOption
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
Definition: OutputDevice.cpp:117
SUMOXMLDefinitions.h
OutputDevice::getPrecision
int getPrecision()
Returns the precison of the underlying stream.
Definition: OutputDevice.h:167
PlainXMLFormatter::writeAttr
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: PlainXMLFormatter.h:125
OutputDevice::writePadding
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:308