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-2024 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 "PlainXMLFormatter.h"
32
33
34// ===========================================================================
35// class definitions
36// ===========================================================================
62public:
65
77 static OutputDevice& getDevice(const std::string& name, bool usePrefix = true);
78
79
97 static bool createDeviceByOption(const std::string& optionName,
98 const std::string& rootElement = "",
99 const std::string& schemaFile = "");
100
101
114 static OutputDevice& getDeviceByOption(const std::string& name);
115
118 static void flushAll();
119
122 static void closeAll(bool keepErrorRetrievers = false);
124
125
132 static std::string realString(const double v, const int precision = gPrecision);
133
134
135public:
138
140 OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
141
142
144 virtual ~OutputDevice();
145
146
150 virtual bool ok();
151
155 virtual bool isNull() {
156 return false;
157 }
158
160 const std::string& getFilename();
161
164 void close();
165
166
171
173 int precision();
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
197
198 template <typename E>
199 bool writeHeader(const SumoXMLTag& rootElement) {
200 return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
201 }
202
203
213 OutputDevice& openTag(const std::string& xmlElement);
214
215
223 OutputDevice& openTag(const SumoXMLTag& xmlElement);
224
225
236 bool closeTag(const std::string& comment = "");
237
238
239
242 void lf() {
243 getOStream() << "\n";
244 }
245
246
253 template <typename T>
254 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
256 return *this;
257 }
258
259 inline bool useAttribute(const SumoXMLAttr attr, SumoXMLAttrMask attributeMask) const {
260 return attributeMask.test(attr);
261 }
262
270 template <typename T>
271 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, long long int attributeMask) {
272 assert((int)attr <= 63);
273 if (attributeMask == 0 || useAttribute(attr, attributeMask)) {
275 }
276 return *this;
277 }
278 template <typename T>
279 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, SumoXMLAttrMask attributeMask) {
280 assert((int)attr <= (int)attributeMask.size());
281 if (attributeMask.none() || useAttribute(attr, attributeMask)) {
283 }
284 return *this;
285 }
286
287
294 template <typename T>
295 OutputDevice& writeAttr(const std::string& attr, const T& val) {
297 return *this;
298 }
299
306 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
307 if (val != "" && val != "default") {
308 writeAttr(attr, val);
309 }
310 return *this;
311 }
312
313
319 OutputDevice& writePreformattedTag(const std::string& val) {
321 return *this;
322 }
323
325 OutputDevice& writePadding(const std::string& val) {
327 return *this;
328 }
329
336 void inform(const std::string& msg, const bool progress = false);
337
338
342 template <class T>
344 getOStream() << t;
346 return *this;
347 }
348
349 void flush() {
350 getOStream().flush();
351 }
352
353 bool wroteHeader() const {
354 return myFormatter->wroteHeader();
355 }
356
357protected:
359 virtual std::ostream& getOStream() = 0;
360
361
366 virtual void postWriteHook();
367
368
369private:
371 static std::map<std::string, OutputDevice*> myOutputDevices;
372
374 static int myPrevConsoleCP;
375
376protected:
377 const std::string myFilename;
378
379private:
382
383private:
385 OutputDevice(const OutputDevice&) = delete;
386
389
390};
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:26
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
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
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 & 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 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.
int precision()
return precision set on the device
const std::string & getFilename()
get filename or suitable description of this device
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)
bool writeHeader(const SumoXMLTag &rootElement)
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
OutputFormatter *const myFormatter
The formatter for XML.
bool useAttribute(const SumoXMLAttr attr, SumoXMLAttrMask attributeMask) const
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 & writeOptionalAttr(const SumoXMLAttr attr, const T &val, SumoXMLAttrMask attributeMask)
virtual bool ok()
returns the information whether one can write into the device
OutputDevice & writeOptionalAttr(const SumoXMLAttr attr, const T &val, long long int attributeMask)
writes a named attribute unless filtered
Abstract base class for output formatters.
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
virtual void writePadding(std::ostream &into, const std::string &val)=0
virtual bool wroteHeader() const =0
Output formatter for plain XML output.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute