Chemical Data Processing Library C++ API - Version 1.4.0
CompressedDataWriter.hpp
Go to the documentation of this file.
1 /*
2  * CompressedDataWriter.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library 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 GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_COMPRESSEDDATAWRITER_HPP
30 #define CDPL_UTIL_COMPRESSEDDATAWRITER_HPP
31 
32 #include <iosfwd>
33 #include <functional>
34 
35 #include "CDPL/Base/DataWriter.hpp"
37 
38 
39 namespace CDPL
40 {
41 
42  namespace Util
43  {
44 
53  template <typename WriterImpl, typename CompStream, typename DataType = typename WriterImpl::DataType>
54  class CompressedDataWriter : public Base::DataWriter<DataType>
55  {
56 
57  public:
63  CompressedDataWriter(std::ostream& os);
64 
70  CompressedDataWriter& write(const DataType& obj);
71 
75  void close();
76 
81  operator const void*() const;
82 
87  bool operator!() const;
88 
89  private:
90  CompStream stream;
91  WriterImpl writer;
92  };
93  } // namespace Util
94 } // namespace CDPL
95 
96 
97 // Implementation
98 
99 template <typename WriterImpl, typename DecompStream, typename DataType>
101  stream(os), writer(stream)
102 {
103  writer.setParent(this);
104  writer.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
105 }
106 
107 template <typename WriterImpl, typename DecompStream, typename DataType>
110 {
111  writer.write(obj);
112 
113  return *this;
114 }
115 
116 template <typename WriterImpl, typename DecompStream, typename DataType>
118 {
119  writer.close();
120  stream.close();
121 }
122 
123 template <typename WriterImpl, typename DecompStream, typename DataType>
125 {
126  return writer.operator const void*();
127 }
128 
129 template <typename WriterImpl, typename DecompStream, typename DataType>
131 {
132  return writer.operator!();
133 }
134 
135 #endif // CDPL_UTIL_COMPRESSEDDATAWRITER_HPP
Definition of I/O-stream classes capable of performing compression and decompression.
Definition of class CDPL::Base::DataWriter.
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
Interface for writing data objects of a given type to an arbitrary data sink.
Definition: DataWriter.hpp:63
typename WriterImpl::DataType DataType
The type of the written data objects.
Definition: DataWriter.hpp:74
Convenience wrapper that combines a Util::CompressionOStream of type CompStream with a stream-based w...
Definition: CompressedDataWriter.hpp:55
CompressedDataWriter(std::ostream &os)
Constructs the CompressedDataWriter instance that compresses output written to os via CompStream and ...
Definition: CompressedDataWriter.hpp:100
CompressedDataWriter & write(const DataType &obj)
Writes obj via the wrapped writer.
Definition: CompressedDataWriter.hpp:109
bool operator!() const
Tells whether the writer is in a bad (non-writable) state.
Definition: CompressedDataWriter.hpp:130
void close()
Closes the wrapped writer and finalizes the compression stream.
Definition: CompressedDataWriter.hpp:117
The namespace of the Chemical Data Processing Library.