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 <cassert>
31#include "CSVFormatter.h"
32#ifdef HAVE_PARQUET
33#include "ParquetFormatter.h"
34#endif
35#include "PlainXMLFormatter.h"
36
37// ===========================================================================
38// class definitions
39// ===========================================================================
65public:
68
80 static OutputDevice& getDevice(const std::string& name, bool usePrefix = true);
81
82
101 static bool createDeviceByOption(const std::string& optionName,
102 const std::string& rootElement = "",
103 const std::string& schemaFile = "",
104 const int maximumDepth = 2);
105
106
119 static OutputDevice& getDeviceByOption(const std::string& name);
120
123 static void flushAll();
124
127 static void closeAll(bool keepErrorRetrievers = false);
129
130public:
133
135 OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
136
137
139 virtual ~OutputDevice();
140
141
145 virtual bool ok();
146
150 virtual bool isNull() {
151 return false;
152 }
153
155 const std::string& getFilename();
156
159 void close();
160
161 bool isXML() const {
163 }
164
165 void setFormatter(OutputFormatter* formatter) {
166 delete myFormatter;
167 myFormatter = formatter;
168 }
169
173 void setPrecision(int precision = gPrecision);
174
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
206 OutputDevice& openTag(const std::string& xmlElement);
207
215 OutputDevice& openTag(const SumoXMLTag& xmlElement);
216
227 bool closeTag(const std::string& comment = "");
228
231 void lf() {
232 getOStream() << "\n";
233 }
234
241 template <typename T>
242 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
245#ifdef HAVE_PARQUET
246 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
247 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
248#endif
249 } else {
250 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
251 }
252 return *this;
253 }
254
264 static const SumoXMLAttrMask parseWrittenAttributes(const std::vector<std::string>& attrList, const std::string& desc,
265 const std::map<std::string, SumoXMLAttrMask>& special = std::map<std::string, SumoXMLAttrMask>());
266
274 template <typename T>
275 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const SumoXMLAttrMask& attributeMask, const bool isNull = false) {
276 assert((int)attr <= (int)attributeMask.size());
277 if (attributeMask.none() || attributeMask.test(attr)) {
279 if (!isNull) {
281 }
282#ifdef HAVE_PARQUET
283 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
284 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull);
285#endif
286 } else {
287 if (isNull) {
288 static_cast<CSVFormatter*>(myFormatter)->writeNull(getOStream(), attr);
289 } else {
290 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
291 }
292 }
293 }
294 return *this;
295 }
296
297 template <typename Func>
298 OutputDevice& writeFuncAttr(const SumoXMLAttr attr, const Func& valFunc, const SumoXMLAttrMask& attributeMask, const bool isNull = false) {
299 assert((int)attr <= (int)attributeMask.size());
300 if (attributeMask.none() || attributeMask.test(attr)) {
302 if (!isNull) {
303 PlainXMLFormatter::writeAttr(getOStream(), attr, valFunc());
304 }
305#ifdef HAVE_PARQUET
306 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
307 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull);
308#endif
309 } else {
310 if (isNull) {
311 static_cast<CSVFormatter*>(myFormatter)->writeNull(getOStream(), attr);
312 } else {
313 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc());
314 }
315 }
316 }
317 return *this;
318 }
319
326 template <typename T>
327 OutputDevice& writeAttr(const std::string& attr, const T& val) {
330#ifdef HAVE_PARQUET
331 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
332 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
333#endif
334 } else {
335 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
336 }
337 return *this;
338 }
339
346 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
347 if (val != "" && val != "default") {
348 writeAttr(attr, val);
349 }
350 return *this;
351 }
352
353 OutputDevice& writeTime(const SumoXMLAttr attr, const SUMOTime val) {
354 myFormatter->writeTime(getOStream(), attr, val);
355 return *this;
356 }
357
363 OutputDevice& writePreformattedTag(const std::string& val) {
365 return *this;
366 }
367
369 OutputDevice& writePadding(const std::string& val) {
371 return *this;
372 }
373
380 void inform(const std::string& msg, const bool progress = false);
381
382
386 template <class T>
388 getOStream() << t;
390 return *this;
391 }
392
393 void flush() {
394 getOStream().flush();
395 }
396
397 bool wroteHeader() const {
398 return myFormatter->wroteHeader();
399 }
400
401 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth = 2) {
402 myFormatter->setExpectedAttributes(expected, depth);
403 }
404
405protected:
407 virtual std::ostream& getOStream() = 0;
408
413 virtual void postWriteHook();
414
415
416private:
418 static std::map<std::string, OutputDevice*> myOutputDevices;
419
421 static int myPrevConsoleCP;
422
423protected:
424 const std::string myFilename;
425
427
430
431private:
433 OutputDevice(const OutputDevice&) = delete;
434
437
438};
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.
virtual ~OutputDevice()
Destructor.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
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.
static const SumoXMLAttrMask parseWrittenAttributes(const std::vector< std::string > &attrList, const std::string &desc, const std::map< std::string, SumoXMLAttrMask > &special=std::map< std::string, SumoXMLAttrMask >())
Parses a list of strings for attribute names and sets the relevant bits in the returned mask.
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.
bool wroteHeader() const
virtual void postWriteHook()
Called after every write access.
static void flushAll()
void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth=2)
OutputDevice & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
const std::string & getFilename()
get filename or suitable description of this device
OutputFormatter * myFormatter
The formatter for XML, CSV or Parquet.
virtual bool isNull()
returns the information whether the device will discard all output
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
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
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="", const int maximumDepth=2)
Creates the device using the output definition stored in the named option.
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.
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, const SumoXMLAttrMask &attributeMask, const bool isNull=false)
writes a named attribute unless filtered
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
OutputDevice & writeFuncAttr(const SumoXMLAttr attr, const Func &valFunc, const SumoXMLAttrMask &attributeMask, const bool isNull=false)
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.
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
OutputFormatterType getType()
Returns the type of formatter being used.
virtual void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth=2)
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)
writes an arbitrary attribute