Variable.cpp
270 lines
| 7.3 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 { | ||
r471 | explicit VariablePrivate(const QString &name, const SqpRange &dateTime, | |||
Alexandre Leroux
|
r373 | const QVariantHash &metadata) | ||
Alexandre Leroux
|
r611 | : m_Name{name}, | ||
m_Range{dateTime}, | ||||
m_Metadata{metadata}, | ||||
m_DataSeries{nullptr}, | ||||
m_RealRange{INVALID_RANGE} | ||||
Alexandre Leroux
|
r151 | { | ||
} | ||||
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
|
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); | ||||
m_RealRange = (minXAxisIt != end && maxXAxisIt != end) | ||||
? SqpRange{minXAxisIt->x(), maxXAxisIt->x()} | ||||
: INVALID_RANGE; | ||||
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; | ||
r512 | ||||
QReadWriteLock m_Lock; | ||||
Alexandre Leroux
|
r151 | }; | ||
r471 | Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata) | |||
Alexandre Leroux
|
r373 | : impl{spimpl::make_unique_impl<VariablePrivate>(name, dateTime, metadata)} | ||
Alexandre Leroux
|
r151 | { | ||
} | ||||
QString Variable::name() const noexcept | ||||
{ | ||||
r512 | impl->lockRead(); | |||
auto name = impl->m_Name; | ||||
impl->unlock(); | ||||
return name; | ||||
Alexandre Leroux
|
r151 | } | ||
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
|
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>{}; | |||
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} | ||||
r509 | << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; | |||
r503 | } | |||
else if (range.m_TStart < impl->m_CacheRange.m_TEnd) { | ||||
r509 | notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; | |||
r503 | } | |||
else { | ||||
qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | ||||
<< QThread::currentThread(); | ||||
} | ||||
} | ||||
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>{}; | ||||
r552 | 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}; | ||||
r537 | } | |||
else if (range.m_TStart >= impl->m_CacheRange.m_TStart | ||||
r552 | && range.m_TEnd <= impl->m_CacheRange.m_TEnd) { | |||
r537 | inCache << range; | |||
} | ||||
r552 | 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; | ||||
r537 | } | |||
else { | ||||
qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | ||||
<< QThread::currentThread(); | ||||
} | ||||
} | ||||
return inCache; | ||||
} | ||||