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-2025 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
100 static bool createDeviceByOption(const std::string& optionName,
101 const std::string& rootElement = "",
102 const std::string& schemaFile = "");
103
104
117 static OutputDevice& getDeviceByOption(const std::string& name);
118
121 static void flushAll();
122
125 static void closeAll(bool keepErrorRetrievers = false);
127
128public:
131
133 OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
134
135
137 virtual ~OutputDevice();
138
139
143 virtual bool ok();
144
148 virtual bool isNull() {
149 return false;
150 }
151
153 const std::string& getFilename();
154
157 void close();
158
159 void setFormatter(OutputFormatter* formatter) {
160 delete myFormatter;
161 myFormatter = formatter;
162 }
163
167 void setPrecision(int precision = gPrecision);
168
172 return (int)getOStream().precision();
173 }
174
186 bool writeXMLHeader(const std::string& rootElement,
187 const std::string& schemaFile,
188 std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>(),
189 bool includeConfig = true);
190
200 OutputDevice& openTag(const std::string& xmlElement);
201
209 OutputDevice& openTag(const SumoXMLTag& xmlElement);
210
221 bool closeTag(const std::string& comment = "");
222
225 void lf() {
226 getOStream() << "\n";
227 }
228
235 template <typename T>
236 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
239#ifdef HAVE_PARQUET
240 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
241 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
242#endif
243 } else {
244 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
245 }
246 return *this;
247 }
248
258 static const SumoXMLAttrMask parseWrittenAttributes(const std::vector<std::string>& attrList, const std::string& desc,
259 const std::map<std::string, SumoXMLAttrMask>& special = std::map<std::string, SumoXMLAttrMask>());
260
268 template <typename T>
269 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, const SumoXMLAttrMask& attributeMask, const bool isNull = false) {
270 assert((int)attr <= (int)attributeMask.size());
271 if (attributeMask.none() || attributeMask.test(attr)) {
273 if (!isNull) {
275 }
276#ifdef HAVE_PARQUET
277 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
278 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val, isNull);
279#endif
280 } else {
281 if (isNull) {
282 static_cast<CSVFormatter*>(myFormatter)->writeNull(getOStream(), attr);
283 } else {
284 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
285 }
286 }
287 }
288 return *this;
289 }
290
291 template <typename Func>
292 OutputDevice& writeFuncAttr(const SumoXMLAttr attr, const Func& valFunc, const SumoXMLAttrMask& attributeMask, const bool isNull = false) {
293 assert((int)attr <= (int)attributeMask.size());
294 if (attributeMask.none() || attributeMask.test(attr)) {
296 if (!isNull) {
297 PlainXMLFormatter::writeAttr(getOStream(), attr, valFunc());
298 }
299#ifdef HAVE_PARQUET
300 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
301 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc(), isNull);
302#endif
303 } else {
304 if (isNull) {
305 static_cast<CSVFormatter*>(myFormatter)->writeNull(getOStream(), attr);
306 } else {
307 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, valFunc());
308 }
309 }
310 }
311 return *this;
312 }
313
320 template <typename T>
321 OutputDevice& writeAttr(const std::string& attr, const T& val) {
324#ifdef HAVE_PARQUET
325 } else if (myFormatter->getType() == OutputFormatterType::PARQUET) {
326 static_cast<ParquetFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
327#endif
328 } else {
329 static_cast<CSVFormatter*>(myFormatter)->writeAttr(getOStream(), attr, val);
330 }
331 return *this;
332 }
333
340 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
341 if (val != "" && val != "default") {
342 writeAttr(attr, val);
343 }
344 return *this;
345 }
346
347 OutputDevice& writeTime(const SumoXMLAttr attr, const SUMOTime val) {
348 myFormatter->writeTime(getOStream(), attr, val);
349 return *this;
350 }
351
357 OutputDevice& writePreformattedTag(const std::string& val) {
359 return *this;
360 }
361
363 OutputDevice& writePadding(const std::string& val) {
365 return *this;
366 }
367
374 void inform(const std::string& msg, const bool progress = false);
375
376
380 template <class T>
382 getOStream() << t;
384 return *this;
385 }
386
387 void flush() {
388 getOStream().flush();
389 }
390
391 bool wroteHeader() const {
392 return myFormatter->wroteHeader();
393 }
394
395 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth = 2) {
396 myFormatter->setExpectedAttributes(expected, depth);
397 }
398
399protected:
401 virtual std::ostream& getOStream() = 0;
402
407 virtual void postWriteHook();
408
409
410private:
412 static std::map<std::string, OutputDevice*> myOutputDevices;
413
415 static int myPrevConsoleCP;
416
417protected:
418 const std::string myFilename;
419
421
424
425private:
427 OutputDevice(const OutputDevice&) = delete;
428
431
432};
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.
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
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.
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
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