|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Static storage of an output device and its base (abstract) implementation 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 #ifndef OutputDevice_h 00020 #define OutputDevice_h 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <string> 00033 #include <vector> 00034 #include <map> 00035 #include <utils/common/UtilExceptions.h> 00036 #include <utils/common/ToString.h> 00037 #include <utils/xml/SUMOXMLDefinitions.h> 00038 00039 00040 // =========================================================================== 00041 // class definitions 00042 // =========================================================================== 00067 class OutputDevice { 00068 public: 00071 00084 static OutputDevice& getDevice(const std::string& name, 00085 const std::string& base="") throw(IOError); 00086 00087 00104 static bool createDeviceByOption(const std::string& optionName, 00105 const std::string& rootElement="") throw(IOError); 00106 00107 00120 static OutputDevice& getDeviceByOption(const std::string& name) throw(IOError, InvalidArgument); 00121 00122 00125 static void closeAll() throw(); 00127 00128 00132 static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY); 00133 00134 00135 00136 public: 00139 00141 OutputDevice(const unsigned int defaultIndentation=0) throw(IOError); 00142 00143 00145 virtual ~OutputDevice() throw() { } 00146 00147 00151 virtual bool ok() throw(); 00152 00153 00156 void close() throw(); 00157 00158 00162 void setPrecision(unsigned int precision=OUTPUT_ACCURACY) throw(); 00163 00164 00177 bool writeXMLHeader(const std::string& rootElement, 00178 const std::string xmlParams="", 00179 const std::string& attrs="", 00180 const std::string& comment="") throw(); 00181 00182 00189 OutputDevice& indent() throw(); 00190 00191 00201 OutputDevice& openTag(const std::string& xmlElement) throw(); 00202 00203 00211 OutputDevice& openTag(const SumoXMLTag& xmlElement) throw(); 00212 00213 00224 bool closeTag(bool abbreviated=false) throw(); 00225 00231 OutputDevice& writeAttr(std::string attr, std::string val) { 00232 getOStream() << " " << attr << "=\"" << val << "\""; 00233 return *this; 00234 } 00235 00241 template <class T> 00242 OutputDevice& writeAttr(const SumoXMLAttr attr, const T& val) { 00243 return writeAttr(toString(attr), toString(val)); 00244 } 00245 00246 00253 void inform(const std::string& msg); 00254 00255 00259 template <class T> 00260 OutputDevice& operator<<(const T& t) { 00261 getOStream() << t; 00262 postWriteHook(); 00263 return *this; 00264 } 00265 00266 protected: 00268 virtual std::ostream& getOStream() throw() = 0; 00269 00270 00275 virtual void postWriteHook() throw(); 00276 00277 00278 private: 00281 00283 typedef std::map<std::string, OutputDevice*> DeviceMap; 00284 00286 static DeviceMap myOutputDevices; 00288 00289 00290 private: 00293 00295 std::vector<std::string> myXMLStack; 00296 00298 unsigned int myDefaultIndentation; 00300 00301 }; 00302 00303 00304 #endif 00305 00306 /****************************************************************************/ 00307