29 #ifndef CDPL_UTIL_COMPOUNDDATAREADER_HPP
30 #define CDPL_UTIL_COMPOUNDDATAREADER_HPP
52 template <
typename DataType>
171 operator const void*()
const;
180 ReaderType* getReaderForRecordIndex(std::size_t& idx)
const;
182 typedef std::vector<ReaderPointer> ReaderArray;
183 typedef std::vector<std::size_t> RecordIndexArray;
187 RecordIndexArray recordIdxBounds;
188 std::size_t recordIdx;
189 std::size_t numRecords;
197 template <
typename DataType>
199 state(false), recordIdx(0), numRecords(0)
202 template <
typename DataType>
208 template <
typename DataType>
211 std::for_each(readers.begin(), readers.end(),
212 std::bind(&ReaderType::setParent, std::placeholders::_1,
215 recordIdxBounds.clear();
222 template <
typename DataType>
225 readers.reserve(readers.size() + 1);
226 recordIdxBounds.reserve(readers.size() + 1);
228 reader->setParent(
this);
230 std::size_t num_recs = reader->getNumRecords();
232 readers.push_back(reader);
233 numRecords += num_recs;
234 recordIdxBounds.push_back(numRecords);
236 state |=
static_cast<bool>(reader->operator
const void*());
239 template <
typename DataType>
242 if (idx >= readers.size())
245 std::size_t num_lost_records = readers[idx]->getNumRecords();
247 readers[idx]->setParent(0);
249 readers.erase(readers.begin() + idx);
250 recordIdxBounds.erase(recordIdxBounds.begin() + idx);
252 for ( ; idx < readers.size(); idx++)
253 recordIdxBounds[idx] -= num_lost_records;
255 numRecords -= num_lost_records;
258 template <
typename DataType>
261 return readers.size();
264 template <
typename DataType>
268 if (idx >= readers.size())
274 template <
typename DataType>
280 if (recordIdx >= numRecords)
283 std::size_t idx = recordIdx;
284 ReaderType* reader = getReaderForRecordIndex(idx);
286 if (reader && (state = reader->
read(idx, obj, overwrite))) {
288 this->invokeIOCallbacks(1.0);
294 template <
typename DataType>
300 if (idx >= numRecords)
303 std::size_t tmp_idx = idx;
304 ReaderType* reader = getReaderForRecordIndex(tmp_idx);
309 if ((state = reader->
read(tmp_idx, obj, overwrite))) {
311 this->invokeIOCallbacks(1.0);
318 template <
typename DataType>
324 if (recordIdx >= numRecords)
330 this->invokeIOCallbacks(1.0);
335 template <
typename DataType>
338 return (recordIdx < numRecords);
341 template <
typename DataType>
347 template <
typename DataType>
350 if (idx > numRecords)
356 template <
typename DataType>
362 template <
typename DataType>
365 return (state ?
this : 0);
368 template <
typename DataType>
374 template <
typename DataType>
377 for (std::size_t i = 0; i < readers.size(); i++) {
378 if (idx < recordIdxBounds[i])
385 template <
typename DataType>
389 for (std::size_t i = 0; i < readers.size(); i++) {
390 if (idx < recordIdxBounds[i]) {
391 idx -= (i == 0 ? std::size_t(0) : recordIdxBounds[i - 1]);
392 return readers[i].get();
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
std::shared_ptr< DataReader > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated DataReader instances.
Definition: DataReader.hpp:84
virtual DataReader & read(DataType &obj, bool overwrite=true)=0
Reads the data record at the current record index and stores the read data in obj.
DataType 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
Composite Base::DataReader that aggregates several underlying readers and presents their records as o...
Definition: CompoundDataReader.hpp:54
void setRecordIndex(std::size_t idx)
Sets the current global record index.
Definition: CompoundDataReader.hpp:348
void addReader(const ReaderPointer &reader)
Appends reader to the list of underlying readers.
Definition: CompoundDataReader.hpp:223
CompoundDataReader()
Constructs an empty CompoundDataReader instance.
Definition: CompoundDataReader.hpp:198
void clear()
Removes all underlying readers.
Definition: CompoundDataReader.hpp:209
std::size_t getNumReaders()
Returns the number of underlying readers.
Definition: CompoundDataReader.hpp:259
std::size_t getRecordIndex() const
Returns the current global record index.
Definition: CompoundDataReader.hpp:342
CompoundDataReader & operator=(const CompoundDataReader &)=delete
bool operator!() const
Tells whether the reader is in a bad (non-readable) state.
Definition: CompoundDataReader.hpp:369
const ReaderPointer & getReader(std::size_t idx) const
Returns the underlying reader at index idx.
Definition: CompoundDataReader.hpp:266
CompoundDataReader(const CompoundDataReader &)=delete
~CompoundDataReader()
Destructor.
Definition: CompoundDataReader.hpp:203
CompoundDataReader & skip()
Skips the next record.
Definition: CompoundDataReader.hpp:320
ReaderType::SharedPointer ReaderPointer
Shared-pointer type for the underlying readers.
Definition: CompoundDataReader.hpp:63
void removeReader(std::size_t idx)
Removes the reader at index idx from the list of underlying readers.
Definition: CompoundDataReader.hpp:240
std::shared_ptr< CompoundDataReader > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated CompoundDataReader instances.
Definition: CompoundDataReader.hpp:58
bool hasMoreData()
Tells whether more records are available.
Definition: CompoundDataReader.hpp:336
CompoundDataReader & read(DataType &obj, bool overwrite=true)
Reads the next record into obj.
Definition: CompoundDataReader.hpp:276
std::size_t getReaderIDForRecordIndex(std::size_t idx) const
Returns the index of the underlying reader that owns the global record index idx.
Definition: CompoundDataReader.hpp:375
Base::DataReader< DataType > ReaderType
Type of the underlying Base::DataReader specialization.
Definition: CompoundDataReader.hpp:61
std::size_t getNumRecords()
Returns the total number of records across all underlying readers.
Definition: CompoundDataReader.hpp:357
The namespace of the Chemical Data Processing Library.