Pakman
OutputStreamHandler.cc
1 #include <string>
2 #include <fstream>
3 
4 #include "core/common.h"
5 
6 #include "OutputStreamHandler.h"
7 
8 // Initialise OutputStreamHandler's static data member
9 OutputStreamHandler* OutputStreamHandler::s_instance = nullptr;
10 
11 // Return singleton instance
13 {
14  if (s_instance == nullptr)
15  s_instance = new OutputStreamHandler(g_output_file);
16 
17  return s_instance;
18 }
19 
20 // Close file if a filename was given
22 {
23  if (s_instance)
24  {
25  delete s_instance;
26  s_instance = nullptr;
27  }
28 }
29 
30 // Return reference to output stream
32 {
33  return *m_p_output_stream;
34 }
35 
36 // Private default constructor
37 OutputStreamHandler::OutputStreamHandler(const std::string& filename)
38  : m_filename(filename)
39 {
40  // Make output file if a filename was given (default is &std::cout)
41  if (!m_filename.empty())
42  m_p_output_stream = new std::ofstream(filename);
43 }
44 
45 // Private destructor
46 OutputStreamHandler::~OutputStreamHandler()
47 {
48  // Close output file if a filename was given
49  if (!m_filename.empty())
50  delete m_p_output_stream;
51 }
static OutputStreamHandler * instance()
std::string g_output_file
Definition: main.cc:34
std::ostream & getOutputStream()