SUMO - Simulation of Urban MObility
OutputDevice.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // Static storage of an output device and its base (abstract) implementation
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2004-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 #ifndef OutputDevice_h
24 #define OutputDevice_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <string>
37 #include <map>
38 #include <utils/common/ToString.h>
40 #include "PlainXMLFormatter.h"
41 #include "BinaryFormatter.h"
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
71 class OutputDevice {
72 public:
75 
87  static OutputDevice& getDevice(const std::string& name);
88 
89 
107  static bool createDeviceByOption(const std::string& optionName,
108  const std::string& rootElement = "",
109  const std::string& schemaFile = "");
110 
111 
124  static OutputDevice& getDeviceByOption(const std::string& name);
125 
126 
129  static void closeAll();
131 
132 
139  static std::string realString(const SUMOReal v, const int precision = OUTPUT_ACCURACY);
140 
141 
142 public:
145 
147  OutputDevice(const bool binary = false, const int defaultIndentation = 0);
148 
149 
151  virtual ~OutputDevice();
152 
153 
157  virtual bool ok();
158 
159 
162  void close();
163 
164 
168  void setPrecision(int precision = OUTPUT_ACCURACY);
169 
170 
183  bool writeXMLHeader(const std::string& rootElement,
184  const std::string& attrs = "",
185  const std::string& comment = "");
186 
187 
188  template <typename E>
189  bool writeHeader(const SumoXMLTag& rootElement) {
190  if (myAmBinary) {
191  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
192  }
193  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
194  }
195 
196 
206  OutputDevice& openTag(const std::string& xmlElement);
207 
208 
216  OutputDevice& openTag(const SumoXMLTag& xmlElement);
217 
218 
229  bool closeTag();
230 
231 
232 
235  void lf() {
236  if (!myAmBinary) {
237  getOStream() << "\n";
238  }
239  }
240 
241 
245  bool isBinary() const {
246  return myAmBinary;
247  }
248 
249 
256  template <typename T>
257  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
258  if (myAmBinary) {
260  } else {
262  }
263  return *this;
264  }
265 
266 
273  template <typename T>
274  OutputDevice& writeAttr(const std::string& attr, const T& val) {
275  if (myAmBinary) {
277  } else {
279  }
280  return *this;
281  }
282 
283 
290  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
291  if (val != "" && val != "default") {
292  writeAttr(attr, val);
293  }
294  return *this;
295  }
296 
297 
303  OutputDevice& writePreformattedTag(const std::string& val) {
305  return *this;
306  }
307 
308 
315  void inform(const std::string& msg, const char progress = 0);
316 
317 
321  template <class T>
322  OutputDevice& operator<<(const T& t) {
323  getOStream() << t;
324  postWriteHook();
325  return *this;
326  }
327 
328 protected:
330  virtual std::ostream& getOStream() = 0;
331 
332 
337  virtual void postWriteHook();
338 
339 
340 private:
342  static std::map<std::string, OutputDevice*> myOutputDevices;
343 
344 
345 private:
348 
349  const bool myAmBinary;
350 
351 public:
353  OutputDevice(const OutputDevice&);
354 
355 private:
356 
359 
360 };
361 
362 
363 #endif
364 
365 /****************************************************************************/
366 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
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:342
virtual void writePreformattedTag(std::ostream &into, const std::string &val)=0
OutputDevice & writePreformattedTag(const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...
Definition: OutputDevice.h:303
void setPrecision(int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
#define OUTPUT_ACCURACY
Definition: config.h:163
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:189
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:290
static void closeAll()
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:274
Output formatter for plain XML output.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:322
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.
static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY)
Helper method for string formatting.
const bool myAmBinary
Definition: OutputDevice.h:349
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:347
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:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:245
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:235