Variable.cpp
430 lines
| 12.4 KiB
| text/x-c
|
CppLexer
|
r163 | #include "Variable/Variable.h" | ||
|
r164 | #include <Data/IDataSeries.h> | ||
r512 | #include <Data/SqpRange.h> | |||
r226 | ||||
r541 | #include <QMutex> | |||
r364 | #include <QReadWriteLock> | |||
#include <QThread> | ||||
r226 | Q_LOGGING_CATEGORY(LOG_Variable, "Variable") | |||
|
r164 | |||
|
r1279 | namespace { | ||
/** | ||||
* Searches in metadata for a value that can be converted to DataSeriesType | ||||
* @param metadata the metadata where to search | ||||
* @return the value converted to a DataSeriesType if it was found, UNKNOWN type otherwise | ||||
* @sa DataSeriesType | ||||
*/ | ||||
DataSeriesType findDataSeriesType(const QVariantHash &metadata) | ||||
{ | ||||
auto dataSeriesType = DataSeriesType::UNKNOWN; | ||||
// Go through the metadata and stop at the first value that could be converted to DataSeriesType | ||||
for (auto it = metadata.cbegin(), end = metadata.cend(); | ||||
it != end && dataSeriesType == DataSeriesType::UNKNOWN; ++it) { | ||||
dataSeriesType = DataSeriesTypeUtils::fromString(it.value().toString()); | ||||
} | ||||
return dataSeriesType; | ||||
} | ||||
} // namespace | ||||
|
r163 | struct Variable::VariablePrivate { | ||
r756 | explicit VariablePrivate(const QString &name, const QVariantHash &metadata) | |||
|
r654 | : m_Name{name}, | ||
r756 | m_Range{INVALID_RANGE}, | |||
m_CacheRange{INVALID_RANGE}, | ||||
|
r654 | m_Metadata{metadata}, | ||
m_DataSeries{nullptr}, | ||||
|
r716 | m_RealRange{INVALID_RANGE}, | ||
|
r1279 | m_NbPoints{0}, | ||
m_Type{findDataSeriesType(m_Metadata)} | ||||
|
r163 | { | ||
} | ||||
|
r707 | VariablePrivate(const VariablePrivate &other) | ||
: m_Name{other.m_Name}, | ||||
m_Range{other.m_Range}, | ||||
r756 | m_CacheRange{other.m_CacheRange}, | |||
|
r707 | m_Metadata{other.m_Metadata}, | ||
m_DataSeries{other.m_DataSeries != nullptr ? other.m_DataSeries->clone() : nullptr}, | ||||
|
r716 | m_RealRange{other.m_RealRange}, | ||
|
r1279 | m_NbPoints{other.m_NbPoints}, | ||
m_Type{findDataSeriesType(m_Metadata)} | ||||
|
r707 | { | ||
} | ||||
r541 | void lockRead() { m_Lock.lockForRead(); } | |||
void lockWrite() { m_Lock.lockForWrite(); } | ||||
void unlock() { m_Lock.unlock(); } | ||||
|
r677 | void purgeDataSeries() | ||
{ | ||||
if (m_DataSeries) { | ||||
m_DataSeries->purge(m_CacheRange.m_TStart, m_CacheRange.m_TEnd); | ||||
} | ||||
updateRealRange(); | ||||
|
r718 | updateNbPoints(); | ||
|
r677 | } | ||
|
r718 | void updateNbPoints() { m_NbPoints = m_DataSeries ? m_DataSeries->nbPoints() : 0; } | ||
|
r654 | /// 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); | ||||
r756 | m_RealRange | |||
= (minXAxisIt != end && maxXAxisIt != end && minXAxisIt->x() <= maxXAxisIt->x()) | ||||
? SqpRange{minXAxisIt->x(), maxXAxisIt->x()} | ||||
: INVALID_RANGE; | ||||
|
r654 | m_DataSeries->unlock(); | ||
} | ||||
else { | ||||
m_RealRange = INVALID_RANGE; | ||||
} | ||||
} | ||||
|
r163 | QString m_Name; | ||
r226 | ||||
r532 | SqpRange m_Range; | |||
SqpRange m_CacheRange; | ||||
|
r406 | QVariantHash m_Metadata; | ||
r542 | std::shared_ptr<IDataSeries> m_DataSeries; | |||
|
r654 | SqpRange m_RealRange; | ||
|
r716 | int m_NbPoints; | ||
|
r1279 | DataSeriesType m_Type; | ||
r541 | ||||
QReadWriteLock m_Lock; | ||||
|
r163 | }; | ||
r756 | Variable::Variable(const QString &name, const QVariantHash &metadata) | |||
: impl{spimpl::make_unique_impl<VariablePrivate>(name, metadata)} | ||||
|
r163 | { | ||
} | ||||
|
r707 | 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); | ||||
} | ||||
|
r163 | QString Variable::name() const noexcept | ||
{ | ||||
r541 | impl->lockRead(); | |||
auto name = impl->m_Name; | ||||
impl->unlock(); | ||||
return name; | ||||
|
r163 | } | ||
|
r686 | void Variable::setName(const QString &name) noexcept | ||
{ | ||||
impl->lockWrite(); | ||||
impl->m_Name = name; | ||||
impl->unlock(); | ||||
} | ||||
r532 | SqpRange Variable::range() const noexcept | |||
r235 | { | |||
r541 | impl->lockRead(); | |||
auto range = impl->m_Range; | ||||
impl->unlock(); | ||||
return range; | ||||
r235 | } | |||
r532 | void Variable::setRange(const SqpRange &range) noexcept | |||
r260 | { | |||
r541 | impl->lockWrite(); | |||
r532 | impl->m_Range = range; | |||
|
r654 | impl->updateRealRange(); | ||
r541 | impl->unlock(); | |||
r532 | } | |||
SqpRange Variable::cacheRange() const noexcept | ||||
{ | ||||
r541 | impl->lockRead(); | |||
auto cacheRange = impl->m_CacheRange; | ||||
impl->unlock(); | ||||
return cacheRange; | ||||
r532 | } | |||
void Variable::setCacheRange(const SqpRange &cacheRange) noexcept | ||||
{ | ||||
r541 | impl->lockWrite(); | |||
|
r677 | if (cacheRange != impl->m_CacheRange) { | ||
impl->m_CacheRange = cacheRange; | ||||
} | ||||
r541 | impl->unlock(); | |||
r260 | } | |||
|
r716 | int Variable::nbPoints() const noexcept | ||
{ | ||||
return impl->m_NbPoints; | ||||
} | ||||
|
r654 | SqpRange Variable::realRange() const noexcept | ||
{ | ||||
return impl->m_RealRange; | ||||
} | ||||
r538 | void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept | |||
{ | ||||
r542 | qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries" | |||
r541 | << QThread::currentThread()->objectName(); | |||
r538 | if (!dataSeries) { | |||
/// @todo ALX : log | ||||
return; | ||||
} | ||||
|
r1284 | auto dataInit = false; | ||
r538 | // Add or merge the data | |||
r541 | impl->lockWrite(); | |||
|
r310 | if (!impl->m_DataSeries) { | ||
impl->m_DataSeries = dataSeries->clone(); | ||||
|
r1284 | dataInit = true; | ||
|
r310 | } | ||
else { | ||||
r235 | impl->m_DataSeries->merge(dataSeries.get()); | |||
} | ||||
|
r677 | impl->purgeDataSeries(); | ||
r541 | impl->unlock(); | |||
|
r1284 | |||
if (dataInit) { | ||||
emit dataInitialized(); | ||||
} | ||||
|
r164 | } | ||
|
r182 | |||
r816 | ||||
r542 | std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept | |||
|
r182 | { | ||
r541 | impl->lockRead(); | |||
r542 | auto dataSeries = impl->m_DataSeries; | |||
r541 | impl->unlock(); | |||
return dataSeries; | ||||
|
r182 | } | ||
r226 | ||||
|
r1279 | DataSeriesType Variable::type() const noexcept | ||
{ | ||||
impl->lockRead(); | ||||
auto type = impl->m_Type; | ||||
impl->unlock(); | ||||
return type; | ||||
} | ||||
|
r406 | QVariantHash Variable::metadata() const noexcept | ||
{ | ||||
r541 | impl->lockRead(); | |||
auto metadata = impl->m_Metadata; | ||||
impl->unlock(); | ||||
return metadata; | ||||
|
r406 | } | ||
r532 | bool Variable::contains(const SqpRange &range) const noexcept | |||
r226 | { | |||
r541 | impl->lockRead(); | |||
auto res = impl->m_Range.contains(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r226 | } | |||
r258 | ||||
r532 | bool Variable::intersect(const SqpRange &range) const noexcept | |||
r258 | { | |||
r541 | ||||
impl->lockRead(); | ||||
auto res = impl->m_Range.intersect(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r258 | } | |||
r318 | ||||
r532 | bool Variable::isInside(const SqpRange &range) const noexcept | |||
r318 | { | |||
r541 | impl->lockRead(); | |||
auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd}); | ||||
impl->unlock(); | ||||
return res; | ||||
r532 | } | |||
bool Variable::cacheContains(const SqpRange &range) const noexcept | ||||
{ | ||||
r541 | impl->lockRead(); | |||
auto res = impl->m_CacheRange.contains(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r532 | } | |||
bool Variable::cacheIntersect(const SqpRange &range) const noexcept | ||||
{ | ||||
r541 | impl->lockRead(); | |||
auto res = impl->m_CacheRange.intersect(range); | ||||
impl->unlock(); | ||||
return res; | ||||
r532 | } | |||
bool Variable::cacheIsInside(const SqpRange &range) const noexcept | ||||
{ | ||||
r541 | impl->lockRead(); | |||
auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd}); | ||||
impl->unlock(); | ||||
return res; | ||||
r532 | } | |||
r541 | QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept | |||
r532 | { | |||
r570 | // This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange | |||
r532 | auto notInCache = QVector<SqpRange>{}; | |||
r756 | 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(); | ||||
} | ||||
r532 | } | |||
} | ||||
r756 | else { | |||
notInCache << range; | ||||
} | ||||
r532 | ||||
return notInCache; | ||||
r318 | } | |||
r570 | ||||
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>{}; | ||||
r756 | if (impl->m_CacheRange != INVALID_RANGE) { | |||
r816 | if (this->cacheIntersect(range)) { | |||
r756 | 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(); | ||||
} | ||||
r570 | } | |||
} | ||||
return inCache; | ||||
} | ||||
r816 | ||||
QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &oldRange, | ||||
const SqpRange &nextRange) | ||||
{ | ||||
// This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange | ||||
auto notInCache = QVector<SqpRange>{}; | ||||
if (oldRange != INVALID_RANGE) { | ||||
if (!oldRange.contains(nextRange)) { | ||||
if (nextRange.m_TEnd <= oldRange.m_TStart || nextRange.m_TStart >= oldRange.m_TEnd) { | ||||
notInCache << nextRange; | ||||
} | ||||
else if (nextRange.m_TStart < oldRange.m_TStart | ||||
&& nextRange.m_TEnd <= oldRange.m_TEnd) { | ||||
notInCache << SqpRange{nextRange.m_TStart, oldRange.m_TStart}; | ||||
} | ||||
else if (nextRange.m_TStart < oldRange.m_TStart && nextRange.m_TEnd > oldRange.m_TEnd) { | ||||
notInCache << SqpRange{nextRange.m_TStart, oldRange.m_TStart} | ||||
<< SqpRange{oldRange.m_TEnd, nextRange.m_TEnd}; | ||||
} | ||||
else if (nextRange.m_TStart < oldRange.m_TEnd) { | ||||
notInCache << SqpRange{oldRange.m_TEnd, nextRange.m_TEnd}; | ||||
} | ||||
else { | ||||
qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | ||||
<< QThread::currentThread(); | ||||
} | ||||
} | ||||
} | ||||
else { | ||||
notInCache << nextRange; | ||||
} | ||||
return notInCache; | ||||
} | ||||
QVector<SqpRange> Variable::provideInCacheRangeList(const SqpRange &oldRange, | ||||
const SqpRange &nextRange) | ||||
{ | ||||
// This code assume that cach is contigue. Can return 0 or 1 SqpRange | ||||
auto inCache = QVector<SqpRange>{}; | ||||
if (oldRange != INVALID_RANGE) { | ||||
if (oldRange.intersect(nextRange)) { | ||||
if (nextRange.m_TStart <= oldRange.m_TStart && nextRange.m_TEnd >= oldRange.m_TStart | ||||
&& nextRange.m_TEnd < oldRange.m_TEnd) { | ||||
inCache << SqpRange{oldRange.m_TStart, nextRange.m_TEnd}; | ||||
} | ||||
else if (nextRange.m_TStart >= oldRange.m_TStart | ||||
&& nextRange.m_TEnd <= oldRange.m_TEnd) { | ||||
inCache << nextRange; | ||||
} | ||||
else if (nextRange.m_TStart > oldRange.m_TStart && nextRange.m_TEnd > oldRange.m_TEnd) { | ||||
inCache << SqpRange{nextRange.m_TStart, oldRange.m_TEnd}; | ||||
} | ||||
else if (nextRange.m_TStart <= oldRange.m_TStart | ||||
&& nextRange.m_TEnd >= oldRange.m_TEnd) { | ||||
inCache << oldRange; | ||||
} | ||||
else { | ||||
qCCritical(LOG_Variable()) << tr("Detection of unknown case.") | ||||
<< QThread::currentThread(); | ||||
} | ||||
} | ||||
} | ||||
return inCache; | ||||
} | ||||