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-2022 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);
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 
116 
119  static void closeAll(bool keepErrorRetrievers = false);
121 
122 
129  static std::string realString(const double v, const int precision = gPrecision);
130 
131 
132 public:
135 
137  OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
138 
139 
141  virtual ~OutputDevice();
142 
143 
147  virtual bool ok();
148 
152  virtual bool isNull() {
153  return false;
154  }
155 
157  const std::string& getFilename();
158 
161  void close();
162 
163 
167  void setPrecision(int precision = gPrecision);
168 
171  int getPrecision() {
172  return (int)getOStream().precision();
173  }
174 
186  bool writeXMLHeader(const std::string& rootElement,
187  const std::string& schemaFile,
188  std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>(),
189  bool includeConfig = true);
190 
191 
192  template <typename E>
193  bool writeHeader(const SumoXMLTag& rootElement) {
194  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
195  }
196 
197 
207  OutputDevice& openTag(const std::string& xmlElement);
208 
209 
217  OutputDevice& openTag(const SumoXMLTag& xmlElement);
218 
219 
230  bool closeTag(const std::string& comment = "");
231 
232 
233 
236  void lf() {
237  getOStream() << "\n";
238  }
239 
240 
247  template <typename T>
248  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
250  return *this;
251  }
252 
253  inline bool useAttribute(const SumoXMLAttr attr, long long int attributeMask) const {
254  return (attributeMask & ((long long int)1 << attr)) != 0;
255  }
256 
264  template <typename T>
265  OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, long long int attributeMask) {
266  assert((int)attr <= 63);
267  if (attributeMask == 0 || useAttribute(attr, attributeMask)) {
269  }
270  return *this;
271  }
272 
273 
280  template <typename T>
281  OutputDevice& writeAttr(const std::string& attr, const T& val) {
283  return *this;
284  }
285 
292  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
293  if (val != "" && val != "default") {
294  writeAttr(attr, val);
295  }
296  return *this;
297  }
298 
299 
305  OutputDevice& writePreformattedTag(const std::string& val) {
307  return *this;
308  }
309 
311  OutputDevice& writePadding(const std::string& val) {
313  return *this;
314  }
315 
322  void inform(const std::string& msg, const char progress = 0);
323 
324 
328  template <class T>
329  OutputDevice& operator<<(const T& t) {
330  getOStream() << t;
331  postWriteHook();
332  return *this;
333  }
334 
335  void flush() {
336  getOStream().flush();
337  }
338 
339 protected:
341  virtual std::ostream& getOStream() = 0;
342 
343 
348  virtual void postWriteHook();
349 
350 
351 private:
353  static std::map<std::string, OutputDevice*> myOutputDevices;
354 
356  static int myPrevConsoleCP;
357 
358 protected:
359  const std::string myFilename;
360 
361 private:
364 
365 private:
367  OutputDevice(const OutputDevice&) = delete;
368 
371 
372 };
SumoXMLTag
Numbers representing SUMO-XML - element names.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
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:305
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:236
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:329
void close()
Closes the device and removes it from the dictionary.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, long long int attributeMask)
writes a named attribute unless filtered
Definition: OutputDevice.h:265
virtual void postWriteHook()
Called after every write access.
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:292
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:281
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:152
virtual std::ostream & getOStream()=0
Returns the associated ostream.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
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:171
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:359
static void closeAll(bool keepErrorRetrievers=false)
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:311
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:193
OutputFormatter *const myFormatter
The formatter for XML.
Definition: OutputDevice.h:363
static int myPrevConsoleCP
old console code page to restore after ending
Definition: OutputDevice.h:356
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:353
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
bool useAttribute(const SumoXMLAttr attr, long long int attributeMask) const
Definition: OutputDevice.h:253
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
Output formatter for plain XML output.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute