From da8b6df7cbcfefd4478322cd5bae1adef4836e43 2017-10-30 16:21:17 From: Alexandre Leroux Date: 2017-10-30 16:21:17 Subject: [PATCH] Handles y-axis in DataSeries. If the y-axis is defined, it implies more controls during construction and merge --- diff --git a/core/include/Data/DataSeries.h b/core/include/Data/DataSeries.h index e7d74c9..5cd9df0 100644 --- a/core/include/Data/DataSeries.h +++ b/core/include/Data/DataSeries.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -156,7 +157,14 @@ public: bool isEmpty() const noexcept { return m_XAxisData->size() == 0; } - /// Merges into the data series an other data series + /// Merges into the data series an other data series. + /// + /// The two dataseries: + /// - must be of the same dimension + /// - must have the same y-axis (if defined) + /// + /// If the prerequisites are not valid, the method does nothing + /// /// @remarks the data series to merge with is cleared after the operation void merge(IDataSeries *dataSeries) override { @@ -164,7 +172,13 @@ public: lockWrite(); if (auto other = dynamic_cast *>(dataSeries)) { - DataSeriesMergeHelper::merge(*other, *this); + if (m_YAxis == other->m_YAxis) { + DataSeriesMergeHelper::merge(*other, *this); + } + else { + qCWarning(LOG_DataSeries()) + << QObject::tr("Can't merge data series that have not the same y-axis"); + } } else { qCWarning(LOG_DataSeries()) @@ -325,18 +339,31 @@ public: virtual void unlock() { m_Lock.unlock(); } protected: - /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a - /// DataSeries with no values will be created. + /// Protected ctor (DataSeries is abstract). + /// + /// Data vectors must be consistent with each other, otherwise an exception will be thrown (@sa + /// class description for consistent rules) /// @remarks data series is automatically sorted on its x-axis data + /// @throws std::invalid_argument if the data are inconsistent with each other explicit DataSeries(std::shared_ptr > xAxisData, const Unit &xAxisUnit, - std::shared_ptr > valuesData, const Unit &valuesUnit) + std::shared_ptr > valuesData, const Unit &valuesUnit, + OptionalAxis yAxis = OptionalAxis{}) : m_XAxisData{xAxisData}, m_XAxisUnit{xAxisUnit}, m_ValuesData{valuesData}, - m_ValuesUnit{valuesUnit} + m_ValuesUnit{valuesUnit}, + m_YAxis{std::move(yAxis)} { if (m_XAxisData->size() != m_ValuesData->size()) { - clear(); + throw std::invalid_argument{ + "The number of values by component must be equal to the number of x-axis data"}; + } + + // Validates y-axis (if defined) + if (yAxis.isDefined() && (yAxis.size() != m_ValuesData->componentCount())) { + throw std::invalid_argument{ + "As the y-axis is defined, the number of value components must be equal to the " + "number of y-axis data"}; } // Sorts data if it's not the case @@ -351,12 +378,16 @@ protected: : m_XAxisData{std::make_shared >(*other.m_XAxisData)}, m_XAxisUnit{other.m_XAxisUnit}, m_ValuesData{std::make_shared >(*other.m_ValuesData)}, - m_ValuesUnit{other.m_ValuesUnit} + m_ValuesUnit{other.m_ValuesUnit}, + m_YAxis{other.m_YAxis} { // Since a series is ordered from its construction and is always ordered, it is not // necessary to call the sort method here ('other' is sorted) } + /// @return the y-axis associated to the data series + OptionalAxis yAxis() const { return m_YAxis; } + /// Assignment operator template DataSeries &operator=(DataSeries other) @@ -365,6 +396,7 @@ protected: std::swap(m_XAxisUnit, other.m_XAxisUnit); std::swap(m_ValuesData, other.m_ValuesData); std::swap(m_ValuesUnit, other.m_ValuesUnit); + std::swap(m_YAxis, other.m_YAxis); return *this; } @@ -380,11 +412,17 @@ private: m_ValuesData = m_ValuesData->sort(permutation); } + // x-axis std::shared_ptr > m_XAxisData; Unit m_XAxisUnit; + + // values std::shared_ptr > m_ValuesData; Unit m_ValuesUnit; + // y-axis (optional) + OptionalAxis m_YAxis; + QReadWriteLock m_Lock; };