OutputDevice_String Class Reference

#include <OutputDevice_String.h>

Inheritance diagram for OutputDevice_String:

OutputDevice

Detailed Description

An output device that encapsulates an ofstream.

Please note that the device gots responsible for the stream and deletes it (it should not be deleted elsewhere).

Definition at line 46 of file OutputDevice_String.h.


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.
OutputDeviceindent () 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
OutputDeviceopenTag (const std::string &xmlElement) throw ()
 Opens an XML tag.
template<class T>
OutputDeviceoperator<< (const T &t)
 Abstract output operator.
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 void postWriteHook () throw ()
 Called after every write access.

Public Member Functions

std::string getString () throw ()
 Returns the current content as a string.
 OutputDevice_String () throw (IOError)
 Constructor
Exceptions:
IOError Should not be thrown by this implementation.

 ~OutputDevice_String () throw ()
 Destructor.

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 OutputDevicegetDevice (const std::string &name, const std::string &base="") throw (IOError)
 Returns the described OutputDevice.
static OutputDevicegetDeviceByOption (const std::string &name) throw (IOError, InvalidArgument)
 Returns the device described by the option.

Protected Member Functions

Methods that override/implement OutputDevice-methods
std::ostream & getOStream () throw ()
 Returns the associated ostream.

Private Attributes

std::ostringstream myStream
 The wrapped ofstream.

Constructor & Destructor Documentation

OutputDevice_String::OutputDevice_String (  )  throw (IOError)

Constructor

Exceptions:
IOError Should not be thrown by this implementation.

Definition at line 42 of file OutputDevice_String.cpp.

00042                                                         {
00043 }

OutputDevice_String::~OutputDevice_String (  )  throw ()

Destructor.

Definition at line 46 of file OutputDevice_String.cpp.

00046                                                   {
00047 }


Member Function Documentation

void OutputDevice::close (  )  throw () [inherited]

void OutputDevice::closeAll (  )  throw () [static, inherited]

Closes all registered devices

Definition at line 115 of file OutputDevice.cpp.

References OutputDevice::myOutputDevices.

Referenced by GUIRunThread::deleteSim(), and main().

00115                                {
00116     while (myOutputDevices.size()!=0) {
00117         myOutputDevices.begin()->second->close();
00118     }
00119     myOutputDevices.clear();
00120 }

bool OutputDevice::closeTag ( bool  abbreviated = false  )  throw () [inherited]

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.

Parameters:
[in] name whether abbreviated closing is performed
Returns:
Whether a further element existed in the stack and could be closed
Todo:
it is not verified that the topmost element was closed

Definition at line 188 of file OutputDevice.cpp.

References OutputDevice::getOStream(), OutputDevice::indent(), OutputDevice::myXMLStack, and OutputDevice::postWriteHook().

Referenced by OutputDevice::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, inherited]

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.

Parameters:
[in] optionName The name of the option to use for retrieving the output definition
[in] rootElement The root element to use (XML-output)
Returns:
Whether a device was built (the option was set)
Exceptions:
IOError If the output could not be built for any reason (error message is supplied)

Definition at line 91 of file OutputDevice.cpp.

References OutputDevice::getDevice(), OptionsCont::getOptions(), and OutputDevice::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, inherited]

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.

Parameters:
[in] name The description of the output name/port/whatever
[in] base The base path the application is run within
Returns:
The corresponding (built or existing) device
Exceptions:
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(), OutputDevice::getOStream(), FileHelpers::isSocket(), and OutputDevice::setPrecision().

Referenced by NLHandler::addE1Detector(), NLHandler::addE2Detector(), NLHandler::addEdgeLaneMeanData(), NLHandler::addRouteProbeDetector(), NLHandler::addVTypeProbeDetector(), NLHandler::beginE3Detector(), NBNetBuilder::buildLoaded(), NLDiscreteEventBuilder::buildSaveTLStateCommand(), NLDiscreteEventBuilder::buildSaveTLSwitchesCommand(), NLDiscreteEventBuilder::buildSaveTLSwitchStatesCommand(), OutputDevice::createDeviceByOption(), OutputDevice::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, inherited]

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.

Parameters:
[in] name The name of the option to use for retrieving the output definition
Returns:
The corresponding (built or existing) device
Exceptions:
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 OutputDevice::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 }

std::ostream & OutputDevice_String::getOStream (  )  throw () [protected, virtual]

Returns the associated ostream.

Returns:
The used stream

Implements OutputDevice.

Definition at line 57 of file OutputDevice_String.cpp.

References myStream.

00057                                         {
00058     return myStream;
00059 }

std::string OutputDevice_String::getString (  )  throw ()

Returns the current content as a string.

Returns:
The content as string

Definition at line 51 of file OutputDevice_String.cpp.

References myStream.

Referenced by GUICompleteSchemeStorage::writeSettings().

00051                                        {
00052     return myStream.str();
00053 }

OutputDevice & OutputDevice::indent (  )  throw () [inherited]

Adds indentation.

An intendation, depending on the current xml-element-stack size, is written.

Returns:
The OutputDevice for further processing

Definition at line 171 of file OutputDevice.cpp.

References OutputDevice::getOStream(), OutputDevice::myXMLStack, and OutputDevice::postWriteHook().

Referenced by OutputDevice::closeTag().

00171                              {
00172     getOStream() << std::string(4*myXMLStack.size(), ' ');
00173     postWriteHook();
00174     return *this;
00175 }

void OutputDevice::inform ( const std::string &  msg  )  [inherited]

Retrieves a message to this device.

Implementation of the MessageRetriever interface. Writes the given message to the output device.

Parameters:
[in] msg The msg to write to the device

Definition at line 209 of file OutputDevice.cpp.

References OutputDevice::getOStream(), and OutputDevice::postWriteHook().

00209                                          {
00210     getOStream() << msg << '\n';
00211     postWriteHook();
00212 }

bool OutputDevice::ok (  )  throw () [virtual, inherited]

returns the information whether one can write into the device

Returns:
Whether the device can be used (stream is good)

Definition at line 128 of file OutputDevice.cpp.

References OutputDevice::getOStream().

00128                          {
00129     return getOStream().good();
00130 }

OutputDevice & OutputDevice::openTag ( const std::string &  xmlElement  )  throw () [inherited]

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.

Parameters:
[in] xmlElement Name of element to open
Returns:
The OutputDevice for further processing

Definition at line 179 of file OutputDevice.cpp.

References OutputDevice::getOStream(), OutputDevice::myXMLStack, and OutputDevice::postWriteHook().

Referenced by MSPersonControl::erase(), MSVehicleControl::scheduleVehicleRemoval(), RORouteDef_Complete::writeXMLDefinition(), RORouteDef_Alternatives::writeXMLDefinition(), RORoute::writeXMLDefinition(), OutputDevice::writeXMLHeader(), and MSVehicle::writeXMLRoute().

00179                                                          {
00180     getOStream() << std::string(4*myXMLStack.size(), ' ') << "<" << xmlElement;
00181     postWriteHook();
00182     myXMLStack.push_back(xmlElement);
00183     return *this;
00184 }

template<class T>
OutputDevice& OutputDevice::operator<< ( const T &  t  )  [inline, inherited]

Abstract output operator.

Returns:
The OutputDevice for further processing

Definition at line 222 of file OutputDevice.h.

References OutputDevice::getOStream(), and OutputDevice::postWriteHook().

00222                                          {
00223         getOStream() << t;
00224         postWriteHook();
00225         return *this;
00226     }

void OutputDevice::postWriteHook (  )  throw () [protected, virtual, inherited]

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 OutputDevice::closeTag(), OutputDevice::indent(), OutputDevice::inform(), OutputDevice::openTag(), and OutputDevice::operator<<().

00205 {}

void OutputDevice::setPrecision ( unsigned int  precision = OUTPUT_ACCURACY  )  throw () [inherited]

Sets the precison or resets it to default.

Parameters:
[in] precision The accuracy (number of digits behind '.') to set

Definition at line 146 of file OutputDevice.cpp.

References OutputDevice::getOStream().

Referenced by MSVTypeProbe::execute(), OutputDevice::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 () [inherited]

Writes an XML header with optional configuration.

If something has been written (myXMLStack is not empty), nothing is written and false returned.

Parameters:
[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)
Todo:
Check which parameter is used herein
Todo:
Describe what is saved

Definition at line 152 of file OutputDevice.cpp.

References OptionsCont::getOptions(), OutputDevice::getOStream(), OutputDevice::myXMLStack, OutputDevice::openTag(), and OptionsCont::writeXMLHeader().

Referenced by OutputDevice::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 }


Field Documentation

std::ostringstream OutputDevice_String::myStream [private]

The wrapped ofstream.

Definition at line 76 of file OutputDevice_String.h.

Referenced by getOStream(), and getString().


The documentation for this class was generated from the following files:

Generated on Wed May 5 00:06:58 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6