SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinaryFormatter.h
Go to the documentation of this file.
1 /****************************************************************************/
7 // Output formatter for plain XML output
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 #ifndef BinaryFormatter_h
21 #define BinaryFormatter_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_VERSION_H
34 #include <version.h>
35 #endif
36 
37 #include <vector>
39 #include <utils/common/ToString.h>
41 #include "OutputFormatter.h"
42 
43 
44 // ===========================================================================
45 // class declarations
46 // ===========================================================================
47 class Position;
48 class PositionVector;
49 class Boundary;
50 class RGBColor;
51 class ROEdge;
52 class MSEdge;
53 
54 
55 // ===========================================================================
56 // class definitions
57 // ===========================================================================
65 public:
67  enum DataType {
108  };
109 
111  BinaryFormatter();
112 
113 
115  virtual ~BinaryFormatter() { }
116 
117 
130  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
131  const std::string& attrs = "",
132  const std::string& comment = "");
133 
134 
143  template <typename E>
144  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
145 
146 
157  void openTag(std::ostream& into, const std::string& xmlElement);
158 
159 
167  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
168 
169 
176  bool closeTag(std::ostream& into);
177 
178 
185  template <typename dummy, typename T>
186  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
187 
188 
195  template <typename dummy, typename T>
196  static void writeAttr(dummy& into, const std::string& attr, const T& val);
197 
198 
199  /* we need to use dummy templating here to compile those functions where they get
200  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
201  template <typename dummy>
202  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
203  template <typename dummy>
204  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
205 
206 
207 private:
214  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type) {
215  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
216  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
217  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
218  }
219 
220 
226  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
227 
228 
234  static void writePosition(std::ostream& into, const Position& val);
235 
236 
237 private:
239  std::vector<SumoXMLTag> myXMLStack;
240 
241 
242 };
243 
244 
245 template <typename E>
246 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
247  if (myXMLStack.empty()) {
249  FileHelpers::writeByte(into, 1);
252  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
253  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
256 
257  const unsigned int numEdges = (const unsigned int)E::dictSize();
259  FileHelpers::writeInt(into, numEdges);
260  for (unsigned int i = 0; i < numEdges; i++) {
262  FileHelpers::writeString(into, E::dictionary(i)->getID());
263  }
265  FileHelpers::writeInt(into, numEdges);
266  for (unsigned int i = 0; i < numEdges; i++) {
267  E* e = E::dictionary(i);
269  FileHelpers::writeInt(into, e->getNoFollowing());
270  for (unsigned int j = 0; j < e->getNoFollowing(); j++) {
272  FileHelpers::writeInt(into, e->getFollower(j)->getNumericalID());
273  }
274  }
275  openTag(into, rootElement);
276  return true;
277  }
278  return false;
279 }
280 
281 
282 template <typename dummy, typename T>
283 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
285  FileHelpers::writeString(into, toString(val, into.precision()));
286 }
287 
288 
289 template <typename dummy, typename T>
290 void BinaryFormatter::writeAttr(dummy& into, const std::string& attr, const T& val) {
291  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
292  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
293  }
294 }
295 
296 
297 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
298 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val);
299 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
300 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val);
301 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
302 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
303 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
304 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
305 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
306 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
307 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
308 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<SUMOReal>& val);
309 
310 
311 template <typename dummy>
312 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
314  FileHelpers::writeEdgeVector(into, val);
315 }
316 
317 
318 template <typename dummy>
319 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
321  FileHelpers::writeEdgeVector(into, val);
322 }
323 
324 #endif
325 
326 /****************************************************************************/
327 
bool writeHeader(std::ostream &into, const SumoXMLTag &rootElement)
Writes a header with optional edge list and connections.
SumoXMLTag
Numbers representing SUMO-XML - element names.
static StringBijection< SumoXMLNodeType > NodeTypes
Abstract base class for output formatters.
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:73
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:51
A basic edge for routing applications.
Definition: ROEdge.h:67
#define VERSION_STRING
Definition: config.h:233
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:221
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:222
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.