A class encapsulating an output stream. More...
#include "output-stream-wrapper.h"
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. | |
SimpleRefCount & | operator= (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. | |
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:
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 60 of file output-stream-wrapper.h.
ns3::OutputStreamWrapper::OutputStreamWrapper | ( | std::string | filename, |
std::ios::openmode | filemode ) |
Constructor.
filename | file name |
filemode | std::ios::openmode flags |
Definition at line 20 of file output-stream-wrapper.cc.
References m_ostream, NS_ABORT_MSG_UNLESS, NS_LOG_FUNCTION, and ns3::FatalImpl::RegisterStream().
ns3::OutputStreamWrapper::OutputStreamWrapper | ( | std::ostream * | os | ) |
Constructor.
os | output stream |
Definition at line 33 of file output-stream-wrapper.cc.
References m_ostream, NS_ABORT_MSG_UNLESS, NS_LOG_FUNCTION, and ns3::FatalImpl::RegisterStream().
ns3::OutputStreamWrapper::~OutputStreamWrapper | ( | ) |
Definition at line 42 of file output-stream-wrapper.cc.
References m_destroyable, m_ostream, NS_LOG_FUNCTION, and ns3::FatalImpl::UnregisterStream().
std::ostream * ns3::OutputStreamWrapper::GetStream | ( | ) |
Return a pointer to an ostream previously set in the wrapper.
Definition at line 54 of file output-stream-wrapper.cc.
References m_ostream, and NS_LOG_FUNCTION.
|
private |
Can be destroyed.
Definition at line 87 of file output-stream-wrapper.h.
Referenced by ~OutputStreamWrapper().
|
private |
The output stream.
Definition at line 86 of file output-stream-wrapper.h.
Referenced by OutputStreamWrapper(), OutputStreamWrapper(), ~OutputStreamWrapper(), and GetStream().