A library for working with phylogenetic and population genetic data.
v0.27.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
fs.hpp
Provides functions for accessing the file system.
genesis::utils::BaseOutputTarget
Abstract base class for writing data to an output target.
Definition: base_output_target.hpp:48
output_stream.hpp
genesis::utils::FileOutputTarget
Output target for writing data to a file.
Definition: file_output_target.hpp:53
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::FileOutputTarget::~FileOutputTarget
~FileOutputTarget() override=default
genesis::utils::FileOutputTarget::FileOutputTarget
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.
Definition: file_output_target.hpp:64
genesis::utils::FileOutputTarget::operator=
FileOutputTarget & operator=(FileOutputTarget const &)=delete
base_output_target.hpp
genesis::utils::file_output_stream
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out, bool create_dirs=true)
Helper function to obtain an output stream to a file.
Definition: output_stream.hpp:69