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 
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.
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:74
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:53
A basic edge for routing applications.
Definition: ROEdge.h:69
#define VERSION_STRING
Definition: config.h:227
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:215
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:223
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.