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.dev/sumo
3 // Copyright (C) 2004-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // Static storage of an output device and its base (abstract) implementation
22 /****************************************************************************/
23 #pragma once
24 #include <config.h>
25 
26 #include <string>
27 #include <map>
28 #include <cassert>
29 #include <utils/common/ToString.h>
31 #include "PlainXMLFormatter.h"
32 
33 
34 // ===========================================================================
35 // class definitions
36 // ===========================================================================
61 class OutputDevice {
62 public:
65 
77  static OutputDevice& getDevice(const std::string& name, bool usePrefix = true);
78 
79 
97  static bool createDeviceByOption(const std::string& optionName,
98  const std::string& rootElement = "",
99  const std::string& schemaFile = "");
100 
101 
114  static OutputDevice& getDeviceByOption(const std::string& name);
115 
118  static void flushAll();
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 int defaultIndentation = 0, const std::string& filename = "");
141 
142 
144  virtual ~OutputDevice();
145 
146 
150  virtual bool ok();
151 
155  virtual bool isNull() {
156  return false;
157  }
158 
160  const std::string& getFilename();
161 
164  void close();
165 
166 
170  void setPrecision(int precision = gPrecision);
171 
173  int precision();
174 
177  int getPrecision() {
178  return (int)getOStream().precision();
179  }
180 
192  bool writeXMLHeader(const std::string& rootElement,
193  const std::string& schemaFile,
194  std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>(),
195  bool includeConfig = true);
196 
197 
198  template <typename E>
199  bool writeHeader(const SumoXMLTag& rootElement) {
200  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
201  }
202 
203 
213  OutputDevice& openTag(const std::string& xmlElement);
214 
215 
223  OutputDevice& openTag(const SumoXMLTag& xmlElement);
224 
225 
236  bool closeTag(const std::string& comment = "");
237 
238 
239 
242  void lf() {
243  getOStream() << "\n";
244  }
245 
246 
253  template <typename T>
254  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
256  return *this;
257  }
258 
259  inline bool useAttribute(const SumoXMLAttr attr, SumoXMLAttrMask attributeMask) const {
260  return attributeMask.test(attr);
261  }
262 
270  template <typename T>
271  OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, long long int attributeMask) {
272  assert((int)attr <= 63);
273  if (attributeMask == 0 || useAttribute(attr, attributeMask)) {
275  }
276  return *this;
277  }
278  template <typename T>
279  OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, SumoXMLAttrMask attributeMask) {
280  assert((int)attr <= (int)attributeMask.size());
281  if (attributeMask.none() || useAttribute(attr, attributeMask)) {
283  }
284  return *this;
285  }
286 
287 
294  template <typename T>
295  OutputDevice& writeAttr(const std::string& attr, const T& val) {
297  return *this;
298  }
299 
306  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
307  if (val != "" && val != "default") {
308  writeAttr(attr, val);
309  }
310  return *this;
311  }
312 
313 
319  OutputDevice& writePreformattedTag(const std::string& val) {
321  return *this;
322  }
323 
325  OutputDevice& writePadding(const std::string& val) {
327  return *this;
328  }
329 
336  void inform(const std::string& msg, const char progress = 0);
337 
338 
342  template <class T>
343  OutputDevice& operator<<(const T& t) {
344  getOStream() << t;
345  postWriteHook();
346  return *this;
347  }
348 
349  void flush() {
350  getOStream().flush();
351  }
352 
353  bool wroteHeader() const {
354  return myFormatter->wroteHeader();
355  }
356 
357 protected:
359  virtual std::ostream& getOStream() = 0;
360 
361 
366  virtual void postWriteHook();
367 
368 
369 private:
371  static std::map<std::string, OutputDevice*> myOutputDevices;
372 
374  static int myPrevConsoleCP;
375 
376 protected:
377  const std::string myFilename;
378 
379 private:
382 
383 private:
385  OutputDevice(const OutputDevice&) = delete;
386 
389 
390 };
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::bitset< 96 > SumoXMLAttrMask
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:26
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
Definition: OutputDevice.h:319
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:242
OutputDevice & operator=(const OutputDevice &)=delete
Invalidated assignment operator.
OutputDevice(const OutputDevice &)=delete
Invalidated copy constructor.
virtual ~OutputDevice()
Destructor.
OutputDevice(const int defaultIndentation=0, const std::string &filename="")
Constructor.
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:343
void close()
Closes the device and removes it from the dictionary.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool wroteHeader() const
Definition: OutputDevice.h:353
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, long long int attributeMask)
writes a named attribute unless filtered
Definition: OutputDevice.h:271
virtual void postWriteHook()
Called after every write access.
static void flushAll()
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:306
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 & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: OutputDevice.h:295
int precision()
return precision set on the device
const std::string & getFilename()
get filename or suitable description of this device
virtual bool isNull()
returns the information whether the device will discard all output
Definition: OutputDevice.h:155
virtual std::ostream & getOStream()=0
Returns the associated ostream.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
int getPrecision()
Returns the precision of the underlying stream.
Definition: OutputDevice.h:177
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void setPrecision(int precision=gPrecision)
Sets the precision or resets it to default.
const std::string myFilename
Definition: OutputDevice.h:377
static void closeAll(bool keepErrorRetrievers=false)
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:325
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:199
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
OutputFormatter *const myFormatter
The formatter for XML.
Definition: OutputDevice.h:381
bool useAttribute(const SumoXMLAttr attr, SumoXMLAttrMask attributeMask) const
Definition: OutputDevice.h:259
static int myPrevConsoleCP
old console code page to restore after ending
Definition: OutputDevice.h:374
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, SumoXMLAttrMask attributeMask)
Definition: OutputDevice.h:279
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:371
virtual bool ok()
returns the information whether one can write into the device
Abstract base class for output formatters.
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
virtual void writePadding(std::ostream &into, const std::string &val)=0
virtual bool wroteHeader() const =0
Output formatter for plain XML output.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute