SUMO - Simulation of Urban MObility
BinaryFormatter.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Output formatter for plain XML output
20 /****************************************************************************/
21 #ifndef BinaryFormatter_h
22 #define BinaryFormatter_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <vector>
36 #include <utils/common/ToString.h>
38 #include "OutputFormatter.h"
39 
40 
41 // ===========================================================================
42 // class declarations
43 // ===========================================================================
44 class Position;
45 class PositionVector;
46 class Boundary;
47 class RGBColor;
48 class ROEdge;
49 class MSEdge;
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
62 public:
64  enum DataType {
107  };
108 
110  BinaryFormatter();
111 
112 
114  virtual ~BinaryFormatter() { }
115 
116 
127  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
128  const std::map<SumoXMLAttr, std::string>& attrs);
129 
130 
139  template <typename E>
140  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
141 
142 
153  void openTag(std::ostream& into, const std::string& xmlElement);
154 
155 
163  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
164 
165 
172  bool closeTag(std::ostream& into);
173 
174 
181  template <typename dummy, typename T>
182  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
183 
184 
191  template <typename dummy, typename T>
192  static void writeAttr(dummy& into, const std::string& attr, const T& val);
193 
194 
200  void writePreformattedTag(std::ostream& into, const std::string& val) {
201  FileHelpers::writeString(into, val);
202  }
203 
204 
205 
206  /* we need to use dummy templating here to compile those functions where they get
207  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
208  template <typename dummy>
209  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
210  template <typename dummy>
211  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
212 
213 
214 private:
221  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type = BF_INVALID) {
222  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
223  const int attrNum = (int)attr;
224  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum % 256));
225  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum / 256));
226  if (type != BF_INVALID) {
227  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
228  }
229  }
230 
231 
238  static void writeStaticHeader(std::ostream& into);
239 
240 
246  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
247 
248 
254  static void writePosition(std::ostream& into, const Position& val);
255 
256 
257 private:
259  std::vector<SumoXMLTag> myXMLStack;
260 
261 
262 };
263 
264 
265 template <typename E>
266 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
267  if (myXMLStack.empty()) {
268  writeStaticHeader(into);
269  const int numEdges = (const int)E::getAllEdges().size();
271  FileHelpers::writeInt(into, numEdges);
272  for (int i = 0; i < numEdges; i++) {
274  FileHelpers::writeString(into, E::getAllEdges()[i]->getID());
275  }
277  FileHelpers::writeInt(into, numEdges);
278  for (int i = 0; i < numEdges; i++) {
279  E* e = E::getAllEdges()[i];
281  FileHelpers::writeInt(into, e->getNumSuccessors());
282  for (int j = 0; j < e->getNumSuccessors(); j++) {
284  FileHelpers::writeInt(into, e->getSuccessors()[j]->getNumericalID());
285  }
286  }
287  openTag(into, rootElement);
288  return true;
289  }
290  return false;
291 }
292 
293 
294 template <typename dummy, typename T>
295 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
297  FileHelpers::writeString(into, toString(val, into.precision()));
298 }
299 
300 
301 template <typename dummy, typename T>
302 void BinaryFormatter::writeAttr(dummy& into, const std::string& attr, const T& val) {
303  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
304  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
305  }
306 }
307 
308 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
309 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val);
310 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
311 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
312 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
313 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
314 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
315 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
316 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
317 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
318 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<double>& val);
319 
320 
321 template <typename dummy>
322 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
324  FileHelpers::writeEdgeVector(into, val);
325 }
326 
327 
328 template <typename dummy>
329 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
331  FileHelpers::writeEdgeVector(into, val);
332 }
333 
334 #endif
335 
336 /****************************************************************************/
337 
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.
Abstract base class for output formatters.
static void writeStaticHeader(std::ostream &into)
writes the part of the header which is always unchanged.
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:47
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
A road/street connecting two junctions.
Definition: MSEdge.h:80
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs)
Writes an XML header with optional configuration.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
A list of positions.
BinaryFormatter()
Constructor.
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
A basic edge for routing applications.
Definition: ROEdge.h:77
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.
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type=BF_INVALID)
writes the header for an arbitrary attribute
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:217
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.