SUMO - Simulation of Urban MObility
BinaryFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Static storage of an output device and its base (abstract) implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2012-2016 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 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_VERSION_H
34 #include <version.h>
35 #endif
36 
37 #include <utils/common/RGBColor.h>
38 #include <utils/common/ToString.h>
42 #include <utils/geom/Boundary.h>
43 #include "BinaryFormatter.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // member method definitions
52 // ===========================================================================
54 }
55 
56 
57 void
60  FileHelpers::writeByte(into, 1);
63  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
64  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
67 }
68 
69 
70 void
71 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
73  FileHelpers::writeInt(into, (int)list.size());
74  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
76  FileHelpers::writeString(into, *it);
77  }
78 }
79 
80 
81 bool
83  const std::string& rootElement,
84  const std::string& /* attrs */,
85  const std::string& /* comment */) {
86  if (myXMLStack.empty()) {
88  FileHelpers::writeByte(into, 1);
91  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
92  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
95  writeStringList(into, std::vector<std::string>());
96  writeStringList(into, std::vector<std::string>());
97 
98  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
99  openTag(into, rootElement);
100  return true;
101  }
102  }
103  return false;
104 }
105 
106 
107 void
108 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
109  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
110  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
111  }
112 }
113 
114 
115 void
116 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
117  myXMLStack.push_back(xmlElement);
119  FileHelpers::writeByte(into, static_cast<unsigned char>(xmlElement));
120 }
121 
122 
123 bool
124 BinaryFormatter::closeTag(std::ostream& into) {
125  if (!myXMLStack.empty()) {
127  FileHelpers::writeByte(into, static_cast<unsigned char>(myXMLStack.back()));
128  myXMLStack.pop_back();
129  return true;
130  }
131  return false;
132 }
133 
134 
135 template<>
136 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
138  FileHelpers::writeByte(into, val);
139 }
140 
141 
142 template<>
143 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
144  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
146  FileHelpers::writeInt(into, int(val * 100. + .5));
147  } else {
149  FileHelpers::writeFloat(into, val);
150  }
151 }
152 
153 
154 template<>
155 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
157  FileHelpers::writeInt(into, val);
158 }
159 
160 
161 template<>
162 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
164  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
165 }
166 
167 
168 template<>
169 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
171  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
172 }
173 
174 
175 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
176  if (val.z() != 0.) {
177  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
178  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
180  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
181  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
182  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
183  } else {
185  FileHelpers::writeFloat(into, val.x());
186  FileHelpers::writeFloat(into, val.y());
187  FileHelpers::writeFloat(into, val.z());
188  }
189  } else {
190  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
191  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
193  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
194  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
195  } else {
197  FileHelpers::writeFloat(into, val.x());
198  FileHelpers::writeFloat(into, val.y());
199  }
200  }
201 }
202 
203 
204 template<>
205 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
207  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
208  writePosition(into, val);
209 }
210 
211 
212 template<>
213 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
215  FileHelpers::writeInt(into, static_cast<int>(val.size()));
216  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
217  writePosition(into, *pos);
218  }
219 }
220 
221 
222 template<>
223 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
225  FileHelpers::writeFloat(into, val.xmin());
226  FileHelpers::writeFloat(into, val.ymin());
227  FileHelpers::writeFloat(into, val.xmax());
228  FileHelpers::writeFloat(into, val.ymax());
229 }
230 
231 
232 template<>
233 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
235  FileHelpers::writeByte(into, val.red());
236  FileHelpers::writeByte(into, val.green());
237  FileHelpers::writeByte(into, val.blue());
238  FileHelpers::writeByte(into, val.alpha());
239 }
240 
241 
242 template<>
243 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
245  FileHelpers::writeInt(into, (int)val.size());
246  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
248  FileHelpers::writeInt(into, *it);
249  }
250 }
251 
252 
253 /****************************************************************************/
SumoXMLTag
Numbers representing SUMO-XML - element names.
static StringBijection< SumoXMLNodeType > NodeTypes
static void writeStaticHeader(std::ostream &into)
writes the part of the header which is always unchanged.
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
bool closeTag(std::ostream &into)
Closes the most recently opened tag.
static std::ostream & writeFloat(std::ostream &strm, SUMOReal value)
Writes a float binary.
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
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
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
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.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
#define VERSION_STRING
Definition: config.h:225
std::vector< SumoXMLTag > myXMLStack
The stack of begun xml elements.
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
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
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...
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
#define SUMOReal
Definition: config.h:213
static void writePosition(std::ostream &into, const Position &val)
writes a position
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.