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