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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Output formatter for plain XML output
18 /****************************************************************************/
19 #ifndef BinaryFormatter_h
20 #define BinaryFormatter_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <vector>
30 #include <utils/common/ToString.h>
32 #include "OutputFormatter.h"
33 
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class Position;
39 class PositionVector;
40 class Boundary;
41 class RGBColor;
42 class ROEdge;
43 class MSEdge;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
56 public:
58  enum DataType {
101  };
102 
104  BinaryFormatter();
105 
106 
108  virtual ~BinaryFormatter() { }
109 
110 
121  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
122  const std::map<SumoXMLAttr, std::string>& attrs);
123 
124 
133  template <typename E>
134  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
135 
136 
147  void openTag(std::ostream& into, const std::string& xmlElement);
148 
149 
157  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
158 
159 
166  bool closeTag(std::ostream& into, const std::string& comment = "");
167 
168 
175  template <typename dummy, typename T>
176  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
177 
178 
185  template <typename dummy, typename T>
186  static void writeAttr(dummy& into, const std::string& attr, const T& val);
187 
188 
194  void writePreformattedTag(std::ostream& into, const std::string& val) {
195  FileHelpers::writeString(into, val);
196  }
197 
199  void writePadding(std::ostream& /*into*/, const std::string&) { }
200 
201 
202  /* we need to use dummy templating here to compile those functions where they get
203  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
204  template <typename dummy>
205  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
206  template <typename dummy>
207  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
208 
209 
210 private:
217  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type = BF_INVALID) {
218  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
219  const int attrNum = (int)attr;
220  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum % 256));
221  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum / 256));
222  if (type != BF_INVALID) {
223  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
224  }
225  }
226 
227 
234  static void writeStaticHeader(std::ostream& into);
235 
236 
242  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
243 
244 
250  static void writePosition(std::ostream& into, const Position& val);
251 
252 
253 private:
255  std::vector<SumoXMLTag> myXMLStack;
256 
257 
258 };
259 
260 
261 template <typename E>
262 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
263  if (myXMLStack.empty()) {
264  writeStaticHeader(into);
265  const int numEdges = (const int)E::getAllEdges().size();
267  FileHelpers::writeInt(into, numEdges);
268  for (int i = 0; i < numEdges; i++) {
270  FileHelpers::writeString(into, E::getAllEdges()[i]->getID());
271  }
273  FileHelpers::writeInt(into, numEdges);
274  for (int i = 0; i < numEdges; i++) {
275  E* e = E::getAllEdges()[i];
277  FileHelpers::writeInt(into, e->getNumSuccessors());
278  for (int j = 0; j < e->getNumSuccessors(); j++) {
280  FileHelpers::writeInt(into, e->getSuccessors()[j]->getNumericalID());
281  }
282  }
283  openTag(into, rootElement);
284  return true;
285  }
286  return false;
287 }
288 
289 
290 template <typename dummy, typename T>
291 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
293  FileHelpers::writeString(into, toString(val, into.precision()));
294 }
295 
296 
297 template <typename dummy, typename T>
298 void BinaryFormatter::writeAttr(dummy& into, const std::string& attr, const T& val) {
299  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
300  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
301  }
302 }
303 
304 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
305 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val);
306 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
307 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
308 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
309 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
310 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
311 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
312 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
313 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
314 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<double>& val);
315 
316 
317 template <typename dummy>
318 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
320  FileHelpers::writeEdgeVector(into, val);
321 }
322 
323 
324 template <typename dummy>
325 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
327  FileHelpers::writeEdgeVector(into, val);
328 }
329 
330 #endif
331 
332 /****************************************************************************/
333 
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.
void writePadding(std::ostream &, const std::string &)
padding is ignored for binary output
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, const std::string &comment="")
Closes the most recently opened tag.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
A road/street connecting two junctions.
Definition: MSEdge.h:75
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:49
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
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:72
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:211
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.