29 #ifndef CDPL_UTIL_COMPOUNDDATAREADER_HPP
30 #define CDPL_UTIL_COMPOUNDDATAREADER_HPP
52 template <
typename DataType>
178 operator const void*()
const;
187 ReaderType* getReaderForRecordIndex(std::size_t& idx)
const;
189 typedef std::vector<ReaderPointer> ReaderArray;
190 typedef std::vector<std::size_t> RecordIndexArray;
194 RecordIndexArray recordIdxBounds;
195 std::size_t recordIdx;
196 std::size_t numRecords;
204 template <
typename DataType>
206 state(false), recordIdx(0), numRecords(0)
209 template <
typename DataType>
215 template <
typename DataType>
218 std::for_each(readers.begin(), readers.end(),
219 std::bind(&ReaderType::setParent, std::placeholders::_1,
222 recordIdxBounds.clear();
229 template <
typename DataType>
232 readers.reserve(readers.size() + 1);
233 recordIdxBounds.reserve(readers.size() + 1);
235 reader->setParent(
this);
237 std::size_t num_recs = reader->getNumRecords();
239 readers.push_back(reader);
240 numRecords += num_recs;
241 recordIdxBounds.push_back(numRecords);
243 state |=
static_cast<bool>(reader->operator
const void*());
246 template <
typename DataType>
249 if (idx >= readers.size())
252 std::size_t num_lost_records = readers[idx]->getNumRecords();
254 readers[idx]->setParent(0);
256 readers.erase(readers.begin() + idx);
257 recordIdxBounds.erase(recordIdxBounds.begin() + idx);
259 for ( ; idx < readers.size(); idx++)
260 recordIdxBounds[idx] -= num_lost_records;
262 numRecords -= num_lost_records;
265 template <
typename DataType>
268 return readers.size();
271 template <
typename DataType>
275 if (idx >= readers.size())
281 template <
typename DataType>
287 if (recordIdx >= numRecords)
290 std::size_t idx = recordIdx;
291 ReaderType* reader = getReaderForRecordIndex(idx);
293 if (reader && (state = reader->
read(idx, obj, overwrite))) {
295 this->invokeIOCallbacks(1.0);
301 template <
typename DataType>
307 if (idx >= numRecords)
310 std::size_t tmp_idx = idx;
311 ReaderType* reader = getReaderForRecordIndex(tmp_idx);
316 if ((state = reader->
read(tmp_idx, obj, overwrite))) {
318 this->invokeIOCallbacks(1.0);
325 template <
typename DataType>
331 if (recordIdx >= numRecords)
337 this->invokeIOCallbacks(1.0);
342 template <
typename DataType>
345 return (recordIdx < numRecords);
348 template <
typename DataType>
354 template <
typename DataType>
357 if (idx > numRecords)
363 template <
typename DataType>
369 template <
typename DataType>
372 return (state ?
this : 0);
375 template <
typename DataType>
381 template <
typename DataType>
384 for (std::size_t i = 0; i < readers.size(); i++) {
385 if (idx < recordIdxBounds[i])
392 template <
typename DataType>
396 for (std::size_t i = 0; i < readers.size(); i++) {
397 if (idx < recordIdxBounds[i]) {
398 idx -= (i == 0 ? std::size_t(0) : recordIdxBounds[i - 1]);
399 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:355
void addReader(const ReaderPointer &reader)
Appends reader to the list of underlying readers.
Definition: CompoundDataReader.hpp:230
CompoundDataReader()
Constructs an empty CompoundDataReader instance.
Definition: CompoundDataReader.hpp:205
void clear()
Removes all underlying readers.
Definition: CompoundDataReader.hpp:216
std::size_t getNumReaders()
Returns the number of underlying readers.
Definition: CompoundDataReader.hpp:266
std::size_t getRecordIndex() const
Returns the current global record index.
Definition: CompoundDataReader.hpp:349
CompoundDataReader & operator=(const CompoundDataReader &)=delete
bool operator!() const
Tells whether the reader is in a bad (non-readable) state.
Definition: CompoundDataReader.hpp:376
const ReaderPointer & getReader(std::size_t idx) const
Returns the underlying reader at index idx.
Definition: CompoundDataReader.hpp:273
CompoundDataReader(const CompoundDataReader &)=delete
~CompoundDataReader()
Destructor.
Definition: CompoundDataReader.hpp:210
CompoundDataReader & skip()
Skips the next record.
Definition: CompoundDataReader.hpp:327
ReaderType::SharedPointer ReaderPointer
Shared-pointer type for the underlying readers.
Definition: CompoundDataReader.hpp:70
void removeReader(std::size_t idx)
Removes the reader at index idx from the list of underlying readers.
Definition: CompoundDataReader.hpp:247
std::shared_ptr< CompoundDataReader > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated CompoundDataReader instances.
Definition: CompoundDataReader.hpp:60
bool hasMoreData()
Tells whether more records are available.
Definition: CompoundDataReader.hpp:343
CompoundDataReader & read(DataType &obj, bool overwrite=true)
Reads the next record into obj.
Definition: CompoundDataReader.hpp:283
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:382
Base::DataReader< DataType > ReaderType
Type of the underlying Base::DataReader specialization.
Definition: CompoundDataReader.hpp:65
std::size_t getNumRecords()
Returns the total number of records across all underlying readers.
Definition: CompoundDataReader.hpp:364
The namespace of the Chemical Data Processing Library.