29 #ifndef CDPL_UTIL_STREAMDATAREADER_HPP
30 #define CDPL_UTIL_STREAMDATAREADER_HPP
71 template <
typename DataType,
typename ReaderImpl>
129 operator const void*()
const;
143 input(is), recordIndex(0), initStreamPos(is.tellg()), state(is.good()), streamScanned(false) {}
148 void scanDataStream();
150 typedef std::vector<std::istream::pos_type> RecordStreamPosTable;
153 std::size_t recordIndex;
154 std::istream::pos_type initStreamPos;
157 RecordStreamPosTable recordPositions;
165 template <
typename DataType,
typename ReaderImpl>
171 if ((state =
static_cast<ReaderImpl*
>(
this)->readData(input, obj, overwrite))) {
173 this->invokeIOCallbacks(1.0);
179 template <
typename DataType,
typename ReaderImpl>
187 if (idx >= recordPositions.size())
191 input.seekg(recordPositions[idx]);
195 if ((state =
static_cast<ReaderImpl*
>(
this)->readData(input, obj, overwrite))) {
197 this->invokeIOCallbacks(1.0);
203 template <
typename DataType,
typename ReaderImpl>
209 if ((state =
static_cast<ReaderImpl*
>(
this)->skipData(input))) {
211 this->invokeIOCallbacks(1.0);
217 template <
typename DataType,
typename ReaderImpl>
220 return static_cast<ReaderImpl*
>(
this)->moreData(input);
223 template <
typename DataType,
typename ReaderImpl>
229 template <
typename DataType,
typename ReaderImpl>
234 if (idx > recordPositions.size())
239 if (idx == recordPositions.size())
240 input.seekg(0, std::ios_base::end);
242 input.seekg(recordPositions[idx]);
247 template <
typename DataType,
typename ReaderImpl>
252 return recordPositions.size();
255 template <
typename DataType,
typename ReaderImpl>
258 return (state ?
this : 0);
261 template <
typename DataType,
typename ReaderImpl>
267 template <
typename DataType,
typename ReaderImpl>
273 streamScanned =
true;
275 std::size_t saved_rec_index = recordIndex;
280 input.seekg(0, std::ios_base::end);
282 std::istream::pos_type end_pos = input.tellg();
284 input.seekg(initStreamPos);
286 while (hasMoreData()) {
287 std::istream::pos_type record_pos = input.tellg();
290 if (!(state =
static_cast<ReaderImpl*
>(
this)->skipData(input)))
293 recordPositions.push_back(record_pos);
296 this->invokeIOCallbacks(record_pos /
double(end_pos));
299 this->invokeIOCallbacks(1.0);
301 if (saved_rec_index < recordPositions.size()) {
302 recordIndex = saved_rec_index;
305 input.seekg(recordPositions[recordIndex]);
Definition of exception classes.
Definition of class CDPL::Base::DataReader.
Interface for reading data objects of a given type from an arbitrary data source.
Definition: DataReader.hpp:73
Pharmacophore DataType
The type of the read data objects.
Definition: DataReader.hpp:79
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Helper class that implements Base::DataReader for std::istream based data readers.
Definition: StreamDataReader.hpp:73
bool operator!() const
Tells whether the reader is in a bad (non-readable) state.
Definition: StreamDataReader.hpp:262
std::size_t getNumRecords()
Returns the total number of records (scanning the stream on first call).
Definition: StreamDataReader.hpp:248
bool hasMoreData()
Tells whether more records are available by delegating to ReaderImpl::moreData().
Definition: StreamDataReader.hpp:218
Base::DataReader< DataType > & skip()
Skips the next data record by delegating to ReaderImpl::skipData().
Definition: StreamDataReader.hpp:205
std::size_t getRecordIndex() const
Returns the current record index.
Definition: StreamDataReader.hpp:224
void setRecordIndex(std::size_t idx)
Sets the current record index.
Definition: StreamDataReader.hpp:230
Base::DataReader< DataType > & read(DataType &obj, bool overwrite=true)
Reads the next data record into obj by delegating to ReaderImpl::readData().
Definition: StreamDataReader.hpp:167
Base::DataReader< DataType > & read(std::size_t idx, DataType &obj, bool overwrite=true)
Reads the data record at index idx into obj by delegating to ReaderImpl::readData().
Definition: StreamDataReader.hpp:181
StreamDataReader(std::istream &is)
Constructs a StreamDataReader instance that will read from the input stream is.
Definition: StreamDataReader.hpp:142
The namespace of the Chemical Data Processing Library.