Improve copy and move constructors and assignment operators

This commit is contained in:
Carles Fernandez 2023-03-25 14:05:01 +01:00
parent 3a235e4859
commit 29cf1c246b
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
8 changed files with 148 additions and 69 deletions

View File

@ -55,7 +55,8 @@ public:
inline Serdes_Galileo_Eph& operator=(const Serdes_Galileo_Eph& rhs) noexcept //!< Copy assignment operator
{
this->monitor_ = rhs.monitor_;
Serdes_Galileo_Eph temp(rhs);
std::swap(this->monitor_, temp.monitor_);
return *this;
}
@ -65,10 +66,7 @@ public:
inline Serdes_Galileo_Eph& operator=(Serdes_Galileo_Eph&& other) noexcept //!< Move assignment operator
{
if (this != &other)
{
this->monitor_ = std::move(other.monitor_);
}
std::swap(this->monitor_, other.monitor_);
return *this;
}

View File

@ -54,7 +54,8 @@ public:
inline Serdes_Gps_Eph& operator=(const Serdes_Gps_Eph& rhs) noexcept //!< Copy assignment operator
{
this->monitor_ = rhs.monitor_;
Serdes_Gps_Eph temp(rhs);
std::swap(this->monitor_, temp.monitor_);
return *this;
}
@ -64,10 +65,7 @@ public:
inline Serdes_Gps_Eph& operator=(Serdes_Gps_Eph&& other) noexcept //!< Move assignment operator
{
if (this != &other)
{
this->monitor_ = std::move(other.monitor_);
}
std::swap(this->monitor_, other.monitor_);
return *this;
}

View File

@ -55,12 +55,17 @@ public:
inline Serdes_Monitor_Pvt& operator=(const Serdes_Monitor_Pvt& rhs) noexcept //!< Copy assignment operator
{
this->monitor_ = rhs.monitor_;
if (this != &rhs)
{
this->monitor_.CopyFrom(rhs.monitor_);
}
return *this;
}
inline Serdes_Monitor_Pvt(Serdes_Monitor_Pvt&& other) noexcept : monitor_(std::move(other.monitor_)) //!< Move constructor
{
// Set the other object's monitor_ to a default-constructed state
other.monitor_ = gnss_sdr::MonitorPvt{};
}
inline Serdes_Monitor_Pvt& operator=(Serdes_Monitor_Pvt&& other) noexcept //!< Move assignment operator

View File

@ -50,31 +50,6 @@ public:
// google::protobuf::ShutdownProtobufLibrary();
}
inline Serdes_Nav_Message(const Serdes_Nav_Message& other) noexcept //!< Copy constructor
{
this->navmsg_ = other.navmsg_;
}
inline Serdes_Nav_Message& operator=(const Serdes_Nav_Message& rhs) noexcept //!< Copy assignment operator
{
this->navmsg_ = rhs.navmsg_;
return *this;
}
inline Serdes_Nav_Message(Serdes_Nav_Message&& other) noexcept //!< Move constructor
{
this->navmsg_ = std::move(other.navmsg_);
}
inline Serdes_Nav_Message& operator=(Serdes_Nav_Message&& other) noexcept //!< Move assignment operator
{
if (this != &other)
{
this->navmsg_ = std::move(other.navmsg_);
}
return *this;
}
inline std::string createProtobuffer(const std::shared_ptr<Nav_Message_Packet> nav_msg_packet) //!< Serialization into a string
{
navmsg_.Clear();

View File

@ -74,7 +74,10 @@ bool operator==(const Gnss_Satellite& sat1, const Gnss_Satellite& sat2)
// Copy constructor
Gnss_Satellite::Gnss_Satellite(const Gnss_Satellite& other) noexcept
{
*this = other;
system = other.system;
block = other.block;
PRN = other.PRN;
rf_link = other.rf_link;
}
@ -96,7 +99,11 @@ Gnss_Satellite& Gnss_Satellite::operator=(const Gnss_Satellite& rhs)
// Move constructor
Gnss_Satellite::Gnss_Satellite(Gnss_Satellite&& other) noexcept
{
*this = std::move(other);
system = std::move(other.system);
block = std::move(other.block);
PRN = other.PRN;
rf_link = other.rf_link;
other.reset();
}
@ -105,10 +112,11 @@ Gnss_Satellite& Gnss_Satellite::operator=(Gnss_Satellite&& other) noexcept
{
if (this != &other)
{
this->system = other.get_system();
this->block = other.get_block();
this->PRN = other.get_PRN();
this->rf_link = other.get_rf_link();
system = std::move(other.system);
block = std::move(other.block);
PRN = other.PRN;
rf_link = other.rf_link;
other.reset();
}
return *this;
}

View File

@ -81,10 +81,7 @@ public:
bool Flag_PLL_180_deg_phase_locked{}; //!< Set by Telemetry Decoder processing block
/// Copy constructor
Gnss_Synchro(const Gnss_Synchro& other) noexcept
{
*this = other;
};
Gnss_Synchro(const Gnss_Synchro& other) noexcept = default;
/// Copy assignment operator
Gnss_Synchro& operator=(const Gnss_Synchro& rhs) noexcept
@ -125,10 +122,7 @@ public:
};
/// Move constructor
Gnss_Synchro(Gnss_Synchro&& other) noexcept
{
*this = std::move(other);
};
Gnss_Synchro(Gnss_Synchro&& other) noexcept = default;
/// Move assignment operator
Gnss_Synchro& operator=(Gnss_Synchro&& other) noexcept
@ -163,6 +157,36 @@ public:
this->Flag_valid_word = other.Flag_valid_word;
this->Flag_valid_pseudorange = other.Flag_valid_pseudorange;
this->Flag_PLL_180_deg_phase_locked = other.Flag_PLL_180_deg_phase_locked;
// Leave the source object in a valid but unspecified state
other.Signal[0] = '\0';
other.Signal[1] = '\0';
other.Signal[2] = '\0';
other.System = 0;
other.PRN = 0;
other.Channel_ID = 0;
other.Acq_delay_samples = 0.0;
other.Acq_doppler_hz = 0.0;
other.Acq_samplestamp_samples = 0;
other.Acq_doppler_step = 0;
other.fs = 0;
other.Prompt_I = 0.0;
other.Prompt_Q = 0.0;
other.CN0_dB_hz = 0.0;
other.Carrier_Doppler_hz = 0.0;
other.Carrier_phase_rads = 0.0;
other.Code_phase_samples = 0.0;
other.Tracking_sample_counter = 0;
other.correlation_length_ms = 0;
other.TOW_at_current_symbol_ms = 0;
other.Pseudorange_m = 0.0;
other.RX_time = 0.0;
other.interp_TOW_ms = 0.0;
other.Flag_valid_acquisition = false;
other.Flag_valid_symbol_output = false;
other.Flag_valid_word = false;
other.Flag_valid_pseudorange = false;
other.Flag_PLL_180_deg_phase_locked = false;
}
return *this;
};

View File

@ -18,6 +18,7 @@
#include "acquisition_dump_reader.h"
#include <matio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
@ -27,13 +28,13 @@ bool Acquisition_Dump_Reader::read_binary_acq()
mat_t* matfile = Mat_Open(d_dump_filename.c_str(), MAT_ACC_RDONLY);
if (matfile == nullptr)
{
std::cout << "¡¡¡Unreachable Acquisition dump file!!!\n";
std::cout << "Unreachable Acquisition dump file " << d_dump_filename << '\n';
return false;
}
matvar_t* var_ = Mat_VarRead(matfile, "acq_grid");
if (var_ == nullptr)
{
std::cout << "¡¡¡Unreachable grid variable into Acquisition dump file!!!\n";
std::cout << "Unreachable grid variable in Acquisition dump file.\n";
Mat_Close(matfile);
return false;
}
@ -162,7 +163,7 @@ Acquisition_Dump_Reader::Acquisition_Dump_Reader(const std::string& basename,
}
else
{
std::cout << "¡¡¡Unreachable Acquisition dump file!!!\n";
std::cout << "Unreachable Acquisition dump file " << d_dump_filename << '\n';
}
acq_doppler_hz = 0.0;
acq_delay_samples = 0.0;
@ -222,18 +223,53 @@ Acquisition_Dump_Reader::Acquisition_Dump_Reader(const std::string& basename,
// Copy constructor
Acquisition_Dump_Reader::Acquisition_Dump_Reader(const Acquisition_Dump_Reader& other) noexcept
: doppler(other.doppler),
samples(other.samples),
mag(other.mag),
acq_doppler_hz(other.acq_doppler_hz),
acq_delay_samples(other.acq_delay_samples),
test_statistic(other.test_statistic),
input_power(other.input_power),
threshold(other.threshold),
positive_acq(other.positive_acq),
PRN(other.PRN),
num_dwells(other.num_dwells),
sample_counter(other.sample_counter),
d_basename(other.d_basename),
d_dump_filename(other.d_dump_filename),
d_sat(other.d_sat),
d_doppler_max(other.d_doppler_max),
d_doppler_step(other.d_doppler_step),
d_samples_per_code(other.d_samples_per_code),
d_num_doppler_bins(other.d_num_doppler_bins)
{
*this = other;
}
// Copy assignment operator
Acquisition_Dump_Reader& Acquisition_Dump_Reader::operator=(const Acquisition_Dump_Reader& rhs)
Acquisition_Dump_Reader& Acquisition_Dump_Reader::operator=(const Acquisition_Dump_Reader& other) noexcept
{
// Only do assignment if RHS is a different object from this.
if (this != &rhs)
if (this != &other)
{
*this = rhs;
doppler = other.doppler;
samples = other.samples;
mag = other.mag;
acq_doppler_hz = other.acq_doppler_hz;
acq_delay_samples = other.acq_delay_samples;
test_statistic = other.test_statistic;
input_power = other.input_power;
threshold = other.threshold;
positive_acq = other.positive_acq;
PRN = other.PRN;
num_dwells = other.num_dwells;
sample_counter = other.sample_counter;
d_basename = other.d_basename;
d_dump_filename = other.d_dump_filename;
d_sat = other.d_sat;
d_doppler_max = other.d_doppler_max;
d_doppler_step = other.d_doppler_step;
d_samples_per_code = other.d_samples_per_code;
d_num_doppler_bins = other.d_num_doppler_bins;
}
return *this;
}
@ -241,17 +277,54 @@ Acquisition_Dump_Reader& Acquisition_Dump_Reader::operator=(const Acquisition_Du
// Move constructor
Acquisition_Dump_Reader::Acquisition_Dump_Reader(Acquisition_Dump_Reader&& other) noexcept
: doppler(std::move(other.doppler)),
samples(std::move(other.samples)),
mag(std::move(other.mag)),
acq_doppler_hz(other.acq_doppler_hz),
acq_delay_samples(other.acq_delay_samples),
test_statistic(other.test_statistic),
input_power(other.input_power),
threshold(other.threshold),
positive_acq(other.positive_acq),
PRN(other.PRN),
num_dwells(other.num_dwells),
sample_counter(other.sample_counter),
d_basename(std::move(other.d_basename)),
d_dump_filename(std::move(other.d_dump_filename)),
d_sat(other.d_sat),
d_doppler_max(other.d_doppler_max),
d_doppler_step(other.d_doppler_step),
d_samples_per_code(other.d_samples_per_code),
d_num_doppler_bins(other.d_num_doppler_bins)
{
*this = std::move(other);
}
// Move assignment operator
Acquisition_Dump_Reader& Acquisition_Dump_Reader::operator=(Acquisition_Dump_Reader&& other) noexcept
{
if (this != &other)
if (this != &other) // Check for self-assignment
{
*this = other;
// Move member variables from the other object to this object
d_basename = std::move(other.d_basename);
d_dump_filename = std::move(other.d_dump_filename);
d_sat = std::move(other.d_sat);
d_doppler_max = std::move(other.d_doppler_max);
d_doppler_step = std::move(other.d_doppler_step);
d_samples_per_code = std::move(other.d_samples_per_code);
d_num_doppler_bins = std::move(other.d_num_doppler_bins);
doppler = std::move(other.doppler);
samples = std::move(other.samples);
mag = std::move(other.mag);
acq_doppler_hz = std::move(other.acq_doppler_hz);
acq_delay_samples = std::move(other.acq_delay_samples);
test_statistic = std::move(other.test_statistic);
input_power = std::move(other.input_power);
threshold = std::move(other.threshold);
positive_acq = std::move(other.positive_acq);
PRN = std::move(other.PRN);
num_dwells = std::move(other.num_dwells);
sample_counter = std::move(other.sample_counter);
}
return *this;
}
}

View File

@ -37,12 +37,10 @@ public:
int channel = 0,
int execution = 1);
~Acquisition_Dump_Reader() = default;
Acquisition_Dump_Reader(const Acquisition_Dump_Reader& other) noexcept; //!< Copy constructor
Acquisition_Dump_Reader& operator=(const Acquisition_Dump_Reader&); //!< Copy assignment operator
Acquisition_Dump_Reader(Acquisition_Dump_Reader&& other) noexcept; //!< Move constructor
Acquisition_Dump_Reader& operator=(Acquisition_Dump_Reader&& other) noexcept; //!< Move assignment operator
Acquisition_Dump_Reader(const Acquisition_Dump_Reader& other) noexcept; //!< Copy constructor
Acquisition_Dump_Reader& operator=(const Acquisition_Dump_Reader& other) noexcept; //!< Copy assignment operator
Acquisition_Dump_Reader(Acquisition_Dump_Reader&& other) noexcept; //!< Move constructor
Acquisition_Dump_Reader& operator=(Acquisition_Dump_Reader&& other) noexcept; //!< Move assignment operator
bool read_binary_acq();
@ -61,12 +59,12 @@ public:
private:
std::string d_basename;
std::string d_dump_filename;
unsigned int d_sat{};
unsigned int d_doppler_max{};
unsigned int d_doppler_step{};
unsigned int d_samples_per_code{};
unsigned int d_num_doppler_bins{};
std::string d_dump_filename;
};
#endif // GNSS_SDR_ACQUISITION_DUMP_READER_H