A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3::OutputStreamWrapper Class Reference

A class encapsulating an output stream. More...

#include "output-stream-wrapper.h"

+ Inheritance diagram for ns3::OutputStreamWrapper:
+ Collaboration diagram for ns3::OutputStreamWrapper:

Public Member Functions

 OutputStreamWrapper (std::ostream *os)
 Constructor.
 
 OutputStreamWrapper (std::string filename, std::ios::openmode filemode)
 Constructor.
 
 ~OutputStreamWrapper ()
 
std::ostream * GetStream ()
 Return a pointer to an ostream previously set in the wrapper.
 
- Public Member Functions inherited from ns3::SimpleRefCount< OutputStreamWrapper >
 SimpleRefCount ()
 Default constructor.
 
 SimpleRefCount (const SimpleRefCount &o)
 Copy constructor.
 
uint32_t GetReferenceCount () const
 Get the reference count of the object.
 
SimpleRefCountoperator= (const SimpleRefCount &o)
 Assignment operator.
 
void Ref () const
 Increment the reference count.
 
void Unref () const
 Decrement the reference count.
 

Private Attributes

bool m_destroyable
 Can be destroyed.
 
std::ostream * m_ostream
 The output stream.
 

Detailed Description

A class encapsulating an output stream.

This class wraps a pointer to a C++ std::ostream and provides reference counting of the object. This class is recommended for users who want to pass output streams in the ns-3 APIs, such as in callbacks or tracing.

This class is motivated by the fact that in C++, copy and assignment of iostreams is forbidden by std::basic_ios<>, because it is not possible to predict the semantics of the stream desired by a user.

When writing traced information to a file, the tempting ns-3 idiom is to create a bound callback with an ofstream as the bound object. Unfortunately, this implies a copy construction in order to get the ofstream object into the callback. This operation, as mentioned above, is forbidden by the STL. Using this class in ns-3 APIs is generally preferable to passing global pointers to ostream objects, or passing a pointer to a stack allocated ostream (which creates object lifetime issues).

One could imagine having this object inherit from stream to get the various overloaded operator<< defined, but we're going to be using a Ptr<OutputStreamWrapper> when passing this object around. In this case, the Ptr<> wouldn't understand the operators and we would have to dereference it to access the underlying object methods. Since we would have to dereference the Ptr<>, we don't bother and just expect the user to Get a saved pointer to an ostream and dereference it him or herself. As in:

*   void
*   TraceSink (Ptr<OutputStreamWrapper> streamWrapper, Ptr<const Packet> packet)
*   {
*     std::ostream *stream = streamWrapper->GetStream ();
*     *stream << "got packet" << std::endl;
*   }
* 

This class uses a basic ns-3 reference counting base class but is not an ns3::Object with attributes, TypeId, or aggregation.

Definition at line 71 of file output-stream-wrapper.h.

Constructor & Destructor Documentation

◆ OutputStreamWrapper() [1/2]

ns3::OutputStreamWrapper::OutputStreamWrapper ( std::string  filename,
std::ios::openmode  filemode 
)

Constructor.

Parameters
filenamefile name
filemodestd::ios::openmode flags

Definition at line 31 of file output-stream-wrapper.cc.

References m_ostream, NS_ABORT_MSG_UNLESS, NS_LOG_FUNCTION, and ns3::FatalImpl::RegisterStream().

+ Here is the call graph for this function:

◆ OutputStreamWrapper() [2/2]

ns3::OutputStreamWrapper::OutputStreamWrapper ( std::ostream *  os)

Constructor.

Parameters
osoutput stream

Definition at line 44 of file output-stream-wrapper.cc.

References m_ostream, NS_ABORT_MSG_UNLESS, NS_LOG_FUNCTION, and ns3::FatalImpl::RegisterStream().

+ Here is the call graph for this function:

◆ ~OutputStreamWrapper()

ns3::OutputStreamWrapper::~OutputStreamWrapper ( )

Definition at line 53 of file output-stream-wrapper.cc.

References m_destroyable, m_ostream, NS_LOG_FUNCTION, and ns3::FatalImpl::UnregisterStream().

+ Here is the call graph for this function:

Member Function Documentation

◆ GetStream()

std::ostream * ns3::OutputStreamWrapper::GetStream ( )

Return a pointer to an ostream previously set in the wrapper.

See also
SetStream
Returns
a pointer to the encapsulated std::ostream

Definition at line 65 of file output-stream-wrapper.cc.

References m_ostream, and NS_LOG_FUNCTION.

Referenced by Ns3TcpLossTestCase::CwndTracer(), Ns3TcpLossTestCase::DoRun(), ns3::V4TraceRoute::HandleWaitReplyTimeout(), LogEndToEndGain(), ns3::V4TraceRoute::Receive(), ns3::V4TraceRoute::StartApplication(), and ns3::V4TraceRoute::StopApplication().

+ Here is the caller graph for this function:

Member Data Documentation

◆ m_destroyable

bool ns3::OutputStreamWrapper::m_destroyable
private

Can be destroyed.

Definition at line 98 of file output-stream-wrapper.h.

Referenced by ~OutputStreamWrapper().

◆ m_ostream

std::ostream* ns3::OutputStreamWrapper::m_ostream
private

The output stream.

Definition at line 97 of file output-stream-wrapper.h.

Referenced by OutputStreamWrapper(), ~OutputStreamWrapper(), and GetStream().


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