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() {
233 getOStream() << "\n";
234 }
235 }
236
247 template <typename T, class ATTR_TYPE>
248 OutputDevice& writeAttr(const ATTR_TYPE& attr, const T& val, const bool isNull = false, const bool escape = false) {
250 PlainXMLFormatter::writeAttr(getOStream(), attr, val, escape);
251#ifdef HAVE_PARQUET
252 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
253 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull, escape);
254#endif
255 } else {
256 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull, escape);
257 }
258 return *this;
259 }
260
270 static const SumoXMLAttrMask parseWrittenAttributes(const std::vector<std::string>& attrList, const std::string& desc,
271 const std::map<std::string, SumoXMLAttrMask>& special = std::map<std::string, SumoXMLAttrMask>());
272
283 template <typename T>
284 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const bool isNull = false, const bool escape = false) {
286 return *this;
287 }
288 return writeAttr(attr, val, isNull, escape);
289 }
290
300 template <typename T>
301 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const SumoXMLAttrMask& attributeMask,
302 const bool isNull = false, const bool escape = false) {
303 assert(attributeMask.none() || (int)attr <= (int)attributeMask.size());
304 if (attributeMask.none() || attributeMask.test(attr)) {
305 return writeOptionalAttr(attr, val, isNull, escape);
306 }
307 return *this;
308 }
309
322 template <typename Func>
323 OutputDevice& writeFuncAttr(const SumoXMLAttr attr, const Func& valFunc, const SumoXMLAttrMask& attributeMask,
324 const bool isNull = false, const bool escape = false) {
325 assert((int)attr <= (int)attributeMask.size());
326 if (attributeMask.none() || attributeMask.test(attr)) {
328 if (!isNull) {
329 PlainXMLFormatter::writeAttr(getOStream(), attr, valFunc(), escape);
330 }
331#ifdef HAVE_PARQUET
332 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
333 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull, escape);
334#endif
335 } else {
336 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull, escape);
337 }
338 }
339 return *this;
340 }
341
348 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
349 if (val != "" && val != "default") {
350 writeAttr(attr, val);
351 }
352 return *this;
353 }
354
355 OutputDevice& writeTime(const SumoXMLAttr attr, const SUMOTime val) {
356 myFormatter->writeTime(getOStream(), attr, val);
357 return *this;
358 }
359
365 OutputDevice& writePreformattedTag(const std::string& val) {
367 return *this;
368 }
369
371 OutputDevice& writePadding(const std::string& val) {
373 return *this;
374 }
375
382 void inform(const std::string& msg, const bool progress = false);
383
384
388 template <class T>
390 getOStream() << t;
392 return *this;
393 }
394
395 void flush() {
396 getOStream().flush();
397 }
398
399 bool wroteHeader() const {
400 return myFormatter->wroteHeader();
401 }
402
403 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth) {
404 myFormatter->setExpectedAttributes(expected, depth);
405 }
406
407protected:
409 virtual std::ostream& getOStream() = 0;
410
415 virtual void postWriteHook();
416
417
418private:
420 static std::map<std::string, OutputDevice*> myOutputDevices;
421
423 static int myPrevConsoleCP;
424
425protected:
426 const std::string myFilename;
427
429
432
433private:
435 OutputDevice(const OutputDevice&) = delete;
436
439
440};
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.
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
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.
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()
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
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.
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