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-2023 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, long long int attributeMask) const {
260 return (attributeMask & ((long long int)1 << attr)) != 0;
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
279
286 template <typename T>
287 OutputDevice& writeAttr(const std::string& attr, const T& val) {
289 return *this;
290 }
291
298 OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
299 if (val != "" && val != "default") {
300 writeAttr(attr, val);
301 }
302 return *this;
303 }
304
305
311 OutputDevice& writePreformattedTag(const std::string& val) {
313 return *this;
314 }
315
317 OutputDevice& writePadding(const std::string& val) {
319 return *this;
320 }
321
328 void inform(const std::string& msg, const char progress = 0);
329
330
334 template <class T>
336 getOStream() << t;
338 return *this;
339 }
340
341 void flush() {
342 getOStream().flush();
343 }
344
345 bool wroteHeader() const {
346 return myFormatter->wroteHeader();
347 }
348
349protected:
351 virtual std::ostream& getOStream() = 0;
352
353
358 virtual void postWriteHook();
359
360
361private:
363 static std::map<std::string, OutputDevice*> myOutputDevices;
364
366 static int myPrevConsoleCP;
367
368protected:
369 const std::string myFilename;
370
371private:
374
375private:
377 OutputDevice(const OutputDevice&) = delete;
378
381
382};
SumoXMLTag
Numbers representing SUMO-XML - element names.
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.
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)
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
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.
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
bool useAttribute(const SumoXMLAttr attr, long long int attributeMask) const
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