Chemical Data Processing Library C++ API - Version 1.4.0
CompressedDataReader.hpp
Go to the documentation of this file.
1 /*
2  * CompressedDataReader.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_COMPRESSEDDATAREADER_HPP
30 #define CDPL_UTIL_COMPRESSEDDATAREADER_HPP
31 
32 #include <iosfwd>
33 #include <functional>
34 
35 #include "CDPL/Base/DataReader.hpp"
37 
38 
39 namespace CDPL
40 {
41 
42  namespace Util
43  {
44 
58  template <typename ReaderImpl, typename DecompStream, typename DataType = typename ReaderImpl::DataType>
59  class CompressedDataReader : public Base::DataReader<DataType>
60  {
61 
62  public:
67  CompressedDataReader(std::istream& is);
68 
75  CompressedDataReader& read(DataType& obj, bool overwrite = true);
76 
84  CompressedDataReader& read(std::size_t idx, DataType& obj, bool overwrite = true);
85 
91 
96  bool hasMoreData();
97 
102  std::size_t getRecordIndex() const;
103 
108  void setRecordIndex(std::size_t idx);
109 
114  std::size_t getNumRecords();
115 
120  operator const void*() const;
121 
126  bool operator!() const;
127 
131  void close();
132 
133  private:
134  DecompStream stream;
135  ReaderImpl reader;
136  };
137  } // namespace Util
138 } // namespace CDPL
139 
140 
141 // Implementation
142 
143 template <typename ReaderImpl, typename DecompStream, typename DataType>
145  stream(is), reader(stream)
146 {
147  reader.setParent(this);
148  reader.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
149 }
150 
151 template <typename ReaderImpl, typename DecompStream, typename DataType>
154 {
155  reader.read(obj, overwrite);
156 
157  return *this;
158 }
159 
160 template <typename ReaderImpl, typename DecompStream, typename DataType>
163 {
164  reader.read(idx, obj, overwrite);
165 
166  return *this;
167 }
168 
169 template <typename ReaderImpl, typename DecompStream, typename DataType>
172 {
173  reader.skip();
174 
175  return *this;
176 }
177 
178 template <typename ReaderImpl, typename DecompStream, typename DataType>
180 {
181  return reader.hasMoreData();
182 }
183 
184 template <typename ReaderImpl, typename DecompStream, typename DataType>
186 {
187  return reader.getRecordIndex();
188 }
189 
190 template <typename ReaderImpl, typename DecompStream, typename DataType>
192 {
193  reader.setRecordIndex(idx);
194 }
195 
196 template <typename ReaderImpl, typename DecompStream, typename DataType>
198 {
199  return reader.getNumRecords();
200 }
201 
202 template <typename ReaderImpl, typename DecompStream, typename DataType>
204 {
205  return reader.operator const void*();
206 }
207 
208 template <typename ReaderImpl, typename DecompStream, typename DataType>
210 {
211  return reader.operator!();
212 }
213 
214 template <typename ReaderImpl, typename DecompStream, typename DataType>
216 {
217  reader.close();
218  stream.close();
219 }
220 
221 #endif // CDPL_UTIL_COMPRESSEDDATAREADER_HPP
Definition of I/O-stream classes capable of performing compression and decompression.
Definition of class CDPL::Base::DataReader.
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
Interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
typename ReaderImpl::DataType DataType
The type of the read data objects.
Definition: DataReader.hpp:79
Adapter that wraps a stream-based reader with a decompression stream to read records from a compresse...
Definition: CompressedDataReader.hpp:60
CompressedDataReader & read(DataType &obj, bool overwrite=true)
Reads the next data record into obj.
Definition: CompressedDataReader.hpp:153
bool hasMoreData()
Tells whether more records are available.
Definition: CompressedDataReader.hpp:179
void setRecordIndex(std::size_t idx)
Sets the index of the next data record to be read.
Definition: CompressedDataReader.hpp:191
CompressedDataReader & skip()
Skips the next data record.
Definition: CompressedDataReader.hpp:171
bool operator!() const
Tells whether the reader is in a bad state.
Definition: CompressedDataReader.hpp:209
CompressedDataReader(std::istream &is)
Constructs a CompressedDataReader instance reading from the input stream is.
Definition: CompressedDataReader.hpp:144
void close()
Closes the wrapped reader and the decompression stream.
Definition: CompressedDataReader.hpp:215
std::size_t getNumRecords()
Returns the total number of records in the input stream.
Definition: CompressedDataReader.hpp:197
std::size_t getRecordIndex() const
Returns the index of the current data record.
Definition: CompressedDataReader.hpp:185
The namespace of the Chemical Data Processing Library.