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-2017 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 {
108  };
109 
111  BinaryFormatter();
112 
113 
115  virtual ~BinaryFormatter() { }
116 
117 
128  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
129  const std::map<SumoXMLAttr, std::string>& attrs);
130 
131 
140  template <typename E>
141  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
142 
143 
154  void openTag(std::ostream& into, const std::string& xmlElement);
155 
156 
164  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
165 
166 
173  bool closeTag(std::ostream& into);
174 
175 
182  template <typename dummy, typename T>
183  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
184 
185 
192  template <typename dummy, typename T>
193  static void writeAttr(dummy& into, const std::string& attr, const T& val);
194 
195 
201  void writePreformattedTag(std::ostream& into, const std::string& val) {
202  FileHelpers::writeString(into, val);
203  }
204 
205 
206 
207  /* we need to use dummy templating here to compile those functions where they get
208  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
209  template <typename dummy>
210  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
211  template <typename dummy>
212  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
213 
214 
215 private:
222  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type = BF_INVALID) {
223  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
224  const int attrNum = (int)attr;
225  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum % 256));
226  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum / 256));
227  if (type != BF_INVALID) {
228  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
229  }
230  }
231 
232 
239  static void writeStaticHeader(std::ostream& into);
240 
241 
247  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
248 
249 
255  static void writePosition(std::ostream& into, const Position& val);
256 
257 
258 private:
260  std::vector<SumoXMLTag> myXMLStack;
261 
262 
263 };
264 
265 
266 template <typename E>
267 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
268  if (myXMLStack.empty()) {
269  writeStaticHeader(into);
270  const int numEdges = (const int)E::getAllEdges().size();
272  FileHelpers::writeInt(into, numEdges);
273  for (int i = 0; i < numEdges; i++) {
275  FileHelpers::writeString(into, E::getAllEdges()[i]->getID());
276  }
278  FileHelpers::writeInt(into, numEdges);
279  for (int i = 0; i < numEdges; i++) {
280  E* e = E::getAllEdges()[i];
282  FileHelpers::writeInt(into, e->getNumSuccessors());
283  for (int j = 0; j < e->getNumSuccessors(); j++) {
285  FileHelpers::writeInt(into, e->getSuccessors()[j]->getNumericalID());
286  }
287  }
288  openTag(into, rootElement);
289  return true;
290  }
291  return false;
292 }
293 
294 
295 template <typename dummy, typename T>
296 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
298  FileHelpers::writeString(into, toString(val, into.precision()));
299 }
300 
301 
302 template <typename dummy, typename T>
303 void BinaryFormatter::writeAttr(dummy& into, const std::string& attr, const T& val) {
304  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
305  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
306  }
307 }
308 
309 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
310 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val);
311 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
312 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
313 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
314 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
315 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
316 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
317 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
318 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
319 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<double>& val);
320 
321 
322 template <typename dummy>
323 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
325  FileHelpers::writeEdgeVector(into, val);
326 }
327 
328 
329 template <typename dummy>
330 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
332  FileHelpers::writeEdgeVector(into, val);
333 }
334 
335 #endif
336 
337 /****************************************************************************/
338 
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.
A road/street connecting two junctions.
Definition: MSEdge.h:80
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs)
Writes an XML header with optional configuration.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
A basic edge for routing applications.
Definition: ROEdge.h:77
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.
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type=BF_INVALID)
writes the header for an arbitrary attribute
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:218
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.