SUMO - Simulation of Urban MObility
BinaryFormatter.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Output formatter for plain XML output
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2012-2015 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 BinaryFormatter_h
23 #define BinaryFormatter_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 <vector>
37 #include <utils/common/ToString.h>
39 #include "OutputFormatter.h"
40 
41 
42 // ===========================================================================
43 // class declarations
44 // ===========================================================================
45 class Position;
46 class PositionVector;
47 class Boundary;
48 class RGBColor;
49 class ROEdge;
50 class MSEdge;
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
63 public:
65  enum DataType {
106  };
107 
109  BinaryFormatter();
110 
111 
113  virtual ~BinaryFormatter() { }
114 
115 
128  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
129  const std::string& attrs = "",
130  const std::string& comment = "");
131 
132 
141  template <typename E>
142  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
143 
144 
155  void openTag(std::ostream& into, const std::string& xmlElement);
156 
157 
165  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
166 
167 
174  bool closeTag(std::ostream& into);
175 
176 
183  template <typename dummy, typename T>
184  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
185 
186 
193  template <typename dummy, typename T>
194  static void writeAttr(dummy& into, const std::string& attr, const T& val);
195 
196 
202  void writePreformattedTag(std::ostream& into, const std::string& val) {
203  FileHelpers::writeString(into, val);
204  }
205 
206 
207 
208  /* we need to use dummy templating here to compile those functions where they get
209  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
210  template <typename dummy>
211  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
212  template <typename dummy>
213  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
214 
215 
216 private:
223  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type) {
224  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
225  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
226  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
227  }
228 
229 
236  static void writeStaticHeader(std::ostream& into);
237 
238 
244  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
245 
246 
252  static void writePosition(std::ostream& into, const Position& val);
253 
254 
255 private:
257  std::vector<SumoXMLTag> myXMLStack;
258 
259 
260 };
261 
262 
263 template <typename E>
264 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
265  if (myXMLStack.empty()) {
266  writeStaticHeader(into);
267  const unsigned int numEdges = (const unsigned int)E::dictSize();
269  FileHelpers::writeInt(into, numEdges);
270  for (unsigned int i = 0; i < numEdges; i++) {
272  FileHelpers::writeString(into, E::dictionary(i)->getID());
273  }
275  FileHelpers::writeInt(into, numEdges);
276  for (unsigned int i = 0; i < numEdges; i++) {
277  E* e = E::dictionary(i);
279  FileHelpers::writeInt(into, e->getNumSuccessors());
280  for (unsigned int j = 0; j < e->getNumSuccessors(); j++) {
282  FileHelpers::writeInt(into, e->getSuccessors()[j]->getNumericalID());
283  }
284  }
285  openTag(into, rootElement);
286  return true;
287  }
288  return false;
289 }
290 
291 
292 template <typename dummy, typename T>
293 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
295  FileHelpers::writeString(into, toString(val, into.precision()));
296 }
297 
298 
299 template <typename dummy, typename T>
300 void BinaryFormatter::writeAttr(dummy& into, const std::string& attr, const T& val) {
301  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
302  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
303  }
304 }
305 
306 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
307 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val);
308 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
309 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val);
310 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
311 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
312 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
313 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
314 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
315 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
316 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
317 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<SUMOReal>& val);
318 
319 
320 template <typename dummy>
321 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
323  FileHelpers::writeEdgeVector(into, val);
324 }
325 
326 
327 template <typename dummy>
328 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
330  FileHelpers::writeEdgeVector(into, val);
331 }
332 
333 #endif
334 
335 /****************************************************************************/
336 
void writePreformattedTag(std::ostream &into, const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...
bool writeHeader(std::ostream &into, const SumoXMLTag &rootElement)
Writes a header with optional edge list and connections.
SumoXMLTag
Numbers representing SUMO-XML - element names.
Abstract base class for output formatters.
static void writeStaticHeader(std::ostream &into)
writes the part of the header which is always unchanged.
DataType
data types in binary output
bool closeTag(std::ostream &into)
Closes the most recently opened tag.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type)
writes the header for an arbitrary attribute
A road/street connecting two junctions.
Definition: MSEdge.h:81
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
BinaryFormatter()
Constructor.
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
A basic edge for routing applications.
Definition: ROEdge.h:73
std::vector< SumoXMLTag > myXMLStack
The stack of begun xml elements.
Output formatter for plain XML output.
static void writeStringList(std::ostream &into, const std::vector< std::string > &list)
writes a list of strings
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
virtual ~BinaryFormatter()
Destructor.
#define SUMOReal
Definition: config.h:214
static void writePosition(std::ostream &into, const Position &val)
writes a position
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:227
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.