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 {
162 return myFormatter->getType() == OutputFormatterType::XML;
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
242 template <typename T, class ATTR_TYPE>
243 OutputDevice& writeAttr(const ATTR_TYPE& attr, const T& val, const bool isNull = false) {
244 if (myFormatter->getType() == OutputFormatterType::XML) {
246#ifdef HAVE_PARQUET
247 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
248 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull);
249#endif
250 } else {
251 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull);
252 }
253 return *this;
254 }
255
265 static const SumoXMLAttrMask parseWrittenAttributes(const std::vector<std::string>& attrList, const std::string& desc,
266 const std::map<std::string, SumoXMLAttrMask>& special = std::map<std::string, SumoXMLAttrMask>());
267
276 template <typename T>
277 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const SumoXMLAttrMask& attributeMask, const bool isNull = false) {
278 assert((int)attr <= (int)attributeMask.size());
279 if (attributeMask.none() || attributeMask.test(attr)) {
280 if (myFormatter->getType() == OutputFormatterType::XML) {
281 if (!isNull) {
283 }
284#ifdef HAVE_PARQUET
285 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
286 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull);
287#endif
288 } else {
289 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull);
290 }
291 }
292 return *this;
293 }
294
295 template <typename Func>
296 OutputDevice& writeFuncAttr(const SumoXMLAttr attr, const Func& valFunc, const SumoXMLAttrMask& attributeMask, const bool isNull = false) {
297 assert((int)attr <= (int)attributeMask.size());
298 if (attributeMask.none() || attributeMask.test(attr)) {
299 if (myFormatter->getType() == OutputFormatterType::XML) {
300 if (!isNull) {
301 PlainXMLFormatter::writeAttr(getOStream(), attr, valFunc());
302 }
303#ifdef HAVE_PARQUET
304 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
305 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull);
306#endif
307 } else {
308 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull);
309 }
310 }
311 return *this;
312 }
313
320 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
321 if (val != "" && val != "default") {
322 writeAttr(attr, val);
323 }
324 return *this;
325 }
326
327 OutputDevice& writeTime(const SumoXMLAttr attr, const SUMOTime val) {
328 myFormatter->writeTime(getOStream(), attr, val);
329 return *this;
330 }
331
337 OutputDevice& writePreformattedTag(const std::string& val) {
338 myFormatter->writePreformattedTag(getOStream(), val);
339 return *this;
340 }
341
343 OutputDevice& writePadding(const std::string& val) {
344 myFormatter->writePadding(getOStream(), val);
345 return *this;
346 }
347
354 void inform(const std::string& msg, const bool progress = false);
355
356
360 template <class T>
362 getOStream() << t;
364 return *this;
365 }
366
367 void flush() {
368 getOStream().flush();
369 }
370
371 bool wroteHeader() const {
372 return myFormatter->wroteHeader();
373 }
374
375 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth = 2) {
376 myFormatter->setExpectedAttributes(expected, depth);
377 }
378
379protected:
381 virtual std::ostream& getOStream() = 0;
382
387 virtual void postWriteHook();
388
389
390private:
392 static std::map<std::string, OutputDevice*> myOutputDevices;
393
395 static int myPrevConsoleCP;
396
397protected:
398 const std::string myFilename;
399
401
404
405private:
407 OutputDevice(const OutputDevice&) = delete;
408
411
412};
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(const int defaultIndentation=0, const std::string &filename="")
Constructor.
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()
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false)
writes a named attribute
void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth=2)
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.
Output formatter for Parquet output.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute