Eclipse SUMO - Simulation of Urban MObility
OutputDevice.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2004-2022 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
116
119 static void closeAll(bool keepErrorRetrievers = false);
121
122
129 static std::string realString(const double v, const int precision = gPrecision);
130
131
132public:
135
137 OutputDevice(const int defaultIndentation = 0, const std::string& filename = "");
138
139
141 virtual ~OutputDevice();
142
143
147 virtual bool ok();
148
152 virtual bool isNull() {
153 return false;
154 }
155
157 const std::string& getFilename();
158
161 void close();
162
163
168
170 int precision();
171
175 return (int)getOStream().precision();
176 }
177
189 bool writeXMLHeader(const std::string& rootElement,
190 const std::string& schemaFile,
191 std::map<SumoXMLAttr, std::string> attrs = std::map<SumoXMLAttr, std::string>(),
192 bool includeConfig = true);
193
194
195 template <typename E>
196 bool writeHeader(const SumoXMLTag& rootElement) {
197 return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
198 }
199
200
210 OutputDevice& openTag(const std::string& xmlElement);
211
212
220 OutputDevice& openTag(const SumoXMLTag& xmlElement);
221
222
233 bool closeTag(const std::string& comment = "");
234
235
236
239 void lf() {
240 getOStream() << "\n";
241 }
242
243
250 template <typename T>
251 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
253 return *this;
254 }
255
256 inline bool useAttribute(const SumoXMLAttr attr, long long int attributeMask) const {
257 return (attributeMask & ((long long int)1 << attr)) != 0;
258 }
259
267 template <typename T>
268 OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, long long int attributeMask) {
269 assert((int)attr <= 63);
270 if (attributeMask == 0 || useAttribute(attr, attributeMask)) {
272 }
273 return *this;
274 }
275
276
283 template <typename T>
284 OutputDevice& writeAttr(const std::string& attr, const T& val) {
286 return *this;
287 }
288
295 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
296 if (val != "" && val != "default") {
297 writeAttr(attr, val);
298 }
299 return *this;
300 }
301
302
308 OutputDevice& writePreformattedTag(const std::string& val) {
310 return *this;
311 }
312
314 OutputDevice& writePadding(const std::string& val) {
316 return *this;
317 }
318
325 void inform(const std::string& msg, const char progress = 0);
326
327
331 template <class T>
333 getOStream() << t;
335 return *this;
336 }
337
338 void flush() {
339 getOStream().flush();
340 }
341
342 bool wroteHeader() const {
343 return myFormatter->wroteHeader();
344 }
345
346protected:
348 virtual std::ostream& getOStream() = 0;
349
350
355 virtual void postWriteHook();
356
357
358private:
360 static std::map<std::string, OutputDevice*> myOutputDevices;
361
363 static int myPrevConsoleCP;
364
365protected:
366 const std::string myFilename;
367
368private:
371
372private:
374 OutputDevice(const OutputDevice&) = delete;
375
378
379};
SumoXMLTag
Numbers representing SUMO-XML - element names.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:239
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 & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:251
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"
Definition: OutputDevice.h:295
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
Definition: OutputDevice.h:308
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
OutputDevice & writePadding(const std::string &val)
writes padding (ignored for binary output)
Definition: OutputDevice.h:314
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.
Definition: OutputDevice.h:332
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool wroteHeader() const
Definition: OutputDevice.h:342
virtual void postWriteHook()
Called after every write access.
OutputDevice & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: OutputDevice.h:284
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
Definition: OutputDevice.h:152
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
int getPrecision()
Returns the precision of the underlying stream.
Definition: OutputDevice.h:174
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
Definition: OutputDevice.h:366
static void closeAll(bool keepErrorRetrievers=false)
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:196
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
OutputFormatter *const myFormatter
The formatter for XML.
Definition: OutputDevice.h:370
static int myPrevConsoleCP
old console code page to restore after ending
Definition: OutputDevice.h:363
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
Definition: OutputDevice.h:360
bool useAttribute(const SumoXMLAttr attr, long long int attributeMask) const
Definition: OutputDevice.h:256
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
Definition: OutputDevice.h:268
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