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-2012 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>
40 #include "OutputFormatter.h"
41 
42 
43 // ===========================================================================
44 // class declarations
45 // ===========================================================================
46 class Position;
47 class PositionVector;
48 class Boundary;
49 class RGBColor;
50 class ROEdge;
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
63 public:
65  enum DataType {
106  };
107 
109  BinaryFormatter();
110 
111 
113  virtual ~BinaryFormatter() { }
114 
115 
129  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
130  const std::string xmlParams = "",
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  void closeOpener(std::ostream& into);
177 
178 
186  bool closeTag(std::ostream& into, bool abbreviated = false);
187 
188 
195  void writeAttr(std::ostream& into, const std::string& attr, const std::string& val);
196 
197 
204  template <typename T, typename S>
205  static void writeAttr(S& into, const SumoXMLAttr attr, const T& val);
206 
207 
214  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
215 
216 
223  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val);
224 
225 
232  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
233 
234 
241  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val);
242 
243 
250  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
251 
252 
259  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
260 
261 
268  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
269 
270 
277  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
278 
279 
286  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
287 
288 
295  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
296 
297 
304  template <typename S>
305  static void writeAttr(S& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
306 
307 
308 private:
315  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type) {
316  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
317  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
318  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
319  }
320 
321 
327  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
328 
329 
335  static void writePosition(std::ostream& into, const Position& val);
336 
337 
338 private:
340  std::vector<SumoXMLTag> myXMLStack;
341 
342 
343 };
344 
345 
346 template <typename E>
347 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
348  if (myXMLStack.empty()) {
350  FileHelpers::writeByte(into, 1);
353  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
354  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
357 
358  const unsigned int numEdges = (const unsigned int)E::dictSize();
360  FileHelpers::writeInt(into, numEdges);
361  for (unsigned int i = 0; i < numEdges; i++) {
363  FileHelpers::writeString(into, E::dictionary(i)->getID());
364  }
366  FileHelpers::writeInt(into, numEdges);
367  for (unsigned int i = 0; i < numEdges; i++) {
368  E* e = E::dictionary(i);
370  FileHelpers::writeInt(into, e->getNoFollowing());
371  for (unsigned int j = 0; j < e->getNoFollowing(); j++) {
373  FileHelpers::writeInt(into, e->getFollower(j)->getNumericalID());
374  }
375  }
376  openTag(into, rootElement);
377  return true;
378  }
379  return false;
380 }
381 
382 
383 template <typename T, typename S>
384 void BinaryFormatter::writeAttr(S& into, const SumoXMLAttr attr, const T& val) {
386  FileHelpers::writeString(into, toString(val, into.precision()));
387 }
388 
389 
390 template <typename S>
391 void BinaryFormatter::writeAttr(S& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
393  FileHelpers::writeEdgeVector(into, val);
394 }
395 
396 
397 #endif
398 
399 /****************************************************************************/
400