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 /****************************************************************************/
9 // Output formatter for plain XML output
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2012-2014 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 #ifdef HAVE_VERSION_H
36 #include <version.h>
37 #endif
38 
39 #include <vector>
41 #include <utils/common/ToString.h>
43 #include "OutputFormatter.h"
44 
45 
46 // ===========================================================================
47 // class declarations
48 // ===========================================================================
49 class Position;
50 class PositionVector;
51 class Boundary;
52 class RGBColor;
53 class ROEdge;
54 class MSEdge;
55 
56 
57 // ===========================================================================
58 // class definitions
59 // ===========================================================================
67 public:
69  enum DataType {
110  };
111 
113  BinaryFormatter();
114 
115 
117  virtual ~BinaryFormatter() { }
118 
119 
132  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
133  const std::string& attrs = "",
134  const std::string& comment = "");
135 
136 
145  template <typename E>
146  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
147 
148 
159  void openTag(std::ostream& into, const std::string& xmlElement);
160 
161 
169  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
170 
171 
178  bool closeTag(std::ostream& into);
179 
180 
187  template <typename dummy, typename T>
188  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
189 
190 
197  template <typename dummy, typename T>
198  static void writeAttr(dummy& into, const std::string& attr, const T& val);
199 
200 
206  void writePreformattedTag(std::ostream& into, const std::string& val) {
207  FileHelpers::writeString(into, val);
208  }
209 
210 
211 
212  /* we need to use dummy templating here to compile those functions where they get
213  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
214  template <typename dummy>
215  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
216  template <typename dummy>
217  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
218 
219 
220 private:
227  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type) {
228  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
229  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
230  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
231  }
232 
233 
239  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
240 
241 
247  static void writePosition(std::ostream& into, const Position& val);
248 
249 
250 private:
252  std::vector<SumoXMLTag> myXMLStack;
253 
254 
255 };
256 
257 
258 template <typename E>
259 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
260  if (myXMLStack.empty()) {
262  FileHelpers::writeByte(into, 1);
265  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
266  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
269 
270  const unsigned int numEdges = (const unsigned int)E::dictSize();
272  FileHelpers::writeInt(into, numEdges);
273  for (unsigned int i = 0; i < numEdges; i++) {
275  FileHelpers::writeString(into, E::dictionary(i)->getID());
276  }
278  FileHelpers::writeInt(into, numEdges);
279  for (unsigned int i = 0; i < numEdges; i++) {
280  E* e = E::dictionary(i);
282  FileHelpers::writeInt(into, e->getNoFollowing());
283  for (unsigned int j = 0; j < e->getNoFollowing(); j++) {
285  FileHelpers::writeInt(into, e->getFollower(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 SUMOReal& 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 unsigned int& val);
313 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
314 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
315 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
316 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
317 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
318 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
319 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
320 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<SUMOReal>& val);
321 
322 
323 template <typename dummy>
324 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
326  FileHelpers::writeEdgeVector(into, val);
327 }
328 
329 
330 template <typename dummy>
331 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
333  FileHelpers::writeEdgeVector(into, val);
334 }
335 
336 #endif
337 
338 /****************************************************************************/
339