Enhance interface of AbstractTaskProgress

* Rename handleTaskProgress() to taskProgressEvent()
* Add task control features (asyncStop() + taskStopEvent())
This commit is contained in:
Hugues Delorme 2012-07-12 09:50:42 +02:00
parent 19b790f61e
commit 075077b4e1
3 changed files with 52 additions and 14 deletions

View File

@ -16,7 +16,8 @@ public:
m_rangeMin(-1.),
m_rangeMax(-2.),
m_rangeLength(0.),
m_progressThreshold(0.01) // Notifies each percent only
m_progressThreshold(0.01), // Notifies each percent only
m_isTaskStopRequested(false)
{
}
@ -26,6 +27,7 @@ public:
double m_rangeMax;
double m_rangeLength;
double m_progressThreshold;
bool m_isTaskStopRequested;
};
} // namespace internal
@ -82,7 +84,7 @@ void AbstractTaskProgress::setValue(double v)
{
if (std::fabs(v - d->m_value) > std::fabs(d->m_progressThreshold * d->m_rangeLength)) {
d->m_value = v;
this->handleProgressUpdate();
this->progressUpdateEvent();
}
}
@ -96,9 +98,19 @@ void AbstractTaskProgress::setProgressUpdateThreshold(double v)
d->m_progressThreshold = v;
}
bool AbstractTaskProgress::isStopRequested() const
void AbstractTaskProgress::asyncTaskStop()
{
return false;
d->m_isTaskStopRequested = true;
}
bool AbstractTaskProgress::isTaskStopRequested() const
{
return d->m_isTaskStopRequested;
}
void AbstractTaskProgress::taskStoppedEvent()
{
d->m_isTaskStopRequested = false;
}
void AbstractTaskProgress::reset()
@ -107,6 +119,7 @@ void AbstractTaskProgress::reset()
d->m_value = -1.;
d->m_rangeMin = -1.;
d->m_rangeMax = -2.;
d->m_isTaskStopRequested = false;
}
} // namespace foug

View File

@ -25,10 +25,14 @@ public:
double progressUpdateThreshold() const;
void setProgressUpdateThreshold(double v);
virtual bool isStopRequested() const;
virtual void handleProgressUpdate() = 0;
virtual void reset();
void asyncTaskStop();
bool isTaskStopRequested() const;
virtual void taskStoppedEvent();
virtual void progressUpdateEvent() = 0;
private:
internal::AbstractTaskProgressPrivate* const d;
};

View File

@ -90,6 +90,9 @@ bool Io::read(AbstractGeometryBuilder* builder)
// const int facetCount = (fileSize - stlHeaderSize - stlFacetCountSize) / stlFacetSize;
if (this->stream() == 0)
return false;
AbstractStream* istream = this->stream();
AbstractTaskProgress* progress = this->taskProgress();
const UInt32 chunkSize = stlTriangleDataSize * 163;
@ -153,25 +156,36 @@ bool Io::read(AbstractGeometryBuilder* builder)
builder->processNextTriangle(triangle, attributeByteCount);
bufferOffset += stlTriangleDataSize;
}
} // end for
if (progress != 0)
progress->setValue(amountReadSize / stlTriangleDataSize);
if (progress != 0) {
if (progress->isTaskStopRequested()) {
streamError = true;
progress->taskStoppedEvent();
}
else {
progress->setValue(amountReadSize / stlTriangleDataSize);
}
}
amountReadSize += iReadSize;
}
else {
streamError = true;
}
}
} // end while
builder->endTriangles();
if (!streamError)
builder->endTriangles();
return !streamError;
}
bool Io::write(const stl::AbstractGeometry& geom, const AbstractGeometryExtraData* extraData)
{
if (this->stream() == 0)
return false;
AbstractStream* ostream = this->stream();
AbstractTaskProgress* progress = this->taskProgress();
@ -232,9 +246,16 @@ bool Io::write(const stl::AbstractGeometry& geom, const AbstractGeometryExtraDat
!= stlTriangleDataSize)
return false;
if (progress != 0)
progress->setValue(facet + 1);
}
if (progress != 0) {
if (progress->isTaskStopRequested()) {
progress->taskStoppedEvent();
return false;
}
else {
progress->setValue(facet + 1);
}
}
} // end for
return true;
}