A toolkit for working with phylogenetic data.
v0.24.0
file_output_target.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_FILE_OUTPUT_TARGET_H_
2 #define GENESIS_UTILS_IO_FILE_OUTPUT_TARGET_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
37 
38 #include <fstream>
39 #include <string>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // File Output Target
46 // =================================================================================================
47 
53 class FileOutputTarget final : public BaseOutputTarget
54 {
55 public:
56 
57  // -------------------------------------------------------------
58  // Constructors and Rule of Five
59  // -------------------------------------------------------------
60 
64  explicit FileOutputTarget(
65  std::string const& file_name,
66  std::ios_base::openmode mode = std::ios_base::out
67  )
68  : file_name_( file_name )
69  , mode_( mode )
70  {}
71 
72  FileOutputTarget( FileOutputTarget const& ) = delete;
73  FileOutputTarget( FileOutputTarget&& ) = default;
74 
75  FileOutputTarget& operator= ( FileOutputTarget const& ) = delete;
77 
78  ~FileOutputTarget() override = default;
79 
80  // -------------------------------------------------------------
81  // Overloaded Internal Members
82  // -------------------------------------------------------------
83 
84 private:
85 
89  std::ostream& out_stream_() override
90  {
91  // Lazy loading.
92  if( !stream_.is_open() ) {
93  file_output_stream( file_name_, stream_, mode_ );
94  }
95  return stream_;
96  }
97 
101  std::string target_name_() const override
102  {
103  return "output file (" + file_name_ + ")";
104  }
105 
109  std::string target_string_() const override
110  {
111  return file_name_;
112  }
113 
114  // -------------------------------------------------------------
115  // Member Variables
116  // -------------------------------------------------------------
117 
118  std::string file_name_;
119  std::ofstream stream_;
120 
121  std::ios_base::openmode mode_ = std::ios_base::out;
122 };
123 
124 } // namespace utils
125 } // namespace genesis
126 
127 #endif // include guard
Abstract base class for writing data to an output target.
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out)
Helper function to obtain an output stream to a file.
FileOutputTarget & operator=(FileOutputTarget const &)=delete
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Output target for writing data to a file.
Provides functions for accessing the file system.
~FileOutputTarget() override=default
FileOutputTarget(std::string const &file_name, std::ios_base::openmode mode=std::ios_base::out)
Construct the output target from a file with the given file name.