Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2026 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 <chrono>
29#include <cassert>
32#include "CSVFormatter.h"
33#ifdef HAVE_PARQUET
34#include "ParquetFormatter.h"
35#endif
36#include "PlainXMLFormatter.h"
37
38// ===========================================================================
39// class declarations
40// ===========================================================================
41// ===========================================================================
42// class definitions
43// ===========================================================================
69public:
71 struct Settings {
73 std::string format;
75 std::string prefix;
77 std::string suffix;
79 std::string columnHeader = "auto";
81 std::string columnSeparator = ";";
83 std::string compression;
85 std::chrono::time_point<std::chrono::system_clock> loadTime;
86 };
87
90
93
105 static OutputDevice& getDevice(const std::string& name, bool usePrefix = true);
106
107
109 static bool deviceExists(const std::string& name) {
110 return myOutputDevices.find(name) != myOutputDevices.end();
111 }
112
115 static void flushAll();
116
119 static void closeAll(bool keepErrorRetrievers = false);
121
122public:
125
127 OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
128
129
131 virtual ~OutputDevice();
132
133
137 virtual bool ok();
138
142 virtual bool isNull() {
143 return false;
144 }
145
147 const std::string& getFilename();
148
151 void close();
152
153 bool isXML() const {
155 }
156
157 void setFormatter(OutputFormatter* formatter) {
158 delete myFormatter;
159 myFormatter = formatter;
160 }
161
165 void setPrecision(int precision = gPrecision);
166
170 return (int)getOStream().precision();
171 }
172
184 bool writeXMLHeader(const std::string& rootElement,
185 const std::string& schemaFile,
186 std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>(),
187 bool includeConfig = true);
188
198 OutputDevice& openTag(const std::string& xmlElement);
199
207 OutputDevice& openTag(const SumoXMLTag& xmlElement);
208
219 bool closeTag(const std::string& comment = "");
220
223 void lf() {
225 getOStream() << "\n";
226 }
227 }
228
239 template <typename T, class ATTR_TYPE>
240 OutputDevice& writeAttr(const ATTR_TYPE& attr, const T& val, const bool isNull = false, const bool escape = false) {
242 PlainXMLFormatter::writeAttr(getOStream(), attr, val, escape);
243#ifdef HAVE_PARQUET
244 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
245 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull, escape);
246#endif
247 } else {
248 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull, escape);
249 }
250 return *this;
251 }
252
263 template <typename T>
264 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const bool isNull = false, const bool escape = false) {
266 return *this;
267 }
268 return writeAttr(attr, val, isNull, escape);
269 }
270
280 template <typename T>
281 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const SumoXMLAttrMask& attributeMask,
282 const bool isNull = false, const bool escape = false) {
283 assert(attributeMask.none() || (int)attr <= (int)attributeMask.size());
284 if (attributeMask.none() || attributeMask.test(attr)) {
285 return writeOptionalAttr(attr, val, isNull, escape);
286 }
287 return *this;
288 }
289
302 template <typename Func>
303 OutputDevice& writeFuncAttr(const SumoXMLAttr attr, const Func& valFunc, const SumoXMLAttrMask& attributeMask,
304 const bool isNull = false, const bool escape = false) {
305 assert((int)attr <= (int)attributeMask.size());
306 if (attributeMask.none() || attributeMask.test(attr)) {
308 if (!isNull) {
309 PlainXMLFormatter::writeAttr(getOStream(), attr, valFunc(), escape);
310 }
311#ifdef HAVE_PARQUET
312 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
313 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull, escape);
314#endif
315 } else {
316 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull, escape);
317 }
318 }
319 return *this;
320 }
321
328 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
329 if (val != "" && val != "default") {
330 writeAttr(attr, val);
331 }
332 return *this;
333 }
334
335 OutputDevice& writeTime(const SumoXMLAttr attr, const SUMOTime val) {
336 myFormatter->writeTime(getOStream(), attr, val);
337 return *this;
338 }
339
345 OutputDevice& writePreformattedTag(const std::string& val) {
347 return *this;
348 }
349
351 OutputDevice& writePadding(const std::string& val) {
353 return *this;
354 }
355
362 void inform(const std::string& msg, const bool progress = false);
363
364
368 template <class T>
370 getOStream() << t;
372 return *this;
373 }
374
375 void flush() {
376 getOStream().flush();
377 }
378
379 bool wroteHeader() const {
380 return myFormatter->wroteHeader();
381 }
382
383 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth) {
384 myFormatter->setExpectedAttributes(expected, depth);
385 }
386
387protected:
389 virtual std::ostream& getOStream() = 0;
390
395 virtual void postWriteHook();
396
397
398private:
400 static std::map<std::string, OutputDevice*> myOutputDevices;
401
403 static int myPrevConsoleCP;
404
405protected:
406 const std::string myFilename;
407
410
411private:
413 OutputDevice(const OutputDevice&) = delete;
414
417
418};
long long int SUMOTime
Definition GUI.h:36
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:27
Output formatter for CSV output.
Static storage of an output device and its base (abstract) implementation.
void lf()
writes a line feed if applicable
virtual std::ostream & getOStream()=0
Returns the associated ostream.
OutputDevice(const OutputDevice &)=delete
Invalidated copy constructor.
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, const SumoXMLAttrMask &attributeMask, const bool isNull=false, const bool escape=false)
writes a named attribute unless filtered
virtual ~OutputDevice()
Destructor.
static Settings mySettings
the output related option values, set by SystemFrame so that this class does not depend on the option...
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"
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
void inform(const std::string &msg, const bool progress=false)
Retrieves a message to this device.
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
void close()
Closes the device and removes it from the dictionary.
OutputDevice & writeFuncAttr(const SumoXMLAttr attr, const Func &valFunc, const SumoXMLAttrMask &attributeMask, const bool isNull=false, const bool escape=false)
writes a named attribute with a generating function unless filtered
bool isXML() const
OutputDevice & operator=(const OutputDevice &)=delete
Invalidated assignment operator.
OutputDevice & operator<<(const T &t)
Abstract output operator.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute
bool wroteHeader() const
virtual void postWriteHook()
Called after every write access.
static void flushAll()
static bool deviceExists(const std::string &name)
Returns whether a device with the given name has already been created.
const std::string & getFilename()
get filename or suitable description of this device
void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth)
OutputFormatter * myFormatter
The formatter for XML, CSV or Parquet.
virtual bool isNull()
returns the information whether the device will discard all output
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute unless null
int getPrecision()
Returns the precision of the underlying stream.
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
static void closeAll(bool keepErrorRetrievers=false)
void setFormatter(OutputFormatter *formatter)
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
static int myPrevConsoleCP
old console code page to restore after ending
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
OutputDevice & writeTime(const SumoXMLAttr attr, const SUMOTime val)
virtual bool ok()
returns the information whether one can write into the device
Abstract base class for output formatters.
OutputFormatterType getType() const
Returns the type of formatter being used.
virtual void writePadding(std::ostream &into, const std::string &val)
Writes some whitespace to format the output. This method is only implemented for XML output.
virtual void writePreformattedTag(std::ostream &into, const std::string &val)
Writes a preformatted tag to the device but ensures that any pending tags are closed....
virtual void writeTime(std::ostream &into, const SumoXMLAttr attr, const SUMOTime val)=0
virtual void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth)
Set the expected attributes to write. This is used for tracking which attributes are expected in tabl...
virtual bool wroteHeader() const =0
Returns whether a header has been written. Useful to detect whether a file is being used by multiple ...
Output formatter for Parquet output.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val, const bool escape)
writes an arbitrary attribute
values of the output related options, handed over by SystemFrame so that this class does not depend o...
std::string suffix
the suffix to append to all output file names
std::string prefix
the prefix to prepend to all output file names
std::string columnHeader
how to derive column headers (CSV and Parquet)
std::string columnSeparator
the separator for CSV output
std::string compression
the compression algorithm for Parquet output
std::chrono::time_point< std::chrono::system_clock > loadTime
the time the configuration was loaded, used to replace TIME in prefix, suffix and file names
std::string format
the default output format ("xml", "csv" or "parquet")