Chemical Data Processing Library C++ API - Version 1.1.1
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 
48  template <typename ReaderImpl, typename DecompStream, typename DataType = typename ReaderImpl::DataType>
49  class CompressedDataReader : public Base::DataReader<DataType>
50  {
51 
52  public:
53  CompressedDataReader(std::istream& is);
54 
55  CompressedDataReader& read(DataType& obj, bool overwrite = true);
56  CompressedDataReader& read(std::size_t idx, DataType& obj, bool overwrite = true);
57 
59 
60  bool hasMoreData();
61 
62  std::size_t getRecordIndex() const;
63  void setRecordIndex(std::size_t idx);
64 
65  std::size_t getNumRecords();
66 
67  operator const void*() const;
68  bool operator!() const;
69 
70  void close();
71 
72  private:
73  DecompStream stream;
74  ReaderImpl reader;
75  };
76  } // namespace Util
77 } // namespace CDPL
78 
79 
80 // Implementation
81 
82 template <typename ReaderImpl, typename DecompStream, typename DataType>
84  stream(is), reader(stream)
85 {
86  reader.setParent(this);
87  reader.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
88 }
89 
90 template <typename ReaderImpl, typename DecompStream, typename DataType>
93 {
94  reader.read(obj, overwrite);
95 
96  return *this;
97 }
98 
99 template <typename ReaderImpl, typename DecompStream, typename DataType>
102 {
103  reader.read(idx, obj, overwrite);
104 
105  return *this;
106 }
107 
108 template <typename ReaderImpl, typename DecompStream, typename DataType>
111 {
112  reader.skip();
113 
114  return *this;
115 }
116 
117 template <typename ReaderImpl, typename DecompStream, typename DataType>
119 {
120  return reader.hasMoreData();
121 }
122 
123 template <typename ReaderImpl, typename DecompStream, typename DataType>
125 {
126  return reader.getRecordIndex();
127 }
128 
129 template <typename ReaderImpl, typename DecompStream, typename DataType>
131 {
132  reader.setRecordIndex(idx);
133 }
134 
135 template <typename ReaderImpl, typename DecompStream, typename DataType>
137 {
138  return reader.getNumRecords();
139 }
140 
141 template <typename ReaderImpl, typename DecompStream, typename DataType>
143 {
144  return reader.operator const void*();
145 }
146 
147 template <typename ReaderImpl, typename DecompStream, typename DataType>
149 {
150  return reader.operator!();
151 }
152 
153 template <typename ReaderImpl, typename DecompStream, typename DataType>
155 {
156  reader.close();
157  stream.close();
158 }
159 
160 #endif // CDPL_UTIL_COMPRESSEDDATAREADER_HPP
CDPL::Util::CompressedDataReader::close
void close()
Performs a reader specific shutdown operation (if required).
Definition: CompressedDataReader.hpp:154
CDPL::Util::CompressedDataReader::setRecordIndex
void setRecordIndex(std::size_t idx)
Sets the index of the current data record to idx.
Definition: CompressedDataReader.hpp:130
CDPL::Base::DataReader< typename ReaderImpl::DataType >::DataType
typename ReaderImpl::DataType DataType
The type of the read data objects.
Definition: DataReader.hpp:79
DataReader.hpp
Definition of the class CDPL::Base::DataReader.
CDPL::Base::DataIOBase::invokeIOCallbacks
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
CDPL::Util::CompressedDataReader::getNumRecords
std::size_t getNumRecords()
Returns the total number of available data records.
Definition: CompressedDataReader.hpp:136
CDPL::Base::DataReader
An interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
CDPL::Util::CompressedDataReader::read
CompressedDataReader & read(DataType &obj, bool overwrite=true)
Definition: CompressedDataReader.hpp:92
CDPL::Util::CompressedDataReader::hasMoreData
bool hasMoreData()
Tells if there are any data records left to read.
Definition: CompressedDataReader.hpp:118
CDPL::Util::CompressedDataReader::skip
CompressedDataReader & skip()
Skips the data record at the current record index.
Definition: CompressedDataReader.hpp:110
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::CompressedDataReader
CompressedDataReader.
Definition: CompressedDataReader.hpp:50
CDPL::Util::CompressedDataReader::getRecordIndex
std::size_t getRecordIndex() const
Definition: CompressedDataReader.hpp:124
CompressionStreams.hpp
Provides I/O-streams capable of performing compression and decompression.
CDPL::Util::CompressedDataReader::operator!
bool operator!() const
Definition: CompressedDataReader.hpp:148
CDPL::Util::CompressedDataReader::CompressedDataReader
CompressedDataReader(std::istream &is)
Definition: CompressedDataReader.hpp:83