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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Static storage of an output device and its base (abstract) implementation
21 /****************************************************************************/
22 #ifndef OutputDevice_h
23 #define OutputDevice_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <map>
37 #include <utils/common/ToString.h>
39 #include "PlainXMLFormatter.h"
40 #include "BinaryFormatter.h"
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
70 class OutputDevice {
71 public:
74 
86  static OutputDevice& getDevice(const std::string& name);
87 
88 
106  static bool createDeviceByOption(const std::string& optionName,
107  const std::string& rootElement = "",
108  const std::string& schemaFile = "");
109 
110 
123  static OutputDevice& getDeviceByOption(const std::string& name);
124 
125 
128  static void closeAll();
130 
131 
138  static std::string realString(const double v, const int precision = gPrecision);
139 
140 
141 public:
144 
146  OutputDevice(const bool binary = false, const int defaultIndentation = 0);
147 
148 
150  virtual ~OutputDevice();
151 
152 
156  virtual bool ok();
157 
158 
161  void close();
162 
163 
167  void setPrecision(int precision = gPrecision);
168 
171  int getPrecision() {
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 
190 
191  template <typename E>
192  bool writeHeader(const SumoXMLTag& rootElement) {
193  if (myAmBinary) {
194  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
195  }
196  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
197  }
198 
199 
209  OutputDevice& openTag(const std::string& xmlElement);
210 
211 
219  OutputDevice& openTag(const SumoXMLTag& xmlElement);
220 
221 
232  bool closeTag();
233 
234 
235 
238  void lf() {
239  if (!myAmBinary) {
240  getOStream() << "\n";
241  }
242  }
243 
244 
248  bool isBinary() const {
249  return myAmBinary;
250  }
251 
252 
259  template <typename T>
260  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
261  if (myAmBinary) {
263  } else {
265  }
266  return *this;
267  }
268 
269 
276  template <typename T>
277  OutputDevice& writeAttr(const std::string& attr, const T& val) {
278  if (myAmBinary) {
280  } else {
282  }
283  return *this;
284  }
285 
286 
293  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
294  if (val != "" && val != "default") {
295  writeAttr(attr, val);
296  }
297  return *this;
298  }
299 
300 
306  OutputDevice& writePreformattedTag(const std::string& val) {
308  return *this;
309  }
310 
311 
318  void inform(const std::string& msg, const char progress = 0);
319 
320 
324  template <class T>
325  OutputDevice& operator<<(const T& t) {
326  getOStream() << t;
327  postWriteHook();
328  return *this;
329  }
330 
331 protected:
333  virtual std::ostream& getOStream() = 0;
334 
335 
340  virtual void postWriteHook();
341 
342 
343 private:
345  static std::map<std::string, OutputDevice*> myOutputDevices;
346 
347 
348 private:
351 
352  const bool myAmBinary;
353 
354 public:
356  OutputDevice(const OutputDevice&);
357 
358 private:
359 
362 
363 };
364 
365 
366 #endif
367 
368 /****************************************************************************/
369 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
void close()
Closes the device and removes it from the dictionary.
SumoXMLTag
Numbers representing SUMO-XML - element names.
Abstract base class for output formatters.
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:345
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:29
int getPrecision()
Returns the precison of the underlying stream.
Definition: OutputDevice.h:171
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...
Definition: OutputDevice.h:306
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:192
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
Output formatter for plain XML output.
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:293
static void closeAll()
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
OutputDevice(const bool binary=false, const int defaultIndentation=0)
Constructor.
virtual bool ok()
returns the information whether one can write into the device
OutputDevice & writeAttr(const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: OutputDevice.h:277
Output formatter for plain XML output.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:325
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
virtual ~OutputDevice()
Destructor.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
const bool myAmBinary
Definition: OutputDevice.h:352
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:350
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.
OutputDevice & operator=(const OutputDevice &)
Invalidated assignment operator.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:248
virtual std::ostream & getOStream()=0
Returns the associated ostream.
virtual void postWriteHook()
Called after every write access.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:238