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.sourceforge.net/
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  void writeAttr(std::ostream& into, const std::string& attr, const std::string& val);
186 
187 
194  template <typename dummy, typename T>
195  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
196  /* we need to use dummy templating here to compile those functions where they get
197  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
198  template <typename dummy>
199  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
200  template <typename dummy>
201  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
202 
203 
204 private:
211  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type) {
212  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
213  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
214  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
215  }
216 
217 
223  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
224 
225 
231  static void writePosition(std::ostream& into, const Position& val);
232 
233 
234 private:
236  std::vector<SumoXMLTag> myXMLStack;
237 
238 
239 };
240 
241 
242 template <typename E>
243 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
244  if (myXMLStack.empty()) {
246  FileHelpers::writeByte(into, 1);
249  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
250  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
253 
254  const unsigned int numEdges = (const unsigned int)E::dictSize();
256  FileHelpers::writeInt(into, numEdges);
257  for (unsigned int i = 0; i < numEdges; i++) {
259  FileHelpers::writeString(into, E::dictionary(i)->getID());
260  }
262  FileHelpers::writeInt(into, numEdges);
263  for (unsigned int i = 0; i < numEdges; i++) {
264  E* e = E::dictionary(i);
266  FileHelpers::writeInt(into, e->getNoFollowing());
267  for (unsigned int j = 0; j < e->getNoFollowing(); j++) {
269  FileHelpers::writeInt(into, e->getFollower(j)->getNumericalID());
270  }
271  }
272  openTag(into, rootElement);
273  return true;
274  }
275  return false;
276 }
277 
278 
279 template <typename dummy, typename T>
280 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
282  FileHelpers::writeString(into, toString(val, into.precision()));
283 }
284 
285 
286 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
287 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val);
288 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
289 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val);
290 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
291 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
292 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
293 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
294 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
295 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
296 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
297 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<SUMOReal>& val);
298 
299 
300 template <typename dummy>
301 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
303  FileHelpers::writeEdgeVector(into, val);
304 }
305 
306 
307 template <typename dummy>
308 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
310  FileHelpers::writeEdgeVector(into, val);
311 }
312 
313 #endif
314 
315 /****************************************************************************/
316