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-2020 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 <utils/common/ToString.h>
30 #include "PlainXMLFormatter.h"
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
60 class OutputDevice {
61 public:
64 
76  static OutputDevice& getDevice(const std::string& name);
77 
78 
96  static bool createDeviceByOption(const std::string& optionName,
97  const std::string& rootElement = "",
98  const std::string& schemaFile = "");
99 
100 
113  static OutputDevice& getDeviceByOption(const std::string& name);
114 
115 
118  static void closeAll(bool keepErrorRetrievers = false);
120 
121 
128  static std::string realString(const double v, const int precision = gPrecision);
129 
130 
131 public:
134 
136  OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
137 
138 
140  virtual ~OutputDevice();
141 
142 
146  virtual bool ok();
147 
149  const std::string& getFilename();
150 
153  void close();
154 
155 
159  void setPrecision(int precision = gPrecision);
160 
163  int getPrecision() {
164  return (int)getOStream().precision();
165  }
166 
178  bool writeXMLHeader(const std::string& rootElement,
179  const std::string& schemaFile,
180  std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>());
181 
182 
183  template <typename E>
184  bool writeHeader(const SumoXMLTag& rootElement) {
185  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
186  }
187 
188 
198  OutputDevice& openTag(const std::string& xmlElement);
199 
200 
208  OutputDevice& openTag(const SumoXMLTag& xmlElement);
209 
210 
221  bool closeTag(const std::string& comment = "");
222 
223 
224 
227  void lf() {
228  getOStream() << "\n";
229  }
230 
231 
238  template <typename T>
239  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
241  return *this;
242  }
243 
244 
251  template <typename T>
252  OutputDevice& writeAttr(const std::string& attr, const T& val) {
254  return *this;
255  }
256 
257 
264  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
265  if (val != "" && val != "default") {
266  writeAttr(attr, val);
267  }
268  return *this;
269  }
270 
271 
277  OutputDevice& writePreformattedTag(const std::string& val) {
279  return *this;
280  }
281 
283  OutputDevice& writePadding(const std::string& val) {
285  return *this;
286  }
287 
294  void inform(const std::string& msg, const char progress = 0);
295 
296 
300  template <class T>
301  OutputDevice& operator<<(const T& t) {
302  getOStream() << t;
303  postWriteHook();
304  return *this;
305  }
306 
307  void flush() {
308  getOStream().flush();
309  }
310 
311 protected:
313  virtual std::ostream& getOStream() = 0;
314 
315 
320  virtual void postWriteHook();
321 
322 
323 private:
325  static std::map<std::string, OutputDevice*> myOutputDevices;
326 
327 
328 private:
331 
332 protected:
333  std::string myFilename;
334 
335 public:
338 
339 private:
340 
343 
344 };
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:60
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
Definition: OutputDevice.h:277
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:227
virtual ~OutputDevice()
Destructor.
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 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:301
void close()
Closes the device and removes it from the dictionary.
OutputDevice & operator=(const OutputDevice &)
Invalidated assignment operator.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
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:264
std::string myFilename
Definition: OutputDevice.h:333
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:252
const std::string & getFilename()
get filename or suitable description of this device
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:330
virtual std::ostream & getOStream()=0
Returns the associated ostream.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:239
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
int getPrecision()
Returns the precison of the underlying stream.
Definition: OutputDevice.h:163
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
static void closeAll(bool keepErrorRetrievers=false)
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:283
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:184
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:325
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
OutputDevice(const OutputDevice &)
Invalidated copy constructor.
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