Variable.cpp
315 lines
| 8.6 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r151 | #include "Variable/Variable.h" | ||
Alexandre Leroux
|
r152 | #include <Data/IDataSeries.h> | ||
r471 | #include <Data/SqpRange.h> | |||
r210 | ||||
r512 | #include <QMutex> | |||
r336 | #include <QReadWriteLock> | |||
#include <QThread> | ||||
r210 | Q_LOGGING_CATEGORY(LOG_Variable, "Variable") | |||
Alexandre Leroux
|
r152 | |||
Alexandre Leroux
|
r151 | struct Variable::VariablePrivate { | ||
r699 | explicit VariablePrivate(const QString &name, const QVariantHash &metadata) | |||
Alexandre Leroux
|
r611 | : m_Name{name}, | ||
r699 | m_Range{INVALID_RANGE}, | |||
m_CacheRange{INVALID_RANGE}, | ||||
Alexandre Leroux
|
r611 | m_Metadata{metadata}, | ||
m_DataSeries{nullptr}, | ||||
Alexandre Leroux
|
r659 | m_RealRange{INVALID_RANGE}, | ||
m_NbPoints{0} | ||||
Alexandre Leroux
|
r151 | { | ||
} | ||||
Alexandre Leroux
|
r651 | VariablePrivate(const VariablePrivate &other) | ||
: m_Name{other.m_Name}, | ||||
m_Range{other.m_Range}, | ||||
r699 | m_CacheRange{other.m_CacheRange}, | |||
Alexandre Leroux
|
r651 | m_Metadata{other.m_Metadata}, | ||
m_DataSeries{other.m_DataSeries != nullptr ? other.m_DataSeries->clone() : nullptr}, | ||||
Alexandre Leroux
|
r659 | m_RealRange{other.m_RealRange}, | ||
m_NbPoints{other.m_NbPoints} | ||||
Alexandre Leroux
|
r651 | { | ||
} | ||||
r512 | void lockRead() { m_Lock.lockForRead(); } | |||
void lockWrite() { m_Lock.lockForWrite(); } | ||||
void unlock() { m_Lock.unlock(); } | ||||
Alexandre Leroux
|
r630 | void purgeDataSeries() | ||
{ | ||||
if (m_DataSeries) { | ||||
m_DataSeries->purge(m_CacheRange.m_TStart, m_CacheRange.m_TEnd); | ||||
} | ||||
updateRealRange(); | ||||
Alexandre Leroux
|
r661 | updateNbPoints(); | ||
Alexandre Leroux
|
r630 | } | ||
Alexandre Leroux
|
r661 | void updateNbPoints() { m_NbPoints = m_DataSeries ? m_DataSeries->nbPoints() : 0; } | ||
Alexandre Leroux
|
r611 | /// Updates real range according to current variable range and data series | ||
void updateRealRange() | ||||
{ | ||||
if (m_DataSeries) { | ||||
m_DataSeries->lockRead(); | ||||
auto end = m_DataSeries->cend(); | ||||
auto minXAxisIt = m_DataSeries->minXAxisData(m_Range.m_TStart); | ||||
auto maxXAxisIt = m_DataSeries->maxXAxisData(m_Range.m_TEnd); | ||||
r699 | m_RealRange | |||
= (minXAxisIt != end && maxXAxisIt != end && minXAxisIt->x() <= maxXAxisIt->x()) | ||||
? SqpRange{minXAxisIt->x(), maxXAxisIt->x()} | ||||
: INVALID_RANGE; | ||||
Alexandre Leroux
|
r611 | m_DataSeries->unlock(); | ||
} | ||||
else { | ||||
m_RealRange = INVALID_RANGE; | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r151 | QString m_Name; | ||
r210 | ||||
r503 | SqpRange m_Range; | |||
SqpRange m_CacheRange; | ||||
Alexandre Leroux
|
r373 | QVariantHash m_Metadata; | ||
r513 | std::shared_ptr<IDataSeries> m_DataSeries; | |||
Alexandre Leroux
|
r611 | SqpRange m_RealRange; | ||
Alexandre Leroux
|
r659 | int m_NbPoints; | ||
r512 | ||||
QReadWriteLock m_Lock; | ||||
Alexandre Leroux
|
r151 | }; | ||
r699 | Variable::Variable(const QString &name, const QVariantHash &metadata) | |||
: impl{spimpl::make_unique_impl<VariablePrivate>(name, metadata)} | ||||
Alexandre Leroux
|
r151 | { | ||
} | ||||
Alexandre Leroux
|
r651 | Variable::Variable(const Variable &other) | ||
: impl{spimpl::make_unique_impl<VariablePrivate>(*other.impl)} | ||||
{ | ||||
} | ||||
std::shared_ptr<Variable> Variable::clone() const | ||||
{ | ||||
return std::make_shared<Variable>(*this); | ||||
} | ||||
Alexandre Leroux
|
r151 | QString Variable::name() const noexcept | ||
{ | ||||
r512 | impl->lockRead(); | |||
auto name = impl->m_Name; | ||||
impl->unlock(); | ||||
return name; | ||||
Alexandre Leroux
|
r151 | } | ||
Alexandre Leroux
|
r638 | void Variable::setName(const QString &name) noexcept | ||
{ | ||||
impl->lockWrite(); | ||||
impl->m_Name = name; | ||||
impl->unlock(); | ||||
} | ||||
r503 | SqpRange Variable::range() const noexcept | |||
r219 | { | |||
r512 | impl->lockRead(); | |||
auto range = impl->m_Range; | ||||
impl->unlock(); | ||||
return range; | ||||
r219 | } | |||
r503 | void Variable::setRange(const SqpRange &range) noexcept | |||
r241 | { | |||
r512 | impl->lockWrite(); | |||
r503 | impl->m_Range = range; | |||
Alexandre Leroux
|
r611 | impl->updateRealRange(); | ||
r512 | impl->unlock(); | |||
r503 | } | |||
SqpRange Variable::cacheRange() const noexcept | ||||
{ | ||||
r512 | impl->lockRead(); | |||
auto cacheRange = impl->m_CacheRange; | ||||
impl->unlock(); | ||||
return cacheRange; | ||||
r503 | } | |||
void Variable::setCacheRange(const SqpRange &cacheRange) noexcept | ||||
{ | ||||
r512 | impl->lockWrite(); | |||
Alexandre Leroux
|
r630 | if (cacheRange != impl->m_CacheRange) { | ||
impl->m_CacheRange = cacheRange; | ||||
impl->purgeDataSeries(); | ||||
} | ||||
r512 | impl->unlock(); | |||
r241 | } | |||
Alexandre Leroux
|
r659 | int Variable::nbPoints() const noexcept | ||
{ | ||||
return impl->m_NbPoints; | ||||
} | ||||
Alexandre Leroux
|
r611 | SqpRange Variable::realRange() const noexcept | ||
{ | ||||
return impl->m_RealRange; | ||||
} | ||||
r509 | void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept | |||
{ | ||||
r513 | qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries" | |||
r512 | << QThread::currentThread()->objectName(); | |||
r509 | if (!dataSeries) { | |||
/// @todo ALX : log | ||||
return; | ||||
} | ||||
// Add or merge the data | ||||
r512 | impl->lockWrite(); | |||
Alexandre Leroux
|
r287 | if (!impl->m_DataSeries) { | ||
impl->m_DataSeries = dataSeries->clone(); | ||||
} | ||||
else { | ||||
r219 | impl->m_DataSeries->merge(dataSeries.get()); | |||
} | ||||
Alexandre Leroux
|
r630 | impl->purgeDataSeries(); | ||
r512 | impl->unlock(); | |||
Alexandre Leroux
|
r152 | } | ||
Alexandre Leroux
|
r169 | |||
r513 | std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept | |||
Alexandre Leroux
|
r169 | { | ||
r512 | impl->lockRead(); | |||
r513 | auto dataSeries = impl->m_DataSeries; | |||
r512 | impl->unlock(); | |||
return dataSeries; | ||||
Alexandre Leroux
|
r169 | } | ||
r210 | ||||
Alexandre Leroux
|
r373 | QVariantHash Variable::metadata() const noexcept | ||
{ | ||||
r512 | impl->lockRead(); | |||
auto metadata = impl->m_Metadata; | ||||
impl->unlock(); | ||||
return metadata; | ||||
Alexandre Leroux
|
r373 | } | ||
r503 | bool Variable::contains(const SqpRange &range) const noexcept | |||
r210 | { | |||
r512 | impl->lockRead(); | |||
auto res = impl->m_Range.contains(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r210 | } | |||
r240 | ||||
r503 | bool Variable::intersect(const SqpRange &range) const noexcept | |||
r240 | { | |||
r512 | ||||
impl->lockRead(); | ||||
auto res = impl->m_Range.intersect(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r240 | } | |||
r293 | ||||
r503 | bool Variable::isInside(const SqpRange &range) const noexcept | |||
r293 | { | |||
r512 | impl->lockRead(); | |||
auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd}); | ||||
impl->unlock(); | ||||
return res; | ||||
r503 | } | |||
bool Variable::cacheContains(const SqpRange &range) const noexcept | ||||
{ | ||||
r512 | impl->lockRead(); | |||
auto res = impl->m_CacheRange.contains(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r503 | } | |||
bool Variable::cacheIntersect(const SqpRange &range) const noexcept | ||||
{ | ||||
r512 | impl->lockRead(); | |||
auto res = impl->m_CacheRange.intersect(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r503 | } | |||
bool Variable::cacheIsInside(const SqpRange &range) const noexcept | ||||
{ | ||||
r512 | impl->lockRead(); | |||
auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd}); | ||||
impl->unlock(); | ||||
return res; | ||||
r503 | } | |||
r512 | QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept | |||
r503 | { | |||
r537 | // This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange | |||
r503 | auto notInCache = QVector<SqpRange>{}; | |||
r699 | if (impl->m_CacheRange != INVALID_RANGE) { | |||
if (!this->cacheContains(range)) { | ||||
if (range.m_TEnd <= impl->m_CacheRange.m_TStart | ||||
|| range.m_TStart >= impl->m_CacheRange.m_TEnd) { | ||||
notInCache << range; | ||||
} | ||||
else if (range.m_TStart < impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd <= impl->m_CacheRange.m_TEnd) { | ||||
notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart}; | ||||
} | ||||
else if (range.m_TStart < impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd > impl->m_CacheRange.m_TEnd) { | ||||
notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart} | ||||
<< SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; | ||||
} | ||||
else if (range.m_TStart < impl->m_CacheRange.m_TEnd) { | ||||
notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; | ||||
} | ||||
else { | ||||
qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | ||||
<< QThread::currentThread(); | ||||
} | ||||
r503 | } | |||
} | ||||
r699 | else { | |||
notInCache << range; | ||||
} | ||||
r503 | ||||
return notInCache; | ||||
r293 | } | |||
r537 | ||||
QVector<SqpRange> Variable::provideInCacheRangeList(const SqpRange &range) const noexcept | ||||
{ | ||||
// This code assume that cach in contigue. Can return 0 or 1 SqpRange | ||||
auto inCache = QVector<SqpRange>{}; | ||||
r699 | if (impl->m_CacheRange != INVALID_RANGE) { | |||
if (this->intersect(range)) { | ||||
if (range.m_TStart <= impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd >= impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd < impl->m_CacheRange.m_TEnd) { | ||||
inCache << SqpRange{impl->m_CacheRange.m_TStart, range.m_TEnd}; | ||||
} | ||||
else if (range.m_TStart >= impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd <= impl->m_CacheRange.m_TEnd) { | ||||
inCache << range; | ||||
} | ||||
else if (range.m_TStart > impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd > impl->m_CacheRange.m_TEnd) { | ||||
inCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TEnd}; | ||||
} | ||||
else if (range.m_TStart <= impl->m_CacheRange.m_TStart | ||||
&& range.m_TEnd >= impl->m_CacheRange.m_TEnd) { | ||||
inCache << impl->m_CacheRange; | ||||
} | ||||
else { | ||||
qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | ||||
<< QThread::currentThread(); | ||||
} | ||||
r537 | } | |||
} | ||||
return inCache; | ||||
} | ||||