From 35433b4f6594f2334520d64c2c08ba56c65b1937 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Fri, 9 Feb 2024 10:02:39 -0300 Subject: [PATCH] Add assignment operators to some classes (#3937) The implicitly defined assignment operator for classes having custom defined copy constructor is deprecated. This patch adds explicit assignment operators. XDataSeries assignment operator now deletes XDataPoints pointed by datapoints array in the target and creates new ones for the source, this required a change in the way XDataSeries is used in JSON parser, which was based on default assignment operator semantics. --- src/Core/Measures.h | 4 ++++ src/FileIO/JsonRideFile.y | 6 +----- src/FileIO/LocationInterpolation.h | 1 + src/FileIO/RideFile.h | 13 +++++++++++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Core/Measures.h b/src/Core/Measures.h index a853072b4..27d5d4826 100644 --- a/src/Core/Measures.h +++ b/src/Core/Measures.h @@ -41,11 +41,15 @@ public: for (int i = 0; iwhen = other.when; this->comment = other.comment; this->source = other.source; this->originalSource = other.originalSource; for (int i = 0; ivalues[i] = other.values[i]; + return *this; } ~Measure() {} diff --git a/src/FileIO/JsonRideFile.y b/src/FileIO/JsonRideFile.y index aa66670fb..dfa948d93 100644 --- a/src/FileIO/JsonRideFile.y +++ b/src/FileIO/JsonRideFile.y @@ -262,11 +262,7 @@ xdata_list: xdata_series | xdata_list ',' xdata_series ; -xdata_series: '{' xdata_items '}' { XDataSeries *add = new XDataSeries; - add->name=jc->xdataseries.name; - add->datapoints=jc->xdataseries.datapoints; - add->valuename=jc->xdataseries.valuename; - add->unitname=jc->xdataseries.unitname; +xdata_series: '{' xdata_items '}' { XDataSeries *add = new XDataSeries(jc->xdataseries); jc->JsonRide->addXData(add->name, add); // clear for next one diff --git a/src/FileIO/LocationInterpolation.h b/src/FileIO/LocationInterpolation.h index 4dc7dc2e2..bef8a9691 100644 --- a/src/FileIO/LocationInterpolation.h +++ b/src/FileIO/LocationInterpolation.h @@ -41,6 +41,7 @@ public: v3(double a, double b, double c) : m_t(a, b, c) {}; v3(const v3& o) : m_t(o.m_t) {} + v3& operator=(const v3& o) { m_t = o.m_t; return *this; } double x() const { return std::get<0>(m_t); } double y() const { return std::get<1>(m_t); } diff --git a/src/FileIO/RideFile.h b/src/FileIO/RideFile.h index bf803e991..80f0d2531 100644 --- a/src/FileIO/RideFile.h +++ b/src/FileIO/RideFile.h @@ -572,13 +572,17 @@ public: string[i]=""; } } - XDataPoint (const XDataPoint &other) { + XDataPoint(const XDataPoint &other) { + *this = other; + } + XDataPoint& operator=(const XDataPoint &other) { this->secs=other.secs; this->km=other.km; for(int i=0; inumber[i]= other.number[i]; this->string[i]= other.string[i]; } + return *this; } double secs, km; @@ -589,16 +593,21 @@ public: class XDataSeries { public: XDataSeries() {} - XDataSeries(XDataSeries &other) { + XDataSeries(const XDataSeries& other) { *this = other; } + XDataSeries& operator=(const XDataSeries &other) { name = other.name; valuename = other.valuename; unitname = other.unitname; valuetype = other.valuetype; + // we need to delete objects pointed by the assignment target + foreach(XDataPoint *p, datapoints) delete p; + datapoints.clear(); // we need to create new objects since we are holding pointers to objects // otherwise we would end up w/ multiple frees or dangling ptrs! foreach (XDataPoint *p, other.datapoints) { datapoints.push_back(new XDataPoint(*p)); } + return *this; } ~XDataSeries() { foreach(XDataPoint *p, datapoints) delete p; }