From 4b4605df15ce7061a08d7911069927bf49778d05 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 7 Mar 2022 09:24:37 +0100 Subject: fix: prevent invalid UniquePtr usage --- src/gyronardo.cpp | 31 ++++++++++++++----------------- src/sensor/calibration.cpp | 28 ++++++++++++++-------------- src/sensor/calibration.hpp | 5 ++--- src/std/memory.hpp | 12 +++++++++--- src/std/memory.tpp | 19 ------------------- 5 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/gyronardo.cpp b/src/gyronardo.cpp index 148a21b..63b4bfc 100644 --- a/src/gyronardo.cpp +++ b/src/gyronardo.cpp @@ -16,17 +16,14 @@ constexpr unsigned int BAUD_RATE = 9600U; constexpr unsigned int SENSOR_RETRY_TIME = 2000U; SerialStream sout(Serial_(), BAUD_RATE); -// SerialStream sout(Serial, BAUD_RATE); -UniquePtr sensor; +auto sensor = Sensor(SENSOR_ADDRESS, Wire, sout, SENSOR_THROTTLE_TIME); void setup() { sout.waitReady(); - sensor = make_unique(SENSOR_ADDRESS, Wire, sout, SENSOR_THROTTLE_TIME); - - while (!sensor->begin()) + while (!sensor.begin()) { sout << "Error: Could not connect to the sensor. Retrying after 2000 " "milliseconds..." @@ -35,18 +32,18 @@ void setup() delay(SENSOR_RETRY_TIME); } - if (!sensor->setAccelSensitivity(2)) // 8g + if (!sensor.setAccelSensitivity(2)) // 8g { sout << "Error: Failed to set the sensor's accelerometer sensitivity. Status: " - << static_cast(sensor->getStatus()) << endl; + << static_cast(sensor.getStatus()) << endl; stop(); } - if (!sensor->setGyroSensitivity(1)) // 500 degrees/s + if (!sensor.setGyroSensitivity(1)) // 500 degrees/s { sout << "Error: Failed to set the sensor's gyro sensitivity. Status: " - << static_cast(sensor->getStatus()) << endl; + << static_cast(sensor.getStatus()) << endl; stop(); } @@ -66,17 +63,17 @@ void setup() sout << "Finished calibrating sensor\n"; sout << "Calibration values:\n" - << "Accelerometer X: " << sensor->accel_cal_x << "\n" - << "Accelerometer Y: " << sensor->accel_cal_y << "\n" - << "Accelerometer Z: " << sensor->accel_cal_z << "\n" - << "Gyro X: " << sensor->gyro_cal_x << "\n" - << "Gyro Y: " << sensor->gyro_cal_y << "\n" - << "Gyro Z: " << sensor->gyro_cal_z << "\n"; + << "Accelerometer X: " << sensor.accel_cal_x << "\n" + << "Accelerometer Y: " << sensor.accel_cal_y << "\n" + << "Accelerometer Z: " << sensor.accel_cal_z << "\n" + << "Gyro X: " << sensor.gyro_cal_x << "\n" + << "Gyro Y: " << sensor.gyro_cal_y << "\n" + << "Gyro Z: " << sensor.gyro_cal_z << "\n"; sout << "Starting..." << endl; - sout << "Pitch: " << sensor->getPitch() << " " - << "Roll: " << sensor->getRoll() << endl; + sout << "Pitch: " << sensor.getPitch() << " " + << "Roll: " << sensor.getRoll() << endl; } void loop() diff --git a/src/sensor/calibration.cpp b/src/sensor/calibration.cpp index 1538baa..42f5010 100644 --- a/src/sensor/calibration.cpp +++ b/src/sensor/calibration.cpp @@ -3,7 +3,7 @@ #include "std/time.hpp" #include "utils.hpp" -SensorCalibrator::SensorCalibrator(UniquePtr sensor, SerialStream sout) +SensorCalibrator::SensorCalibrator(Sensor sensor, SerialStream sout) : _sensor(sensor), _sout(sout) { } @@ -51,26 +51,26 @@ bool SensorCalibrator::calibrate(unsigned int throttle_time) void SensorCalibrator::_updateValues(SensorCalibratorValues &values) { - _sensor->read(); + _sensor.read(); - values.accel_x -= _sensor->getAccelX(); - values.accel_y -= _sensor->getAccelY(); - values.accel_z -= _sensor->getAccelZ(); + values.accel_x -= _sensor.getAccelX(); + values.accel_y -= _sensor.getAccelY(); + values.accel_z -= _sensor.getAccelZ(); - values.gyro_x -= _sensor->getGyroX(); - values.gyro_y -= _sensor->getGyroY(); - values.gyro_z -= _sensor->getGyroZ(); + values.gyro_x -= _sensor.getGyroX(); + values.gyro_y -= _sensor.getGyroY(); + values.gyro_z -= _sensor.getGyroZ(); } void SensorCalibrator::_adjustCalibrationWithValues(const SensorCalibratorValues &values) { - _sensor->accel_cal_x += values.accel_x; - _sensor->accel_cal_y += values.accel_y; - _sensor->accel_cal_z += values.accel_z; + _sensor.accel_cal_x += values.accel_x; + _sensor.accel_cal_y += values.accel_y; + _sensor.accel_cal_z += values.accel_z; - _sensor->gyro_cal_x += values.gyro_x; - _sensor->gyro_cal_y += values.gyro_y; - _sensor->gyro_cal_z += values.gyro_z; + _sensor.gyro_cal_x += values.gyro_x; + _sensor.gyro_cal_y += values.gyro_y; + _sensor.gyro_cal_z += values.gyro_z; } void SensorCalibrator::_adjustValues(SensorCalibratorValues &values) diff --git a/src/sensor/calibration.hpp b/src/sensor/calibration.hpp index a2da58b..090fd27 100644 --- a/src/sensor/calibration.hpp +++ b/src/sensor/calibration.hpp @@ -2,7 +2,6 @@ #include "sensor/sensor.hpp" #include "serial.hpp" -#include "std/memory.hpp" // Calibration precision constexpr float ACCEL_CAL_X_MAX = 0.006; @@ -52,7 +51,7 @@ public: * @param sensor A sensor to calibrate * @param sout A Serial output stream */ - SensorCalibrator(UniquePtr sensor, SerialStream sout); + SensorCalibrator(Sensor sensor, SerialStream sout); /** * Calibrates the sensor. @@ -70,7 +69,7 @@ private: static void _adjustValues(SensorCalibratorValues &values); static bool _isValuesInRange(const SensorCalibratorValues &values); - UniquePtr _sensor; + Sensor _sensor; SerialStream _sout; }; diff --git a/src/std/memory.hpp b/src/std/memory.hpp index 0c5719c..d1ca762 100644 --- a/src/std/memory.hpp +++ b/src/std/memory.hpp @@ -11,13 +11,19 @@ class UniquePtr public: explicit UniquePtr() = default; explicit UniquePtr(Target *target); - UniquePtr(const UniquePtr &unique_ptr); - UniquePtr(UniquePtr &&unique_ptr) noexcept; - UniquePtr &operator=(const UniquePtr &unique_ptr); + // Move constructor + UniquePtr(UniquePtr &&unique_ptr) noexcept; + // Move assignment operator UniquePtr &operator=(UniquePtr &&unique_ptr) noexcept; + // Disable the copy constructor + UniquePtr(const UniquePtr &unique_ptr) = delete; + + // Disable the copy assignment operator + UniquePtr &operator=(const UniquePtr &unique_ptr) = delete; + ~UniquePtr(); Target operator*() const; diff --git a/src/std/memory.tpp b/src/std/memory.tpp index fdfe72b..b2f39bf 100644 --- a/src/std/memory.tpp +++ b/src/std/memory.tpp @@ -26,12 +26,6 @@ UniquePtr::UniquePtr(Target *target) : _target(target) { } -template -UniquePtr::UniquePtr(const UniquePtr &unique_ptr) - : _target(new Target(*(unique_ptr._target))) -{ -} - template UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept : _target(unique_ptr._target) @@ -39,19 +33,6 @@ UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept unique_ptr._target = nullptr; } -template -UniquePtr &UniquePtr::operator=(const UniquePtr &unique_ptr) -{ - if (&unique_ptr != this) - { - delete _target; - _target = nullptr; - _target = new Target(*(unique_ptr._target)); - } - - return *this; -} - template UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept { -- cgit v1.2.3-18-g5258