|
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 <vector> 00037 #include <map> 00038 #include <utils/common/UtilExceptions.h> 00039 #include <utils/common/ToString.h> 00040 #include <utils/xml/SUMOXMLDefinitions.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 00135 static std::string realString(const SUMOReal v, const int precision = OUTPUT_ACCURACY); 00136 00137 00138 00139 public: 00142 00144 OutputDevice(const unsigned int defaultIndentation = 0); 00145 00146 00148 virtual ~OutputDevice() { } 00149 00150 00154 virtual bool ok() ; 00155 00156 00159 void close() ; 00160 00161 00165 void setPrecision(unsigned int precision = OUTPUT_ACCURACY) ; 00166 00167 00180 bool writeXMLHeader(const std::string& rootElement, 00181 const std::string xmlParams = "", 00182 const std::string& attrs = "", 00183 const std::string& comment = "") ; 00184 00185 00192 OutputDevice& indent() ; 00193 00194 00204 OutputDevice& openTag(const std::string& xmlElement) ; 00205 00206 00214 OutputDevice& openTag(const SumoXMLTag& xmlElement) ; 00215 00216 00227 bool closeTag(bool abbreviated = false) ; 00228 00234 OutputDevice& writeAttr(std::string attr, std::string val); 00235 00236 00242 template <class T> 00243 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) { 00244 return writeAttr(toString(attr), toString(val, getOStream().precision())); 00245 } 00246 00247 00254 void inform(const std::string& msg); 00255 00256 00260 template <class T> 00261 OutputDevice& operator<<(const T& t) { 00262 getOStream() << t; 00263 postWriteHook(); 00264 return *this; 00265 } 00266 00267 protected: 00269 virtual std::ostream& getOStream() = 0; 00270 00271 00276 virtual void postWriteHook() ; 00277 00278 00279 private: 00282 00284 typedef std::map<std::string, OutputDevice*> DeviceMap; 00285 00287 static DeviceMap myOutputDevices; 00289 00290 00291 private: 00294 00296 std::vector<std::string> myXMLStack; 00297 00299 unsigned int myDefaultIndentation; 00301 00302 }; 00303 00304 00305 #endif 00306 00307 /****************************************************************************/ 00308