SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Output formatter for plain XML output 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This file is part of SUMO. 00014 // SUMO is free software: you can redistribute it and/or modify 00015 // it under the terms of the GNU General Public License as published by 00016 // the Free Software Foundation, either version 3 of the License, or 00017 // (at your option) any later version. 00018 // 00019 /****************************************************************************/ 00020 #ifndef BinaryFormatter_h 00021 #define BinaryFormatter_h 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #include <utils/common/FileHelpers.h> 00034 #include "OutputFormatter.h" 00035 00036 00037 // =========================================================================== 00038 // class declarations 00039 // =========================================================================== 00040 class Position; 00041 class PositionVector; 00042 class Boundary; 00043 00044 00045 // =========================================================================== 00046 // class definitions 00047 // =========================================================================== 00054 class BinaryFormatter : public OutputFormatter { 00055 public: 00057 enum DataType { 00059 BF_BYTE, 00061 BF_INTEGER, 00063 BF_FLOAT, 00065 BF_STRING, 00067 BF_LIST, 00069 BF_XML_TAG_START, 00071 BF_XML_TAG_END, 00073 BF_XML_ATTRIBUTE, 00075 BF_EDGE, 00077 BF_LANE, 00079 BF_POSITION_2D, 00081 BF_POSITION_3D, 00083 BF_COLOR, 00085 BF_NODE_TYPE, 00087 BF_EDGE_FUNCTION 00088 }; 00089 00091 BinaryFormatter(); 00092 00093 00095 virtual ~BinaryFormatter() { } 00096 00097 00111 bool writeXMLHeader(std::ostream& into, const std::string& rootElement, 00112 const std::string xmlParams = "", 00113 const std::string& attrs = "", 00114 const std::string& comment = ""); 00115 00116 00127 void openTag(std::ostream& into, const std::string& xmlElement); 00128 00129 00137 void openTag(std::ostream& into, const SumoXMLTag& xmlElement); 00138 00139 00146 void closeOpener(std::ostream& into); 00147 00148 00156 bool closeTag(std::ostream& into, bool abbreviated=false); 00157 00158 00165 void writeAttr(std::ostream& into, const std::string& attr, const std::string& val); 00166 00167 00174 template <typename T> 00175 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val); 00176 00177 00184 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val); 00185 00186 00193 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val); 00194 00195 00202 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val); 00203 00204 00211 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val); 00212 00213 00220 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val); 00221 00222 00229 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val); 00230 00231 00238 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val); 00239 00240 00241 private: 00248 static inline void writeAttrHeader(std::ostream& into, const SumoXMLAttr attr, const DataType type) { 00249 FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE)); 00250 FileHelpers::writeInt(into, attr); 00251 FileHelpers::writeByte(into, static_cast<unsigned char>(type)); 00252 } 00253 00254 00260 void writeStringList(std::ostream& into, const std::vector<std::string>& list); 00261 00262 00263 private: 00265 std::vector<SumoXMLTag> myXMLStack; 00266 00267 00268 }; 00269 00270 00271 template <typename T> 00272 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val) { 00273 BinaryFormatter::writeAttrHeader(into, attr, BF_STRING); 00274 FileHelpers::writeString(into, toString(val, into.precision())); 00275 } 00276 00277 00278 #endif 00279 00280 /****************************************************************************/ 00281