Eclipse 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-2019 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 /****************************************************************************/
16 // Output formatter for plain XML output
17 /****************************************************************************/
18 #ifndef BinaryFormatter_h
19 #define BinaryFormatter_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
29 #include <utils/common/ToString.h>
31 #include "OutputFormatter.h"
32 
33 
34 // ===========================================================================
35 // class declarations
36 // ===========================================================================
37 class Position;
38 class PositionVector;
39 class Boundary;
40 class RGBColor;
41 class ROEdge;
42 class MSEdge;
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
55 public:
57  enum DataType {
100  };
101 
103  BinaryFormatter();
104 
105 
107  virtual ~BinaryFormatter() { }
108 
109 
120  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
121  const std::map<SumoXMLAttr, std::string>& attrs);
122 
123 
132  template <typename E>
133  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
134 
135 
146  void openTag(std::ostream& into, const std::string& xmlElement);
147 
148 
156  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
157 
158 
165  bool closeTag(std::ostream& into, const std::string& comment = "");
166 
167 
174  template <typename dummy, typename T>
175  static void writeAttr(dummy& into, const SumoXMLAttr attr, const T& val);
176 
177 
184  template <typename dummy, typename T>
185  static void writeAttr(dummy& into, const std::string& attr, const T& val);
186 
187 
193  void writePreformattedTag(std::ostream& into, const std::string& val) {
194  FileHelpers::writeString(into, val);
195  }
196 
198  void writePadding(std::ostream& /*into*/, const std::string&) { }
199 
200 
201  /* we need to use dummy templating here to compile those functions where they get
202  called to avoid an explicit dependency of utils/iodevices on the edge implementations */
203  template <typename dummy>
204  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val);
205  template <typename dummy>
206  static void writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val);
207 
208 
209 private:
216  static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type = BF_INVALID) {
217  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
218  const int attrNum = (int)attr;
219  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum % 256));
220  FileHelpers::writeByte(into, static_cast<unsigned char>(attrNum / 256));
221  if (type != BF_INVALID) {
222  FileHelpers::writeByte(into, static_cast<unsigned char>(type));
223  }
224  }
225 
226 
233  static void writeStaticHeader(std::ostream& into);
234 
235 
241  static void writeStringList(std::ostream& into, const std::vector<std::string>& list);
242 
243 
249  static void writePosition(std::ostream& into, const Position& val);
250 
251 
252 private:
254  std::vector<SumoXMLTag> myXMLStack;
255 
256 
257 };
258 
259 
260 template <typename E>
261 bool BinaryFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
262  if (myXMLStack.empty()) {
263  writeStaticHeader(into);
264  const int numEdges = (const int)E::getAllEdges().size();
266  FileHelpers::writeInt(into, numEdges);
267  for (int i = 0; i < numEdges; i++) {
269  FileHelpers::writeString(into, E::getAllEdges()[i]->getID());
270  }
272  FileHelpers::writeInt(into, numEdges);
273  for (int i = 0; i < numEdges; i++) {
274  E* e = E::getAllEdges()[i];
276  FileHelpers::writeInt(into, e->getNumSuccessors());
277  for (int j = 0; j < e->getNumSuccessors(); j++) {
279  FileHelpers::writeInt(into, e->getSuccessors()[j]->getNumericalID());
280  }
281  }
282  openTag(into, rootElement);
283  return true;
284  }
285  return false;
286 }
287 
288 
289 template <typename dummy, typename T>
290 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const T& val) {
292  FileHelpers::writeString(into, toString(val, into.precision()));
293 }
294 
295 
296 template <typename dummy, typename T>
297 void BinaryFormatter::writeAttr(dummy& into, const std::string& attr, const T& val) {
298  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
299  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
300  }
301 }
302 
303 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val);
304 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val);
305 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val);
306 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val);
307 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val);
308 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val);
309 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val);
310 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val);
311 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val);
312 template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val);
313 //template<> void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<double>& val);
314 
315 
316 template <typename dummy>
317 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const ROEdge*>& val) {
319  FileHelpers::writeEdgeVector(into, val);
320 }
321 
322 
323 template <typename dummy>
324 void BinaryFormatter::writeAttr(dummy& into, const SumoXMLAttr attr, const std::vector<const MSEdge*>& val) {
326  FileHelpers::writeEdgeVector(into, val);
327 }
328 
329 #endif
330 
331 /****************************************************************************/
332 
ToString.h
BinaryFormatter::writeHeader
bool writeHeader(std::ostream &into, const SumoXMLTag &rootElement)
Writes a header with optional edge list and connections.
Definition: BinaryFormatter.h:261
BinaryFormatter::BF_SCALED2INT_POSITION_3D
Definition: BinaryFormatter.h:97
FileHelpers::writeEdgeVector
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:206
BinaryFormatter::BF_XML_TAG_START
Definition: BinaryFormatter.h:69
FileHelpers.h
BinaryFormatter::writeStaticHeader
static void writeStaticHeader(std::ostream &into)
writes the part of the header which is always unchanged.
Definition: BinaryFormatter.cpp:46
FileHelpers::writeInt
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
Definition: FileHelpers.cpp:184
BinaryFormatter::BF_LANE
Definition: BinaryFormatter.h:77
SumoXMLEdgeFunc
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
Definition: SUMOXMLDefinitions.h:1079
BinaryFormatter::BF_LIST
Definition: BinaryFormatter.h:67
BinaryFormatter::BF_NODE_TYPE
Definition: BinaryFormatter.h:87
BinaryFormatter::BF_BYTE
Definition: BinaryFormatter.h:59
FileHelpers::writeByte
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
Definition: FileHelpers.cpp:198
PositionVector
A list of positions.
Definition: PositionVector.h:45
BinaryFormatter::BF_XML_TAG_END
Definition: BinaryFormatter.h:71
BinaryFormatter::writePadding
void writePadding(std::ostream &, const std::string &)
padding is ignored for binary output
Definition: BinaryFormatter.h:198
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:41
BinaryFormatter::DataType
DataType
data types in binary output
Definition: BinaryFormatter.h:57
BinaryFormatter::closeTag
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
Definition: BinaryFormatter.cpp:108
RGBColor
Definition: RGBColor.h:39
OutputFormatter
Abstract base class for output formatters.
Definition: OutputFormatter.h:51
BinaryFormatter::BF_SCALED2INT_POSITION_2D
Definition: BinaryFormatter.h:95
FileHelpers::writeString
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.
Definition: FileHelpers.cpp:205
SumoXMLNodeType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Definition: SUMOXMLDefinitions.h:1054
BinaryFormatter::BF_SCALED2INT
Definition: BinaryFormatter.h:93
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
BinaryFormatter::openTag
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
Definition: BinaryFormatter.cpp:90
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
BinaryFormatter::BF_EDGE_FUNCTION
Definition: BinaryFormatter.h:89
BinaryFormatter::writeStringList
static void writeStringList(std::ostream &into, const std::vector< std::string > &list)
writes a list of strings
Definition: BinaryFormatter.cpp:59
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
BinaryFormatter::BF_ROUTE
Definition: BinaryFormatter.h:91
OutputFormatter.h
SUMOXMLDefinitions::Attrs
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
Definition: SUMOXMLDefinitions.h:1362
BinaryFormatter::BF_POSITION_2D
Definition: BinaryFormatter.h:79
BinaryFormatter::BF_XML_ATTRIBUTE
Definition: BinaryFormatter.h:73
BinaryFormatter::BF_EDGE
Definition: BinaryFormatter.h:75
BinaryFormatter::writeXMLHeader
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs)
Writes an XML header with optional configuration.
Definition: BinaryFormatter.cpp:70
BinaryFormatter::myXMLStack
std::vector< SumoXMLTag > myXMLStack
The stack of begun xml elements.
Definition: BinaryFormatter.h:254
BinaryFormatter::BF_INTEGER
Definition: BinaryFormatter.h:61
BinaryFormatter::BF_POSITION_3D
Definition: BinaryFormatter.h:81
BinaryFormatter::BinaryFormatter
BinaryFormatter()
Constructor.
Definition: BinaryFormatter.cpp:41
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
BinaryFormatter
Output formatter for plain XML output.
Definition: BinaryFormatter.h:54
BinaryFormatter::writeAttrHeader
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type=BF_INVALID)
writes the header for an arbitrary attribute
Definition: BinaryFormatter.h:216
ROEdge
A basic edge for routing applications.
Definition: ROEdge.h:72
BinaryFormatter::BF_INVALID
Definition: BinaryFormatter.h:99
BinaryFormatter::BF_STRING
Definition: BinaryFormatter.h:65
BinaryFormatter::writePosition
static void writePosition(std::ostream &into, const Position &val)
writes a position
Definition: BinaryFormatter.cpp:158
BinaryFormatter::BF_BOUNDARY
Definition: BinaryFormatter.h:83
config.h
BinaryFormatter::writeAttr
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
Definition: BinaryFormatter.h:290
BinaryFormatter::BF_FLOAT
Definition: BinaryFormatter.h:63
SumoXMLAttr
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
Definition: SUMOXMLDefinitions.h:372
BinaryFormatter::~BinaryFormatter
virtual ~BinaryFormatter()
Destructor.
Definition: BinaryFormatter.h:107
SUMOXMLDefinitions.h
BinaryFormatter::writePreformattedTag
void writePreformattedTag(std::ostream &into, const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
Definition: BinaryFormatter.h:193
BinaryFormatter::BF_COLOR
Definition: BinaryFormatter.h:85