A toolkit for working with phylogenetic data.
v0.24.0
gzip_output_target.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_GZIP_OUTPUT_TARGET_H_
2 #define GENESIS_UTILS_IO_GZIP_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 <cassert>
39 #include <memory>
40 #include <string>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Gzip Output Target
47 // =================================================================================================
48 
59 class GzipOutputTarget final : public BaseOutputTarget
60 {
61 public:
62 
63  // -------------------------------------------------------------
64  // Constructors and Rule of Five
65  // -------------------------------------------------------------
66 
72  explicit GzipOutputTarget(
73  std::shared_ptr<BaseOutputTarget> output_target,
75  )
76  : output_target_( output_target )
77  , level_( level )
78  {}
79 
80  GzipOutputTarget( GzipOutputTarget const& ) = delete;
81  GzipOutputTarget( GzipOutputTarget&& ) = default;
82 
83  GzipOutputTarget& operator= ( GzipOutputTarget const& ) = delete;
85 
86  ~GzipOutputTarget() override = default;
87 
88  // -------------------------------------------------------------
89  // Overloaded Internal Members
90  // -------------------------------------------------------------
91 
92 private:
93 
97  std::ostream& out_stream_() override
98  {
99  // Lazy loading. Needed in case we want to write in parallel to many files - having all
100  // open when creating the output targets might overflow the file pointers.
101  if( !stream_ || !stream_->good() ) {
102  stream_ = make_unique<GzipOStream>( output_target_->ostream(), level_ );
103  }
104  assert( stream_ );
105  assert( stream_->good() );
106  return *stream_;
107  }
108 
117  std::string target_name_() const override
118  {
119  if( level_ == GzipCompressionLevel::kNoCompression ) {
120  return output_target_->target_name();
121  }
122  return "gzip-compressed " + output_target_->target_name();
123  }
124 
128  std::string target_string_() const override
129  {
130  return output_target_->target_name();
131  }
132 
133  // -------------------------------------------------------------
134  // Member Variables
135  // -------------------------------------------------------------
136 
137  // Need the wrapped target in order to keep it alive when it was handed over from
138  // a function such as to_file()
139  std::shared_ptr<BaseOutputTarget> output_target_;
140 
141  GzipCompressionLevel level_;
142  std::unique_ptr<GzipOStream> stream_;
143 
144 };
145 
146 } // namespace utils
147 } // namespace genesis
148 
149 #endif // include guard
GzipCompressionLevel
List of possible compression levels used for GzipOStream.
Abstract base class for writing data to an output target.
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Provides some valuable additions to STD.
GzipOutputTarget & operator=(GzipOutputTarget const &)=delete
Output target for writing byte data to a gzip/zlib-compressed target.
~GzipOutputTarget() override=default
GzipOutputTarget(std::shared_ptr< BaseOutputTarget > output_target, GzipCompressionLevel level=GzipCompressionLevel::kDefaultCompression)
Construct the output target using another output target (FileOutputTarget, StringOutputTarget, StreamOutputTarget, etc), and add gzip/zlib compression on top, using the specified GzipCompressionLevel%.