#include <OutputDevice.h>

OutputDevices are basically a capsule around an std::ostream, which give a unified access to sockets, files and stdout.
Usually, an application builds as many output devices as needed. Each output device may also be used to save outputs from several sources (several detectors, for example). Building is done using OutputDevice::getDevice() what also parses the given output description in order to decide what kind of an OutputDevice shall be built. OutputDevices are closed via OutputDevice::closeAll(), normally called at the application's end.
Although everything that can be written to a stream can also be written to an OutputDevice, there is special support for XML tags (remembering all open tags to close them at the end). OutputDevices are still lacking support for function pointers with the '<<' operator (no endl, use '
'). The most important method to implement in subclasses is getOStream, the most used part of the interface is the '<<' operator.
The Boolean markers are used rarely and might get removed in future versions.
Definition at line 65 of file OutputDevice.h.
static members | |
| typedef std::map< std::string, OutputDevice * > | DeviceMap |
| Definition of a map from names to output devices. | |
| static DeviceMap | myOutputDevices |
| map from names to output devices | |
OutputDevice member methods | |
| void | close () throw () |
| Closes the device and removes it from the dictionary. | |
| bool | closeTag (bool abbreviated=false) throw () |
| Closes the most recently opened tag. | |
| OutputDevice & | indent () throw () |
| Adds indentation. | |
| void | inform (const std::string &msg) |
| Retrieves a message to this device. | |
| virtual bool | ok () throw () |
| returns the information whether one can write into the device | |
| OutputDevice & | openTag (const std::string &xmlElement) throw () |
| Opens an XML tag. | |
| template<class T> | |
| OutputDevice & | operator<< (const T &t) |
| Abstract output operator. | |
| OutputDevice () throw (IOError) | |
| Constructor. | |
| void | setPrecision (unsigned int precision=OUTPUT_ACCURACY) throw () |
| Sets the precison or resets it to default. | |
| bool | writeXMLHeader (const std::string &rootElement, const bool writeConfig=true, const std::string &attrs="", const std::string &comment="") throw () |
| Writes an XML header with optional configuration. | |
| virtual | ~OutputDevice () throw () |
| Destructor. | |
| virtual std::ostream & | getOStream ()=0 throw () |
| Returns the associated ostream. | |
| virtual void | postWriteHook () throw () |
| Called after every write access. | |
Static Public Member Functions | |
static access methods to OutputDevices | |
| static void | closeAll () throw () |
| static bool | createDeviceByOption (const std::string &optionName, const std::string &rootElement="") throw (IOError) |
| Creates the device using the output definition stored in the named option. | |
| static OutputDevice & | getDevice (const std::string &name, const std::string &base="") throw (IOError) |
| Returns the described OutputDevice. | |
| static OutputDevice & | getDeviceByOption (const std::string &name) throw (IOError, InvalidArgument) |
| Returns the device described by the option. | |
Private Attributes | |
Non-static members of each OutputDevice | |
| std::vector< std::string > | myXMLStack |
| The stack of begun xml elements. | |
typedef std::map<std::string, OutputDevice*> OutputDevice::DeviceMap [private] |
| OutputDevice::OutputDevice | ( | ) | throw (IOError) [inline] |
| virtual OutputDevice::~OutputDevice | ( | ) | throw () [inline, virtual] |
| void OutputDevice::close | ( | ) | throw () |
Closes the device and removes it from the dictionary.
Definition at line 133 of file OutputDevice.cpp.
References closeTag(), and myOutputDevices.
Referenced by RONet::closeOutput(), GUIDialog_ViewSettings::onCmdExportSetting(), GUIParameterTracker::onCmdSave(), GUIDialog_Breakpoints::onCmdSave(), RODFDetectorCon::save(), PCPolyContainer::save(), GUISelectedStorage::save(), GUISelectedStorage::SingleTypeSelections::save(), RODFDetectorCon::saveAsPOIs(), GUIDialog_ViewSettings::saveDecals(), NBNodeCont::savePlain(), NBEdgeCont::savePlain(), RODFDetectorCon::saveRoutes(), RODFDetector::writeEmitterDefinition(), RODFDetectorCon::writeEmitterPOIs(), RODFDetectorCon::writeEmitters(), RODFDetectorCon::writeEndRerouterDetectors(), RODFDetector::writeSingleSpeedTrigger(), RODFDetectorCon::writeSpeedTrigger(), and RODFDetectorCon::writeValidationDetectors().
00133 { 00134 while (closeTag()); 00135 for (DeviceMap::iterator i=myOutputDevices.begin(); i!=myOutputDevices.end(); ++i) { 00136 if (i->second == this) { 00137 myOutputDevices.erase(i); 00138 break; 00139 } 00140 } 00141 delete this; 00142 }
| void OutputDevice::closeAll | ( | ) | throw () [static] |
Closes all registered devices
Definition at line 115 of file OutputDevice.cpp.
References myOutputDevices.
Referenced by GUIRunThread::deleteSim(), and main().
00115 { 00116 while (myOutputDevices.size()!=0) { 00117 myOutputDevices.begin()->second->close(); 00118 } 00119 myOutputDevices.clear(); 00120 }
Closes the most recently opened tag.
The topmost xml-element from the stack is written into the stream as a closing element ("</" + element + ">") and is then removed from the stack. If abbreviated closing is requested, only "/>" is the output.
| [in] | name | whether abbreviated closing is performed |
Definition at line 188 of file OutputDevice.cpp.
References getOStream(), indent(), myXMLStack, and postWriteHook().
Referenced by close(), MSPersonControl::erase(), ROVehicle::saveAllAsXML(), MSVehicleControl::scheduleVehicleRemoval(), RORouteDef_Complete::writeXMLDefinition(), RORouteDef_Alternatives::writeXMLDefinition(), and RORoute::writeXMLDefinition().
00188 { 00189 if (!myXMLStack.empty()) { 00190 if (abbreviated) { 00191 getOStream() << "/>" << std::endl; 00192 } else { 00193 std::string indent(4*(myXMLStack.size()-1), ' '); 00194 getOStream() << indent << "</" << myXMLStack.back() << ">" << std::endl; 00195 } 00196 myXMLStack.pop_back(); 00197 postWriteHook(); 00198 return true; 00199 } 00200 return false; 00201 }
| bool OutputDevice::createDeviceByOption | ( | const std::string & | optionName, | |
| const std::string & | rootElement = "" | |||
| ) | throw (IOError) [static] |
Creates the device using the output definition stored in the named option.
Creates and returns the device named by the option. Asks whether the option and retrieves the name from the option if so. Optionally the XML header gets written as well. Returns whether a device was created (option was set).
Please note, that we do not have to consider the "application base" herein, because this call is only used to get file names of files referenced within XML-declarations of structures which paths already is aware of the cwd.
| [in] | optionName | The name of the option to use for retrieving the output definition |
| [in] | rootElement | The root element to use (XML-output) |
| IOError | If the output could not be built for any reason (error message is supplied) |
Definition at line 91 of file OutputDevice.cpp.
References getDevice(), OptionsCont::getOptions(), and writeXMLHeader().
Referenced by MSFrame::buildStreams(), NBNetBuilder::compute(), NBNodeCont::computeNodeShapes(), and main().
00092 { 00093 if (!OptionsCont::getOptions().isSet(optionName)) { 00094 return false; 00095 } 00096 OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName)); 00097 if (rootElement != "") { 00098 dev.writeXMLHeader(rootElement); 00099 } 00100 return true; 00101 }
| OutputDevice & OutputDevice::getDevice | ( | const std::string & | name, | |
| const std::string & | base = "" | |||
| ) | throw (IOError) [static] |
Returns the described OutputDevice.
Creates and returns the named device. "stdout" and "-" refer to standard out, "hostname:port" initiates socket connection. Otherwise a filename is assumed and the second parameter may be used to give a base directory. If there already is a device with the same name this one is returned.
| [in] | name | The description of the output name/port/whatever |
| [in] | base | The base path the application is run within |
| IOError | If the output could not be built for any reason (error message is supplied) |
Definition at line 60 of file OutputDevice.cpp.
References TplConvert< E >::_2int(), FileHelpers::checkForRelativity(), getOStream(), FileHelpers::isSocket(), and setPrecision().
Referenced by NLHandler::addE1Detector(), NLHandler::addE2Detector(), NLHandler::addEdgeLaneMeanData(), NLHandler::addRouteProbeDetector(), NLHandler::addVTypeProbeDetector(), NLHandler::beginE3Detector(), NBNetBuilder::buildLoaded(), NLDiscreteEventBuilder::buildSaveTLStateCommand(), NLDiscreteEventBuilder::buildSaveTLSwitchesCommand(), NLDiscreteEventBuilder::buildSaveTLSwitchStatesCommand(), createDeviceByOption(), getDeviceByOption(), MsgHandler::initOutputOptions(), GUIDialog_ViewSettings::onCmdExportSetting(), GUIParameterTracker::onCmdSave(), GUIDialog_EditViewport::onCmdSave(), GUIDialog_Breakpoints::onCmdSave(), RONet::openOutput(), RODFDetectorCon::save(), PCPolyContainer::save(), GUISelectedStorage::save(), GUISelectedStorage::SingleTypeSelections::save(), RODFDetectorCon::saveAsPOIs(), GUIDialog_ViewSettings::saveDecals(), NBNodeCont::savePlain(), NBEdgeCont::savePlain(), RODFDetectorCon::saveRoutes(), RODFDetector::writeEmitterDefinition(), RODFDetectorCon::writeEmitterPOIs(), RODFDetectorCon::writeEmitters(), RODFDetectorCon::writeEndRerouterDetectors(), RODFDetector::writeSingleSpeedTrigger(), RODFDetectorCon::writeSpeedTrigger(), and RODFDetectorCon::writeValidationDetectors().
00061 { 00062 // check whether the device has already been aqcuired 00063 if (myOutputDevices.find(name)!=myOutputDevices.end()) { 00064 return *myOutputDevices[name]; 00065 } 00066 // build the device 00067 OutputDevice *dev = 0; 00068 // check whether the device shall print to stdout 00069 if (name=="stdout" || name=="-") { 00070 dev = new OutputDevice_COUT(); 00071 } else if (FileHelpers::isSocket(name)) { 00072 try { 00073 int port = TplConvert<char>::_2int(name.substr(name.find(":")+1).c_str()); 00074 dev = new OutputDevice_Network(name.substr(0, name.find(":")), port); 00075 } catch (NumberFormatException &) { 00076 throw IOError("Given port number '" + name.substr(name.find(":")+1) + "' is not numeric."); 00077 } catch (EmptyData &) { 00078 throw IOError("No port number given."); 00079 } 00080 } else { 00081 dev = new OutputDevice_File(FileHelpers::checkForRelativity(name, base)); 00082 } 00083 dev->setPrecision(); 00084 dev->getOStream() << std::setiosflags(std::ios::fixed); 00085 myOutputDevices[name] = dev; 00086 return *dev; 00087 }
| OutputDevice & OutputDevice::getDeviceByOption | ( | const std::string & | name | ) | throw (IOError, InvalidArgument) [static] |
Returns the device described by the option.
Returns the device named by the option. If the option is unknown, unset or the device was not created before, InvalidArgument is thrown.
Please note, that we do not have to consider the "application base" herein.
| [in] | name | The name of the option to use for retrieving the output definition |
| IOError | If the output could not be built for any reason (error message is supplied) | |
| InvalidArgument | If the option with the given name does not exist |
Definition at line 105 of file OutputDevice.cpp.
References getDevice(), OptionsCont::getOptions(), and OptionsCont::getString().
Referenced by NBNodeShapeComputer::compute(), NBNodeShapeComputer::computeNodeShapeByCrosses(), MSPersonControl::erase(), main(), MSVehicleControl::scheduleVehicleRemoval(), NBNode::sortNodesEdges(), and MSNet::writeOutput().
00105 { 00106 std::string devName = OptionsCont::getOptions().getString(optionName); 00107 if (myOutputDevices.find(devName)==myOutputDevices.end()) { 00108 throw InvalidArgument("Device '" + devName + "' has not been created."); 00109 } 00110 return OutputDevice::getDevice(devName); 00111 }
| virtual std::ostream& OutputDevice::getOStream | ( | ) | throw () [protected, pure virtual] |
Returns the associated ostream.
Implemented in MsgRetrievingFunction< T >, OutputDevice_COUT, OutputDevice_File, OutputDevice_Network, OutputDevice_String, and OutputDeviceMock.
Referenced by closeTag(), getDevice(), indent(), inform(), ok(), openTag(), operator<<(), setPrecision(), and writeXMLHeader().
| OutputDevice & OutputDevice::indent | ( | ) | throw () |
Adds indentation.
An intendation, depending on the current xml-element-stack size, is written.
Definition at line 171 of file OutputDevice.cpp.
References getOStream(), myXMLStack, and postWriteHook().
Referenced by closeTag().
00171 { 00172 getOStream() << std::string(4*myXMLStack.size(), ' '); 00173 postWriteHook(); 00174 return *this; 00175 }
| void OutputDevice::inform | ( | const std::string & | msg | ) |
Retrieves a message to this device.
Implementation of the MessageRetriever interface. Writes the given message to the output device.
| [in] | msg | The msg to write to the device |
Definition at line 209 of file OutputDevice.cpp.
References getOStream(), and postWriteHook().
00209 { 00210 getOStream() << msg << '\n'; 00211 postWriteHook(); 00212 }
| bool OutputDevice::ok | ( | ) | throw () [virtual] |
returns the information whether one can write into the device
Definition at line 128 of file OutputDevice.cpp.
References getOStream().
00128 { 00129 return getOStream().good(); 00130 }
| OutputDevice & OutputDevice::openTag | ( | const std::string & | xmlElement | ) | throw () |
Opens an XML tag.
An intendation, depending on the current xml-element-stack size, is written followed by the given xmlelement ("<" + xmlElement) The xmlelement is added to the stack, then.
| [in] | xmlElement | Name of element to open |
Definition at line 179 of file OutputDevice.cpp.
References getOStream(), myXMLStack, and postWriteHook().
Referenced by MSPersonControl::erase(), MSVehicleControl::scheduleVehicleRemoval(), RORouteDef_Complete::writeXMLDefinition(), RORouteDef_Alternatives::writeXMLDefinition(), RORoute::writeXMLDefinition(), writeXMLHeader(), and MSVehicle::writeXMLRoute().
00179 { 00180 getOStream() << std::string(4*myXMLStack.size(), ' ') << "<" << xmlElement; 00181 postWriteHook(); 00182 myXMLStack.push_back(xmlElement); 00183 return *this; 00184 }
| OutputDevice& OutputDevice::operator<< | ( | const T & | t | ) | [inline] |
Abstract output operator.
Definition at line 222 of file OutputDevice.h.
References getOStream(), and postWriteHook().
00222 { 00223 getOStream() << t; 00224 postWriteHook(); 00225 return *this; 00226 }
| void OutputDevice::postWriteHook | ( | ) | throw () [protected, virtual] |
Called after every write access.
Default implementation does nothing.
Reimplemented in MsgRetrievingFunction< T >, and OutputDevice_Network.
Definition at line 205 of file OutputDevice.cpp.
Referenced by closeTag(), indent(), inform(), openTag(), and operator<<().
| void OutputDevice::setPrecision | ( | unsigned int | precision = OUTPUT_ACCURACY |
) | throw () |
Sets the precison or resets it to default.
| [in] | precision | The accuracy (number of digits behind '.') to set |
Definition at line 146 of file OutputDevice.cpp.
References getOStream().
Referenced by MSVTypeProbe::execute(), getDevice(), NBNodeCont::savePlain(), RORouteDef_Alternatives::writeXMLDefinition(), and RORoute::writeXMLDefinition().
00146 { 00147 getOStream() << std::setprecision(precision); 00148 }
| bool OutputDevice::writeXMLHeader | ( | const std::string & | rootElement, | |
| const bool | writeConfig = true, |
|||
| const std::string & | attrs = "", |
|||
| const std::string & | comment = "" | |||
| ) | throw () |
Writes an XML header with optional configuration.
If something has been written (myXMLStack is not empty), nothing is written and false returned.
| [in] | rootElement | The root element to use |
| [in] | writeConfig | Whether the configuration used while this file was generated shall be saved |
| [in] | attrs | Additional attributes to save within the rootElement |
| [in] | comment | Additional comment (saved in front the rootElement) |
Definition at line 152 of file OutputDevice.cpp.
References OptionsCont::getOptions(), getOStream(), myXMLStack, openTag(), and OptionsCont::writeXMLHeader().
Referenced by createDeviceByOption(), RONet::openOutput(), RODFDetectorCon::save(), PCPolyContainer::save(), RODFDetectorCon::saveAsPOIs(), NBNodeCont::savePlain(), NBEdgeCont::savePlain(), RODFDetectorCon::saveRoutes(), RODFDetector::writeEmitterDefinition(), RODFDetectorCon::writeEmitterPOIs(), RODFDetectorCon::writeEmitters(), RODFDetectorCon::writeEndRerouterDetectors(), RODFDetector::writeSingleSpeedTrigger(), RODFDetectorCon::writeSpeedTrigger(), and RODFDetectorCon::writeValidationDetectors().
00153 { 00154 if (myXMLStack.empty()) { 00155 OptionsCont::getOptions().writeXMLHeader(getOStream(), writeConfig); 00156 if (comment != "") { 00157 getOStream() << comment << "\n"; 00158 } 00159 openTag(rootElement); 00160 if (attrs != "") { 00161 getOStream() << " " << attrs; 00162 } 00163 getOStream() << ">\n"; 00164 return true; 00165 } 00166 return false; 00167 }
OutputDevice::DeviceMap OutputDevice::myOutputDevices [static, private] |
map from names to output devices
Definition at line 248 of file OutputDevice.h.
Referenced by close(), and closeAll().
std::vector<std::string> OutputDevice::myXMLStack [private] |
The stack of begun xml elements.
Definition at line 257 of file OutputDevice.h.
Referenced by closeTag(), indent(), openTag(), and writeXMLHeader().
1.5.6