SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinaryFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Static storage of an output device and its base (abstract) implementation
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #ifdef HAVE_VERSION_H
32 #include <version.h>
33 #endif
34 
35 #include <utils/common/RGBColor.h>
36 #include <utils/common/ToString.h>
40 #include <utils/geom/Boundary.h>
41 #include "BinaryFormatter.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
52 }
53 
54 
55 void
56 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
58  FileHelpers::writeInt(into, (int)list.size());
59  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
61  FileHelpers::writeString(into, *it);
62  }
63 }
64 
65 bool
67  const std::string& rootElement,
68  const std::string& /* attrs */,
69  const std::string& /* comment */) {
70  if (myXMLStack.empty()) {
72  FileHelpers::writeByte(into, 1);
75  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
76  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
79  writeStringList(into, std::vector<std::string>());
80  writeStringList(into, std::vector<std::string>());
81 
82  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
83  openTag(into, rootElement);
84  return true;
85  }
86  }
87  return false;
88 }
89 
90 
91 void
92 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
93  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
94  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
95  }
96 }
97 
98 
99 void
100 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
101  myXMLStack.push_back(xmlElement);
103  FileHelpers::writeByte(into, static_cast<unsigned char>(xmlElement));
104 }
105 
106 
107 bool
108 BinaryFormatter::closeTag(std::ostream& into) {
109  if (!myXMLStack.empty()) {
111  FileHelpers::writeByte(into, static_cast<unsigned char>(myXMLStack.back()));
112  myXMLStack.pop_back();
113  return true;
114  }
115  return false;
116 }
117 
118 
119 template<>
120 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
122  FileHelpers::writeByte(into, val);
123 }
124 
125 
126 template<>
127 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
128  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
130  FileHelpers::writeInt(into, int(val * 100. + .5));
131  } else {
133  FileHelpers::writeFloat(into, val);
134  }
135 }
136 
137 
138 template<>
139 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
141  FileHelpers::writeInt(into, val);
142 }
143 
144 
145 template<>
146 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val) {
148  FileHelpers::writeInt(into, val);
149 }
150 
151 
152 template<>
153 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
155  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
156 }
157 
158 
159 template<>
160 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
162  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
163 }
164 
165 
166 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
167  if (val.z() != 0.) {
168  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
169  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
171  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
172  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
173  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
174  } else {
176  FileHelpers::writeFloat(into, val.x());
177  FileHelpers::writeFloat(into, val.y());
178  FileHelpers::writeFloat(into, val.z());
179  }
180  } else {
181  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
182  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
184  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
185  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
186  } else {
188  FileHelpers::writeFloat(into, val.x());
189  FileHelpers::writeFloat(into, val.y());
190  }
191  }
192 }
193 
194 
195 template<>
196 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
198  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
199  writePosition(into, val);
200 }
201 
202 
203 template<>
204 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
206  FileHelpers::writeInt(into, static_cast<int>(val.size()));
207  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
208  writePosition(into, *pos);
209  }
210 }
211 
212 
213 template<>
214 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
216  FileHelpers::writeFloat(into, val.xmin());
217  FileHelpers::writeFloat(into, val.ymin());
218  FileHelpers::writeFloat(into, val.xmax());
219  FileHelpers::writeFloat(into, val.ymax());
220 }
221 
222 
223 template<>
224 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
226  FileHelpers::writeByte(into, val.red());
227  FileHelpers::writeByte(into, val.green());
228  FileHelpers::writeByte(into, val.blue());
229  FileHelpers::writeByte(into, val.alpha());
230 }
231 
232 
233 template<>
234 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
236  FileHelpers::writeInt(into, (int)val.size());
237  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
239  FileHelpers::writeInt(into, *it);
240  }
241 }
242 
243 
244 /****************************************************************************/
SumoXMLTag
Numbers representing SUMO-XML - element names.
static StringBijection< SumoXMLNodeType > NodeTypes
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
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 xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:112
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
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.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type)
writes the header for an arbitrary attribute
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
BinaryFormatter()
Constructor.
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
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.
#define VERSION_STRING
Definition: config.h:227
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
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.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:215
static void writePosition(std::ostream &into, const Position &val)
writes a position
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.