##// END OF EJS Templates
Use std::shared_ptr in CosinusProvider
Alexandre Leroux -
r310:9a5cb57f1573
parent child
Show More
@@ -26,12 +26,14 class DataSeries : public IDataSeries {
26 public:
26 public:
27 /// @sa IDataSeries::xAxisData()
27 /// @sa IDataSeries::xAxisData()
28 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
28 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
29 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
29
30
30 /// @sa IDataSeries::xAxisUnit()
31 /// @sa IDataSeries::xAxisUnit()
31 Unit xAxisUnit() const override { return m_XAxisUnit; }
32 Unit xAxisUnit() const override { return m_XAxisUnit; }
32
33
33 /// @return the values dataset
34 /// @return the values dataset
34 std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
35 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
36 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
35
37
36 /// @sa IDataSeries::valuesUnit()
38 /// @sa IDataSeries::valuesUnit()
37 Unit valuesUnit() const override { return m_ValuesUnit; }
39 Unit valuesUnit() const override { return m_ValuesUnit; }
@@ -60,6 +62,27 protected:
60 {
62 {
61 }
63 }
62
64
65 /// Copy ctor
66 explicit DataSeries(const DataSeries<Dim> &other)
67 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
68 m_XAxisUnit{other.m_XAxisUnit},
69 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
70 m_ValuesUnit{other.m_ValuesUnit}
71 {
72 }
73
74 /// Assignment operator
75 template <int D>
76 DataSeries &operator=(DataSeries<D> other)
77 {
78 std::swap(m_XAxisData, other.m_XAxisData);
79 std::swap(m_XAxisUnit, other.m_XAxisUnit);
80 std::swap(m_ValuesData, other.m_ValuesData);
81 std::swap(m_ValuesUnit, other.m_ValuesUnit);
82
83 return *this;
84 }
85
63 private:
86 private:
64 std::shared_ptr<ArrayData<1> > m_XAxisData;
87 std::shared_ptr<ArrayData<1> > m_XAxisData;
65 Unit m_XAxisUnit;
88 Unit m_XAxisUnit;
@@ -26,7 +26,7 class IDataProvider : public QObject {
26 public:
26 public:
27 virtual ~IDataProvider() noexcept = default;
27 virtual ~IDataProvider() noexcept = default;
28
28
29 virtual std::unique_ptr<IDataSeries>
29 virtual std::shared_ptr<IDataSeries>
30 retrieveData(const DataProviderParameters &parameters) const = 0;
30 retrieveData(const DataProviderParameters &parameters) const = 0;
31
31
32
32
@@ -41,11 +41,16 public:
41 /// Returns the x-axis dataset
41 /// Returns the x-axis dataset
42 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
42 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
43
43
44 /// Returns the x-axis dataset (as const)
45 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
46
44 virtual Unit xAxisUnit() const = 0;
47 virtual Unit xAxisUnit() const = 0;
45
48
46 virtual Unit valuesUnit() const = 0;
49 virtual Unit valuesUnit() const = 0;
47
50
48 virtual void merge(IDataSeries *dataSeries) = 0;
51 virtual void merge(IDataSeries *dataSeries) = 0;
52
53 virtual std::unique_ptr<IDataSeries> clone() const = 0;
49 };
54 };
50
55
51 // Required for using shared_ptr in signals/slots
56 // Required for using shared_ptr in signals/slots
@@ -23,6 +23,8 public:
23 * @param value the value data
23 * @param value the value data
24 */
24 */
25 void setData(int index, double x, double value) noexcept;
25 void setData(int index, double x, double value) noexcept;
26
27 std::unique_ptr<IDataSeries> clone() const;
26 };
28 };
27
29
28 #endif // SCIQLOP_SCALARSERIES_H
30 #endif // SCIQLOP_SCALARSERIES_H
@@ -36,10 +36,9 public:
36
36
37 bool contains(const SqpDateTime &dateTime);
37 bool contains(const SqpDateTime &dateTime);
38 bool intersect(const SqpDateTime &dateTime);
38 bool intersect(const SqpDateTime &dateTime);
39 void setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept;
40
39
41 public slots:
40 public slots:
42 void onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
41 void setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
43
42
44 signals:
43 signals:
45 void updated();
44 void updated();
@@ -30,7 +30,7 public:
30 */
30 */
31 std::shared_ptr<Variable>
31 std::shared_ptr<Variable>
32 createVariable(const QString &name, const SqpDateTime &dateTime,
32 createVariable(const QString &name, const SqpDateTime &dateTime,
33 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept;
33 std::shared_ptr<IDataSeries> defaultDataSeries) noexcept;
34
34
35 std::shared_ptr<Variable> variable(int index) const;
35 std::shared_ptr<Variable> variable(int index) const;
36
36
@@ -11,3 +11,8 void ScalarSeries::setData(int index, double x, double value) noexcept
11 xAxisData()->setData(index, x);
11 xAxisData()->setData(index, x);
12 valuesData()->setData(index, value);
12 valuesData()->setData(index, value);
13 }
13 }
14
15 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
16 {
17 return std::make_unique<ScalarSeries>(*this);
18 }
@@ -55,16 +55,18 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
55 impl->m_DateTime = dateTime;
55 impl->m_DateTime = dateTime;
56 }
56 }
57
57
58 void Variable::setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept
58 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
59 {
59 {
60 if (!impl->m_DataSeries) {
60 if (!dataSeries) {
61 impl->m_DataSeries = std::move(dataSeries);
61 /// @todo ALX : log
62 return;
62 }
63 }
63 }
64
64
65 void Variable::onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
65 // Inits the data series of the variable
66 {
66 if (!impl->m_DataSeries) {
67 if (impl->m_DataSeries) {
67 impl->m_DataSeries = dataSeries->clone();
68 }
69 else {
68 impl->m_DataSeries->merge(dataSeries.get());
70 impl->m_DataSeries->merge(dataSeries.get());
69
71
70 emit updated();
72 emit updated();
@@ -21,7 +21,7 namespace {
21
21
22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 /// will be deleted when the timerange is recovered from SciQlop
23 /// will be deleted when the timerange is recovered from SciQlop
24 std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
24 std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 const SqpDateTime &dateTime) noexcept
25 const SqpDateTime &dateTime) noexcept
26 {
26 {
27 auto parameters = DataProviderParameters{dateTime};
27 auto parameters = DataProviderParameters{dateTime};
@@ -105,7 +105,7 void VariableController::createVariable(const QString &name,
105 // store the provider
105 // store the provider
106 impl->m_VariableToProviderMap[newVariable] = provider;
106 impl->m_VariableToProviderMap[newVariable] = provider;
107 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
107 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
108 &Variable::onAddDataSeries);
108 &Variable::setDataSeries);
109
109
110
110
111 // store in cache
111 // store in cache
@@ -54,7 +54,7 VariableModel::VariableModel(QObject *parent)
54
54
55 std::shared_ptr<Variable>
55 std::shared_ptr<Variable>
56 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
56 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
57 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
57 std::shared_ptr<IDataSeries> defaultDataSeries) noexcept
58 {
58 {
59 auto insertIndex = rowCount();
59 auto insertIndex = rowCount();
60 beginInsertRows({}, insertIndex, insertIndex);
60 beginInsertRows({}, insertIndex, insertIndex);
@@ -13,7 +13,7 Q_DECLARE_LOGGING_CATEGORY(LOG_CosinusProvider)
13 class CosinusProvider : public IDataProvider {
13 class CosinusProvider : public IDataProvider {
14 public:
14 public:
15 /// @sa IDataProvider::retrieveData()
15 /// @sa IDataProvider::retrieveData()
16 std::unique_ptr<IDataSeries>
16 std::shared_ptr<IDataSeries>
17 retrieveData(const DataProviderParameters &parameters) const override;
17 retrieveData(const DataProviderParameters &parameters) const override;
18
18
19 void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) override;
19 void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) override;
@@ -9,47 +9,11
9
9
10 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
10 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
11
11
12 std::unique_ptr<IDataSeries>
12 std::shared_ptr<IDataSeries>
13 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
13 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
14 {
14 {
15 auto dateTime = parameters.m_Time;
15 auto dateTime = parameters.m_Time;
16
16
17 // Gets the timerange from the parameters
18 auto start = dateTime.m_TStart;
19 auto end = dateTime.m_TEnd;
20
21
22 // We assure that timerange is valid
23 if (end < start) {
24 std::swap(start, end);
25 }
26
27 // Generates scalar series containing cosinus values (one value per second)
28 auto scalarSeries
29 = std::make_unique<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
30
31 auto dataIndex = 0;
32 for (auto time = start; time < end; ++time, ++dataIndex) {
33 scalarSeries->setData(dataIndex, time, std::cos(time));
34 }
35
36 return scalarSeries;
37 }
38
39 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
40 {
41 // NOTE: Try to use multithread if possible
42 for (const auto &dateTime : dateTimeList) {
43
44 auto scalarSeries = this->retrieveDataSeries(dateTime);
45
46 emit dataProvided(scalarSeries, dateTime);
47 }
48 }
49
50
51 std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTime &dateTime)
52 {
53 auto dataIndex = 0;
17 auto dataIndex = 0;
54
18
55 // Gets the timerange from the parameters
19 // Gets the timerange from the parameters
@@ -72,3 +36,12 std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTi
72 }
36 }
73 return scalarSeries;
37 return scalarSeries;
74 }
38 }
39
40 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
41 {
42 // NOTE: Try to use multithread if possible
43 for (const auto &dateTime : dateTimeList) {
44 auto scalarSeries = this->retrieveData(DataProviderParameters{dateTime});
45 emit dataProvided(scalarSeries, dateTime);
46 }
47 }
General Comments 0
You need to be logged in to leave comments. Login now