A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
output-stream-wrapper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#ifndef OUTPUT_STREAM_WRAPPER_H
19#define OUTPUT_STREAM_WRAPPER_H
20
21#include "ns3/object.h"
22#include "ns3/ptr.h"
23#include "ns3/simple-ref-count.h"
24
25#include <fstream>
26
27namespace ns3
28{
29
30/**
31 * @brief A class encapsulating an output stream.
32 *
33 * This class wraps a pointer to a C++ std::ostream and provides
34 * reference counting of the object. This class is recommended for users
35 * who want to pass output streams in the ns-3 APIs, such as in callbacks
36 * or tracing.
37 *
38 * This class is motivated by the fact that in C++, copy and assignment of
39 * iostreams is forbidden by std::basic_ios<>, because it is not possible
40 * to predict the semantics of the stream desired by a user.
41 *
42 * When writing traced information to a file, the tempting ns-3 idiom is to
43 * create a bound callback with an ofstream as the bound object. Unfortunately,
44 * this implies a copy construction in order to get the ofstream object into the
45 * callback. This operation, as mentioned above, is forbidden by the STL.
46 * Using this class in ns-3 APIs is generally preferable to passing global
47 * pointers to ostream objects, or passing a pointer to a stack allocated
48 * ostream (which creates object lifetime issues).
49 *
50 * One could imagine having this object inherit from stream to get the various
51 * overloaded operator<< defined, but we're going to be using a
52 * Ptr<OutputStreamWrapper> when passing this object around. In this case, the
53 * Ptr<> wouldn't understand the operators and we would have to dereference it
54 * to access the underlying object methods. Since we would have to dereference
55 * the Ptr<>, we don't bother and just expect the user to Get a saved pointer
56 * to an ostream and dereference it him or herself. As in:
57 *
58 * \verbatim
59 * void
60 * TraceSink (Ptr<OutputStreamWrapper> streamWrapper, Ptr<const Packet> packet)
61 * {
62 * std::ostream *stream = streamWrapper->GetStream ();
63 * *stream << "got packet" << std::endl;
64 * }
65 * \endverbatim
66 *
67 *
68 * This class uses a basic ns-3 reference counting base class but is not
69 * an ns3::Object with attributes, TypeId, or aggregation.
70 */
71class OutputStreamWrapper : public SimpleRefCount<OutputStreamWrapper>
72{
73 public:
74 /**
75 * Constructor
76 * \param filename file name
77 * \param filemode std::ios::openmode flags
78 */
79 OutputStreamWrapper(std::string filename, std::ios::openmode filemode);
80 /**
81 * Constructor
82 * \param os output stream
83 */
84 OutputStreamWrapper(std::ostream* os);
86
87 /**
88 * Return a pointer to an ostream previously set in the wrapper.
89 *
90 * \see SetStream
91 *
92 * \returns a pointer to the encapsulated std::ostream
93 */
94 std::ostream* GetStream();
95
96 private:
97 std::ostream* m_ostream; //!< The output stream
98 bool m_destroyable; //!< Can be destroyed
99};
100
101} // namespace ns3
102
103#endif /* OUTPUT_STREAM_WRAPPER_H */
A class encapsulating an output stream.
std::ostream * GetStream()
Return a pointer to an ostream previously set in the wrapper.
bool m_destroyable
Can be destroyed.
std::ostream * m_ostream
The output stream.
A template-based reference counting class.
Every class exported by the ns3 library is enclosed in the ns3 namespace.