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 /****************************************************************************/
17 // Static storage of an output device and its base (abstract) implementation
18 /****************************************************************************/
19 #ifndef OutputDevice_h
20 #define OutputDevice_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <map>
30 #include <utils/common/ToString.h>
32 #include "PlainXMLFormatter.h"
33 #include "BinaryFormatter.h"
34 
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
63 class OutputDevice {
64 public:
67 
79  static OutputDevice& getDevice(const std::string& name);
80 
81 
99  static bool createDeviceByOption(const std::string& optionName,
100  const std::string& rootElement = "",
101  const std::string& schemaFile = "");
102 
103 
116  static OutputDevice& getDeviceByOption(const std::string& name);
117 
118 
121  static void closeAll(bool keepErrorRetrievers = false);
123 
124 
131  static std::string realString(const double v, const int precision = gPrecision);
132 
133 
134 public:
137 
139  OutputDevice(const bool binary = false, const int defaultIndentation = 0, const std::string& filename = "");
140 
141 
143  virtual ~OutputDevice();
144 
145 
149  virtual bool ok();
150 
152  const std::string& getFilename();
153 
156  void close();
157 
158 
162  void setPrecision(int precision = gPrecision);
163 
166  int getPrecision() {
167  return (int)getOStream().precision();
168  }
169 
181  bool writeXMLHeader(const std::string& rootElement,
182  const std::string& schemaFile,
183  std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>());
184 
185 
186  template <typename E>
187  bool writeHeader(const SumoXMLTag& rootElement) {
188  if (myAmBinary) {
189  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
190  }
191  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
192  }
193 
194 
204  OutputDevice& openTag(const std::string& xmlElement);
205 
206 
214  OutputDevice& openTag(const SumoXMLTag& xmlElement);
215 
216 
227  bool closeTag(const std::string& comment = "");
228 
229 
230 
233  void lf() {
234  if (!myAmBinary) {
235  getOStream() << "\n";
236  }
237  }
238 
239 
243  bool isBinary() const {
244  return myAmBinary;
245  }
246 
247 
254  template <typename T>
255  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
256  if (myAmBinary) {
258  } else {
260  }
261  return *this;
262  }
263 
264 
271  template <typename T>
272  OutputDevice& writeAttr(const std::string& attr, const T& val) {
273  if (myAmBinary) {
275  } else {
277  }
278  return *this;
279  }
280 
281 
288  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
289  if (val != "" && val != "default") {
290  writeAttr(attr, val);
291  }
292  return *this;
293  }
294 
295 
301  OutputDevice& writePreformattedTag(const std::string& val) {
303  return *this;
304  }
305 
307  OutputDevice& writePadding(const std::string& val) {
309  return *this;
310  }
311 
318  void inform(const std::string& msg, const char progress = 0);
319 
320 
324  template <class T>
325  OutputDevice& operator<<(const T& t) {
326  getOStream() << t;
327  postWriteHook();
328  return *this;
329  }
330 
331  void flush() {
332  getOStream().flush();
333  }
334 
335 protected:
337  virtual std::ostream& getOStream() = 0;
338 
339 
344  virtual void postWriteHook();
345 
346 
347 private:
349  static std::map<std::string, OutputDevice*> myOutputDevices;
350 
351 
352 private:
355 
356  const bool myAmBinary;
357 
358 protected:
359  std::string myFilename;
360 
361 public:
364 
365 private:
366 
369 
370 };
371 
372 
373 #endif
374 
375 /****************************************************************************/
376 
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:101
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:301
OutputFormatter::writePreformattedTag
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
OutputDevice::closeAll
static void closeAll(bool keepErrorRetrievers=false)
Definition: OutputDevice.cpp:126
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
OutputDevice::myFormatter
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:354
OutputDevice::operator<<
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:325
OutputDevice::writeAttr
OutputDevice & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: OutputDevice.h:272
OutputDevice::isBinary
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:243
OutputDevice::setPrecision
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
Definition: OutputDevice.cpp:221
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:179
OutputDevice::close
void close()
Closes the device and removes it from the dictionary.
Definition: OutputDevice.cpp:207
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:41
OutputDevice::myAmBinary
const bool myAmBinary
Definition: OutputDevice.h:356
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
OutputDevice::postWriteHook
virtual void postWriteHook()
Called after every write access.
Definition: OutputDevice.cpp:263
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
OutputDevice::myFilename
std::string myFilename
Definition: OutputDevice.h:359
OutputFormatter
Abstract base class for output formatters.
Definition: OutputFormatter.h:51
OutputDevice::flush
void flush()
Definition: OutputDevice.h:331
OutputDevice::inform
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
Definition: OutputDevice.cpp:267
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:288
PlainXMLFormatter
Output formatter for plain XML output.
Definition: PlainXMLFormatter.h:38
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:54
OutputDevice::writeHeader
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:187
OutputDevice::lf
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:233
BinaryFormatter
Output formatter for plain XML output.
Definition: BinaryFormatter.h:54
OutputDevice::myOutputDevices
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:349
config.h
OutputDevice::realString
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
Definition: OutputDevice.cpp:159
gPrecision
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:26
OutputDevice::ok
virtual bool ok()
returns the information whether one can write into the device
Definition: OutputDevice.cpp:196
BinaryFormatter::writeAttr
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
Definition: BinaryFormatter.h:290
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:227
OutputDevice::getFilename
const std::string & getFilename()
get filename or suitable description of this device
Definition: OutputDevice.cpp:202
OutputDevice::operator=
OutputDevice & operator=(const OutputDevice &)
Invalidated assignment operator.
SumoXMLAttr
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
Definition: SUMOXMLDefinitions.h:372
OutputDevice::~OutputDevice
virtual ~OutputDevice()
Destructor.
Definition: OutputDevice.cpp:190
BinaryFormatter.h
OutputDevice::getDeviceByOption
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
Definition: OutputDevice.cpp:116
OutputDevice::OutputDevice
OutputDevice(const OutputDevice &)
Invalidated copy constructor.
SUMOXMLDefinitions.h
OutputDevice::getPrecision
int getPrecision()
Returns the precison of the underlying stream.
Definition: OutputDevice.h:166
PlainXMLFormatter::writeAttr
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: PlainXMLFormatter.h:124
OutputDevice::writePadding
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:307