SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // Static storage of an output device and its base (abstract) implementation 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 #ifndef OutputDevice_h 00023 #define OutputDevice_h 00024 00025 00026 // =========================================================================== 00027 // included modules 00028 // =========================================================================== 00029 #ifdef _MSC_VER 00030 #include <windows_config.h> 00031 #else 00032 #include <config.h> 00033 #endif 00034 00035 #include <string> 00036 #include <map> 00037 #include <utils/common/ToString.h> 00038 #include <utils/xml/SUMOXMLDefinitions.h> 00039 #include "PlainXMLFormatter.h" 00040 #include "BinaryFormatter.h" 00041 00042 00043 // =========================================================================== 00044 // class definitions 00045 // =========================================================================== 00070 class OutputDevice { 00071 public: 00074 00087 static OutputDevice& getDevice(const std::string& name, 00088 const std::string& base = ""); 00089 00090 00107 static bool createDeviceByOption(const std::string& optionName, 00108 const std::string& rootElement = ""); 00109 00110 00123 static OutputDevice& getDeviceByOption(const std::string& name) throw(IOError, InvalidArgument); 00124 00125 00128 static void closeAll() ; 00130 00131 00138 static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY); 00139 00140 00141 public: 00144 00146 OutputDevice(const bool binary=false, const unsigned int defaultIndentation=0); 00147 00148 00150 virtual ~OutputDevice(); 00151 00152 00156 virtual bool ok() ; 00157 00158 00161 void close() ; 00162 00163 00167 void setPrecision(unsigned int precision=OUTPUT_ACCURACY); 00168 00169 00183 bool writeXMLHeader(const std::string& rootElement, 00184 const std::string xmlParams = "", 00185 const std::string& attrs = "", 00186 const std::string& comment = ""); 00187 00188 00198 OutputDevice& openTag(const std::string& xmlElement); 00199 00200 00208 OutputDevice& openTag(const SumoXMLTag& xmlElement); 00209 00210 00215 void closeOpener(); 00216 00227 bool closeTag(bool abbreviated=false); 00228 00235 OutputDevice& writeAttr(std::string attr, std::string val); 00236 00237 00240 void lf() { 00241 if (!myAmBinary) { 00242 getOStream() << "\n"; 00243 } 00244 } 00245 00246 00250 bool isBinary() const { 00251 return myAmBinary; 00252 } 00253 00254 00261 template <typename T> 00262 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) { 00263 if (myAmBinary) { 00264 BinaryFormatter::writeAttr(getOStream(), attr, val); 00265 } else { 00266 PlainXMLFormatter::writeAttr(getOStream(), attr, val); 00267 } 00268 return *this; 00269 } 00270 00271 00278 void inform(const std::string& msg, const char progress=0); 00279 00280 00284 template <class T> 00285 OutputDevice& operator<<(const T& t) { 00286 getOStream() << t; 00287 postWriteHook(); 00288 return *this; 00289 } 00290 00291 protected: 00293 virtual std::ostream& getOStream() = 0; 00294 00295 00300 virtual void postWriteHook() ; 00301 00302 00303 private: 00305 static std::map<std::string, OutputDevice*> myOutputDevices; 00306 00307 00308 private: 00310 OutputFormatter* myFormatter; 00311 00312 const bool myAmBinary; 00313 00314 private: 00316 OutputDevice(const OutputDevice&); 00317 00319 OutputDevice& operator=(const OutputDevice&); 00320 00321 }; 00322 00323 00324 #endif 00325 00326 /****************************************************************************/ 00327