A Discrete-Event Network Simulator
API
Public Member Functions | Private Attributes | List of all members
ns3::OutputStreamWrapper Class Reference


A class encapsulating an output stream. More...

#include "output-stream-wrapper.h"

+ Inheritance diagram for ns3::OutputStreamWrapper:

Public Member Functions

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

Private Attributes

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

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 70 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 29 of file output-stream-wrapper.cc.

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

◆ OutputStreamWrapper() [2/2]

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

Constructor.

Parameters
osoutput stream

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

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

◆ ~OutputStreamWrapper()

ns3::OutputStreamWrapper::~OutputStreamWrapper ( )

Member Function Documentation

◆ GetStream()

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

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 58 of file output-stream-wrapper.cc.

References m_ostream, and NS_LOG_FUNCTION.

Referenced by ns3::AsciiLrWpanMacTransmitSinkWithContext(), ns3::AsciiLrWpanMacTransmitSinkWithoutContext(), ns3::AsciiPhyReceiveSinkWithContext(), ns3::AsciiPhyReceiveSinkWithoutContext(), ns3::AsciiPhyTransmitSinkWithContext(), ns3::AsciiPhyTransmitSinkWithoutContext(), ns3::WimaxHelper::AsciiRxEvent(), ns3::WimaxHelper::AsciiTxEvent(), BytesInQueueTrace(), ns3::MobilityHelper::CourseChanged(), CwndChange(), CwndTracer(), Ns3TcpLossTestCase::CwndTracer(), ns3::AsciiTraceHelper::DefaultDequeueSinkWithContext(), ns3::AsciiTraceHelper::DefaultDequeueSinkWithoutContext(), ns3::AsciiTraceHelper::DefaultDropSinkWithContext(), ns3::AsciiTraceHelper::DefaultDropSinkWithoutContext(), ns3::AsciiTraceHelper::DefaultEnqueueSinkWithContext(), ns3::AsciiTraceHelper::DefaultEnqueueSinkWithoutContext(), ns3::AsciiTraceHelper::DefaultReceiveSinkWithContext(), ns3::AsciiTraceHelper::DefaultReceiveSinkWithoutContext(), Ns3TcpLossTestCase::DoRun(), Ns3TcpStateTestCase::DoRun(), DroppingStateTracer(), EveryDropTracer(), GoodputSampling(), InFlightTracer(), ns3::Ipv4L3ProtocolDropSinkWithContext(), ns3::Ipv4L3ProtocolDropSinkWithoutContext(), ns3::Ipv4L3ProtocolRxSinkWithContext(), ns3::Ipv4L3ProtocolRxSinkWithoutContext(), ns3::Ipv4L3ProtocolTxSinkWithContext(), ns3::Ipv4L3ProtocolTxSinkWithoutContext(), ns3::Ipv6L3ProtocolDropSinkWithContext(), ns3::Ipv6L3ProtocolDropSinkWithoutContext(), ns3::Ipv6L3ProtocolRxSinkWithContext(), ns3::Ipv6L3ProtocolRxSinkWithoutContext(), ns3::Ipv6L3ProtocolTxSinkWithContext(), ns3::Ipv6L3ProtocolTxSinkWithoutContext(), LimitsTrace(), NextRxTracer(), NextTxTracer(), ns3::dsdv::RoutingTableEntry::Print(), ns3::aodv::RoutingTableEntry::Print(), ns3::dsdv::RoutingTable::Print(), ns3::aodv::RoutingTable::Print(), ns3::ArpCache::PrintArpCache(), ns3::Ipv4RoutingHelper::PrintArpCache(), ns3::Ipv4RoutingHelper::PrintArpCacheEvery(), ns3::NdiscCache::PrintNdiscCache(), ns3::Ipv6RoutingHelper::PrintNdiscCache(), ns3::Ipv6RoutingHelper::PrintNdiscCacheEvery(), ns3::aodv::RoutingProtocol::PrintRoutingTable(), ns3::dsdv::RoutingProtocol::PrintRoutingTable(), ns3::Ipv4StaticRouting::PrintRoutingTable(), ns3::Ipv4ListRouting::PrintRoutingTable(), ns3::Ipv6ListRouting::PrintRoutingTable(), ns3::Ipv4GlobalRouting::PrintRoutingTable(), ns3::Rip::PrintRoutingTable(), ns3::RipNg::PrintRoutingTable(), ns3::Ipv6StaticRouting::PrintRoutingTable(), ns3::olsr::RoutingProtocol::PrintRoutingTable(), QueueLengthTracer(), RtoTracer(), RttTracer(), SojournTracer(), SsThreshTracer(), and ns3::WriteAveragePowerSpectralDensityReport().

Member Data Documentation

◆ m_destroyable

bool ns3::OutputStreamWrapper::m_destroyable
private

Can be destroyed.

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

Referenced by ~OutputStreamWrapper().

◆ m_ostream

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

The output stream.

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

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


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