SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OutputDevice.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Static storage of an output device and its base (abstract) implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
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 SUMOReal v, const int precision = OUTPUT_ACCURACY);
139 
140 
141 public:
144 
146  OutputDevice(const bool binary = false, const unsigned int defaultIndentation = 0);
147 
148 
150  virtual ~OutputDevice();
151 
152 
156  virtual bool ok();
157 
158 
161  void close();
162 
163 
167  void setPrecision(unsigned int precision = OUTPUT_ACCURACY);
168 
169 
182  bool writeXMLHeader(const std::string& rootElement,
183  const std::string& attrs = "",
184  const std::string& comment = "");
185 
186 
187  template <typename E>
188  bool writeHeader(const SumoXMLTag& rootElement) {
189  if (myAmBinary) {
190  return static_cast<BinaryFormatter*>(myFormatter)->writeHeader<E>(getOStream(), rootElement);
191  }
192  return static_cast<PlainXMLFormatter*>(myFormatter)->writeHeader(getOStream(), rootElement);
193  }
194 
195 
205  OutputDevice& openTag(const std::string& xmlElement);
206 
207 
215  OutputDevice& openTag(const SumoXMLTag& xmlElement);
216 
217 
228  bool closeTag();
229 
230 
231 
234  void lf() {
235  if (!myAmBinary) {
236  getOStream() << "\n";
237  }
238  }
239 
240 
244  bool isBinary() const {
245  return myAmBinary;
246  }
247 
248 
255  template <typename T>
256  OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) {
257  if (myAmBinary) {
259  } else {
261  }
262  return *this;
263  }
264 
265 
272  template <typename T>
273  OutputDevice& writeAttr(const std::string& attr, const T& val) {
274  if (myAmBinary) {
276  } else {
278  }
279  return *this;
280  }
281 
282 
289  OutputDevice& writeNonEmptyAttr(const SumoXMLAttr attr, const std::string& val) {
290  if (val != "" && val != "default") {
291  writeAttr(attr, val);
292  }
293  return *this;
294  }
295 
296 
303  void inform(const std::string& msg, const char progress = 0);
304 
305 
309  template <class T>
310  OutputDevice& operator<<(const T& t) {
311  getOStream() << t;
312  postWriteHook();
313  return *this;
314  }
315 
316 protected:
318  virtual std::ostream& getOStream() = 0;
319 
320 
325  virtual void postWriteHook();
326 
327 
328 private:
330  static std::map<std::string, OutputDevice*> myOutputDevices;
331 
332 
333 private:
336 
337  const bool myAmBinary;
338 
339 public:
341  OutputDevice(const OutputDevice&);
342 
343 private:
344 
347 
348 };
349 
350 
351 #endif
352 
353 /****************************************************************************/
354 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:330
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
OutputDevice(const bool binary=false, const unsigned int defaultIndentation=0)
Constructor.
#define OUTPUT_ACCURACY
Definition: config.h:162
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:188
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
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:289
static void closeAll()
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:273
Output formatter for plain XML output.
OutputDevice & operator<<(const T &t)
Abstract output operator.
Definition: OutputDevice.h:310
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:337
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:335
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.
#define SUMOReal
Definition: config.h:215
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:234
bool isBinary() const
Returns whether we have a binary output.
Definition: OutputDevice.h:244