Add move constructors and assignment operators to wpilibc (#1314)

Fixes #898.
This commit is contained in:
Tyler Veness
2018-09-24 00:08:25 -07:00
committed by Peter Johnson
parent b1965f74a8
commit 1aa8446725
136 changed files with 764 additions and 89 deletions

View File

@@ -8,6 +8,7 @@
#include "frc/AnalogGyro.h"
#include <climits>
#include <utility>
#include <hal/AnalogGyro.h>
#include <hal/Errors.h>
@@ -64,6 +65,20 @@ AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel, int center,
AnalogGyro::~AnalogGyro() { HAL_FreeAnalogGyro(m_gyroHandle); }
AnalogGyro::AnalogGyro(AnalogGyro&& rhs)
: GyroBase(std::move(rhs)), m_analog(std::move(rhs.m_analog)) {
std::swap(m_gyroHandle, rhs.m_gyroHandle);
}
AnalogGyro& AnalogGyro::operator=(AnalogGyro&& rhs) {
GyroBase::operator=(std::move(rhs));
m_analog = std::move(rhs.m_analog);
std::swap(m_gyroHandle, rhs.m_gyroHandle);
return *this;
}
double AnalogGyro::GetAngle() const {
if (StatusIsFatal()) return 0.0;
int32_t status = 0;

View File

@@ -7,6 +7,8 @@
#include "frc/AnalogInput.h"
#include <utility>
#include <hal/AnalogAccumulator.h>
#include <hal/AnalogInput.h>
#include <hal/HAL.h>
@@ -43,9 +45,27 @@ AnalogInput::AnalogInput(int channel) {
SetName("AnalogInput", channel);
}
AnalogInput::~AnalogInput() {
HAL_FreeAnalogInputPort(m_port);
m_port = HAL_kInvalidHandle;
AnalogInput::~AnalogInput() { HAL_FreeAnalogInputPort(m_port); }
AnalogInput::AnalogInput(AnalogInput&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
PIDSource(std::move(rhs)),
m_channel(std::move(rhs.m_channel)),
m_accumulatorOffset(std::move(rhs.m_accumulatorOffset)) {
std::swap(m_port, rhs.m_port);
}
AnalogInput& AnalogInput::operator=(AnalogInput&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
PIDSource::operator=(std::move(rhs));
m_channel = std::move(rhs.m_channel);
std::swap(m_port, rhs.m_port);
m_accumulatorOffset = std::move(rhs.m_accumulatorOffset);
return *this;
}
int AnalogInput::GetValue() const {

View File

@@ -8,6 +8,7 @@
#include "frc/AnalogOutput.h"
#include <limits>
#include <utility>
#include <hal/HAL.h>
#include <hal/Ports.h>
@@ -46,6 +47,23 @@ AnalogOutput::AnalogOutput(int channel) {
AnalogOutput::~AnalogOutput() { HAL_FreeAnalogOutputPort(m_port); }
AnalogOutput::AnalogOutput(AnalogOutput&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
m_channel(std::move(rhs.m_channel)) {
std::swap(m_port, rhs.m_port);
}
AnalogOutput& AnalogOutput::operator=(AnalogOutput&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
m_channel = std::move(rhs.m_channel);
std::swap(m_port, rhs.m_port);
return *this;
}
void AnalogOutput::SetVoltage(double voltage) {
int32_t status = 0;
HAL_SetAnalogOutput(m_port, voltage, &status);

View File

@@ -7,7 +7,7 @@
#include "frc/AnalogTrigger.h"
#include <memory>
#include <utility>
#include <hal/HAL.h>
@@ -43,11 +43,32 @@ AnalogTrigger::~AnalogTrigger() {
int32_t status = 0;
HAL_CleanAnalogTrigger(m_trigger, &status);
if (m_ownsAnalog && m_analogInput != nullptr) {
if (m_ownsAnalog) {
delete m_analogInput;
}
}
AnalogTrigger::AnalogTrigger(AnalogTrigger&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
m_index(std::move(rhs.m_index)) {
std::swap(m_trigger, rhs.m_trigger);
std::swap(m_analogInput, rhs.m_analogInput);
std::swap(m_ownsAnalog, rhs.m_ownsAnalog);
}
AnalogTrigger& AnalogTrigger::operator=(AnalogTrigger&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
m_index = std::move(rhs.m_index);
std::swap(m_trigger, rhs.m_trigger);
std::swap(m_analogInput, rhs.m_analogInput);
std::swap(m_ownsAnalog, rhs.m_ownsAnalog);
return *this;
}
void AnalogTrigger::SetLimitsVoltage(double lower, double upper) {
if (StatusIsFatal()) return;
int32_t status = 0;

View File

@@ -7,6 +7,8 @@
#include "frc/CAN.h"
#include <utility>
#include <hal/HAL.h>
using namespace frc;
@@ -46,6 +48,18 @@ CAN::~CAN() {
}
}
CAN::CAN(CAN&& rhs) : ErrorBase(std::move(rhs)) {
std::swap(m_handle, rhs.m_handle);
}
CAN& CAN::operator=(CAN&& rhs) {
ErrorBase::operator=(std::move(rhs));
std::swap(m_handle, rhs.m_handle);
return *this;
}
void CAN::WritePacket(const uint8_t* data, int length, int apiId) {
int32_t status = 0;
HAL_WriteCANPacket(m_handle, data, length, apiId, &status);

View File

@@ -7,6 +7,8 @@
#include "frc/Counter.h"
#include <utility>
#include <hal/HAL.h>
#include "frc/AnalogTrigger.h"
@@ -91,6 +93,29 @@ Counter::~Counter() {
m_counter = HAL_kInvalidHandle;
}
Counter::Counter(Counter&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
CounterBase(std::move(rhs)),
m_upSource(std::move(rhs.m_upSource)),
m_downSource(std::move(rhs.m_downSource)),
m_index(std::move(rhs.m_index)) {
std::swap(m_counter, rhs.m_counter);
}
Counter& Counter::operator=(Counter&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
CounterBase::operator=(std::move(rhs));
m_upSource = std::move(rhs.m_upSource);
m_downSource = std::move(rhs.m_downSource);
std::swap(m_counter, rhs.m_counter);
m_index = std::move(rhs.m_index);
return *this;
}
void Counter::SetUpSource(int channel) {
if (StatusIsFatal()) return;
SetUpSource(std::make_shared<DigitalInput>(channel));

View File

@@ -9,6 +9,7 @@
#include <algorithm>
#include <array>
#include <utility>
#include <hal/Constants.h>
#include <hal/DIO.h>
@@ -47,6 +48,20 @@ DigitalGlitchFilter::~DigitalGlitchFilter() {
}
}
DigitalGlitchFilter::DigitalGlitchFilter(DigitalGlitchFilter&& rhs)
: ErrorBase(std::move(rhs)), SendableBase(std::move(rhs)) {
std::swap(m_channelIndex, rhs.m_channelIndex);
}
DigitalGlitchFilter& DigitalGlitchFilter::operator=(DigitalGlitchFilter&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
std::swap(m_channelIndex, rhs.m_channelIndex);
return *this;
}
void DigitalGlitchFilter::Add(DigitalSource* input) {
DoAdd(input, m_channelIndex + 1);
}

View File

@@ -8,6 +8,7 @@
#include "frc/DigitalInput.h"
#include <limits>
#include <utility>
#include <hal/DIO.h>
#include <hal/HAL.h>
@@ -54,6 +55,20 @@ DigitalInput::~DigitalInput() {
HAL_FreeDIOPort(m_handle);
}
DigitalInput::DigitalInput(DigitalInput&& rhs)
: DigitalSource(std::move(rhs)), m_channel(std::move(rhs.m_channel)) {
std::swap(m_handle, rhs.m_handle);
}
DigitalInput& DigitalInput::operator=(DigitalInput&& rhs) {
DigitalSource::operator=(std::move(rhs));
m_channel = std::move(rhs.m_channel);
std::swap(m_handle, rhs.m_handle);
return *this;
}
bool DigitalInput::Get() const {
if (StatusIsFatal()) return false;
int32_t status = 0;

View File

@@ -8,6 +8,7 @@
#include "frc/DigitalOutput.h"
#include <limits>
#include <utility>
#include <hal/DIO.h>
#include <hal/HAL.h>
@@ -51,6 +52,25 @@ DigitalOutput::~DigitalOutput() {
HAL_FreeDIOPort(m_handle);
}
DigitalOutput::DigitalOutput(DigitalOutput&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
m_channel(std::move(rhs.m_channel)),
m_pwmGenerator(std::move(rhs.m_pwmGenerator)) {
std::swap(m_handle, rhs.m_handle);
}
DigitalOutput& DigitalOutput::operator=(DigitalOutput&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
m_channel = std::move(rhs.m_channel);
std::swap(m_handle, rhs.m_handle);
m_pwmGenerator = std::move(rhs.m_pwmGenerator);
return *this;
}
void DigitalOutput::Set(bool value) {
if (StatusIsFatal()) return;

View File

@@ -7,6 +7,8 @@
#include "frc/DoubleSolenoid.h"
#include <utility>
#include <hal/HAL.h>
#include <hal/Ports.h>
#include <hal/Solenoid.h>
@@ -81,6 +83,29 @@ DoubleSolenoid::~DoubleSolenoid() {
HAL_FreeSolenoidPort(m_reverseHandle);
}
DoubleSolenoid::DoubleSolenoid(DoubleSolenoid&& rhs)
: SolenoidBase(std::move(rhs)),
m_forwardChannel(std::move(rhs.m_forwardChannel)),
m_reverseChannel(std::move(rhs.m_reverseChannel)),
m_forwardMask(std::move(rhs.m_forwardMask)),
m_reverseMask(std::move(rhs.m_reverseMask)) {
std::swap(m_forwardHandle, rhs.m_forwardHandle);
std::swap(m_reverseHandle, rhs.m_reverseHandle);
}
DoubleSolenoid& DoubleSolenoid::operator=(DoubleSolenoid&& rhs) {
SolenoidBase::operator=(std::move(rhs));
m_forwardChannel = std::move(rhs.m_forwardChannel);
m_reverseChannel = std::move(rhs.m_reverseChannel);
m_forwardMask = std::move(rhs.m_forwardMask);
m_reverseMask = std::move(rhs.m_reverseMask);
std::swap(m_forwardHandle, rhs.m_forwardHandle);
std::swap(m_reverseHandle, rhs.m_reverseHandle);
return *this;
}
void DoubleSolenoid::Set(Value value) {
if (StatusIsFatal()) return;

View File

@@ -7,6 +7,8 @@
#include "frc/Encoder.h"
#include <utility>
#include <hal/HAL.h>
#include "frc/DigitalInput.h"
@@ -57,6 +59,31 @@ Encoder::~Encoder() {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
}
Encoder::Encoder(Encoder&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
CounterBase(std::move(rhs)),
PIDSource(std::move(rhs)),
m_aSource(std::move(rhs.m_aSource)),
m_bSource(std::move(rhs.m_bSource)),
m_indexSource(std::move(rhs.m_indexSource)) {
std::swap(m_encoder, rhs.m_encoder);
}
Encoder& Encoder::operator=(Encoder&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
CounterBase::operator=(std::move(rhs));
PIDSource::operator=(std::move(rhs));
m_aSource = std::move(rhs.m_aSource);
m_bSource = std::move(rhs.m_bSource);
m_indexSource = std::move(rhs.m_indexSource);
std::swap(m_encoder, rhs.m_encoder);
return *this;
}
int Encoder::Get() const {
if (StatusIsFatal()) return 0;
int32_t status = 0;

View File

@@ -7,6 +7,8 @@
#include "frc/I2C.h"
#include <utility>
#include <hal/HAL.h>
#include <hal/I2C.h>
@@ -25,6 +27,21 @@ I2C::I2C(Port port, int deviceAddress)
I2C::~I2C() { HAL_CloseI2C(m_port); }
I2C::I2C(I2C&& rhs)
: ErrorBase(std::move(rhs)),
m_deviceAddress(std::move(rhs.m_deviceAddress)) {
std::swap(m_port, rhs.m_port);
}
I2C& I2C::operator=(I2C&& rhs) {
ErrorBase::operator=(std::move(rhs));
std::swap(m_port, rhs.m_port);
m_deviceAddress = std::move(rhs.m_deviceAddress);
return *this;
}
bool I2C::Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived,
int receiveSize) {
int32_t status = 0;

View File

@@ -7,6 +7,8 @@
#include "frc/Notifier.h"
#include <utility>
#include <hal/HAL.h>
#include "frc/Timer.h"
@@ -63,6 +65,31 @@ Notifier::~Notifier() {
HAL_CleanNotifier(handle, &status);
}
Notifier::Notifier(Notifier&& rhs)
: ErrorBase(std::move(rhs)),
m_thread(std::move(rhs.m_thread)),
m_notifier(rhs.m_notifier.load()),
m_handler(std::move(rhs.m_handler)),
m_expirationTime(std::move(rhs.m_expirationTime)),
m_period(std::move(rhs.m_period)),
m_periodic(std::move(rhs.m_periodic)) {
rhs.m_notifier = HAL_kInvalidHandle;
}
Notifier& Notifier::operator=(Notifier&& rhs) {
ErrorBase::operator=(std::move(rhs));
m_thread = std::move(rhs.m_thread);
m_notifier = rhs.m_notifier.load();
rhs.m_notifier = HAL_kInvalidHandle;
m_handler = std::move(rhs.m_handler);
m_expirationTime = std::move(rhs.m_expirationTime);
m_period = std::move(rhs.m_period);
m_periodic = std::move(rhs.m_periodic);
return *this;
}
void Notifier::SetHandler(TimerEventHandler handler) {
std::lock_guard<wpi::mutex> lock(m_processMutex);
m_handler = handler;

View File

@@ -7,6 +7,8 @@
#include "frc/PWM.h"
#include <utility>
#include <hal/HAL.h>
#include <hal/PWM.h>
#include <hal/Ports.h>
@@ -57,6 +59,23 @@ PWM::~PWM() {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
}
PWM::PWM(PWM&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
m_channel(std::move(rhs.m_channel)) {
std::swap(m_handle, rhs.m_handle);
}
PWM& PWM::operator=(PWM&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
m_channel = std::move(rhs.m_channel);
std::swap(m_handle, rhs.m_handle);
return *this;
}
void PWM::SetRaw(uint16_t value) {
if (StatusIsFatal()) return;

View File

@@ -7,6 +7,8 @@
#include "frc/Relay.h"
#include <utility>
#include <hal/HAL.h>
#include <hal/Ports.h>
#include <hal/Relay.h>
@@ -90,6 +92,31 @@ Relay::~Relay() {
if (m_reverseHandle != HAL_kInvalidHandle) HAL_FreeRelayPort(m_reverseHandle);
}
Relay::Relay(Relay&& rhs)
: MotorSafety(std::move(rhs)),
ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
m_channel(std::move(rhs.m_channel)),
m_direction(std::move(rhs.m_direction)),
m_safetyHelper(std::move(rhs.m_safetyHelper)) {
std::swap(m_forwardHandle, rhs.m_forwardHandle);
std::swap(m_reverseHandle, rhs.m_reverseHandle);
}
Relay& Relay::operator=(Relay&& rhs) {
MotorSafety::operator=(std::move(rhs));
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
m_channel = std::move(rhs.m_channel);
m_direction = std::move(rhs.m_direction);
std::swap(m_forwardHandle, rhs.m_forwardHandle);
std::swap(m_reverseHandle, rhs.m_reverseHandle);
m_safetyHelper = std::move(rhs.m_safetyHelper);
return *this;
}
void Relay::Set(Relay::Value value) {
if (StatusIsFatal()) return;

View File

@@ -103,3 +103,7 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
LiveWindow::GetInstance()->SetEnabled(false);
}
RobotBase::RobotBase(RobotBase&&) : m_ds(DriverStation::GetInstance()) {}
RobotBase& RobotBase::operator=(RobotBase&&) { return *this; }

View File

@@ -8,6 +8,7 @@
#include "frc/SPI.h"
#include <cstring>
#include <utility>
#include <hal/HAL.h>
#include <hal/SPI.h>
@@ -138,6 +139,27 @@ SPI::SPI(Port port) : m_port(static_cast<HAL_SPIPort>(port)) {
SPI::~SPI() { HAL_CloseSPI(m_port); }
SPI::SPI(SPI&& rhs)
: ErrorBase(std::move(rhs)),
m_msbFirst(std::move(rhs.m_msbFirst)),
m_sampleOnTrailing(std::move(rhs.m_sampleOnTrailing)),
m_clockIdleHigh(std::move(rhs.m_clockIdleHigh)),
m_accum(std::move(rhs.m_accum)) {
std::swap(m_port, rhs.m_port);
}
SPI& SPI::operator=(SPI&& rhs) {
ErrorBase::operator=(std::move(rhs));
std::swap(m_port, rhs.m_port);
m_msbFirst = std::move(rhs.m_msbFirst);
m_sampleOnTrailing = std::move(rhs.m_sampleOnTrailing);
m_clockIdleHigh = std::move(rhs.m_clockIdleHigh);
m_accum = std::move(rhs.m_accum);
return *this;
}
void SPI::SetClockRate(double hz) { HAL_SetSPISpeed(m_port, hz); }
void SPI::SetMSBFirst() {

View File

@@ -7,6 +7,8 @@
#include "frc/SerialPort.h"
#include <utility>
#include <hal/HAL.h>
#include <hal/SerialPort.h>
@@ -95,6 +97,25 @@ SerialPort::~SerialPort() {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
}
SerialPort::SerialPort(SerialPort&& rhs)
: ErrorBase(std::move(rhs)),
m_resourceManagerHandle(std::move(rhs.m_resourceManagerHandle)),
m_portHandle(std::move(rhs.m_portHandle)),
m_consoleModeEnabled(std::move(rhs.m_consoleModeEnabled)) {
std::swap(m_port, rhs.m_port);
}
SerialPort& SerialPort::operator=(SerialPort&& rhs) {
ErrorBase::operator=(std::move(rhs));
m_resourceManagerHandle = std::move(rhs.m_resourceManagerHandle);
m_portHandle = std::move(rhs.m_portHandle);
m_consoleModeEnabled = std::move(rhs.m_consoleModeEnabled);
std::swap(m_port, rhs.m_port);
return *this;
}
void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl) {
int32_t status = 0;
HAL_SetSerialFlowControl(static_cast<HAL_SerialPort>(m_port), flowControl,

View File

@@ -7,6 +7,8 @@
#include "frc/Solenoid.h"
#include <utility>
#include <hal/HAL.h>
#include <hal/Ports.h>
#include <hal/Solenoid.h>
@@ -50,6 +52,20 @@ Solenoid::Solenoid(int moduleNumber, int channel)
Solenoid::~Solenoid() { HAL_FreeSolenoidPort(m_solenoidHandle); }
Solenoid::Solenoid(Solenoid&& rhs)
: SolenoidBase(std::move(rhs)), m_channel(std::move(rhs.m_channel)) {
std::swap(m_solenoidHandle, rhs.m_solenoidHandle);
}
Solenoid& Solenoid::operator=(Solenoid&& rhs) {
SolenoidBase::operator=(std::move(rhs));
std::swap(m_solenoidHandle, rhs.m_solenoidHandle);
m_channel = std::move(rhs.m_channel);
return *this;
}
void Solenoid::Set(bool on) {
if (StatusIsFatal()) return;
int32_t status = 0;

View File

@@ -9,6 +9,8 @@
#include <stdint.h>
#include <utility>
#include <hal/HAL.h>
#include "frc/Timer.h"
@@ -61,6 +63,22 @@ TimedRobot::~TimedRobot() {
HAL_CleanNotifier(m_notifier, &status);
}
TimedRobot::TimedRobot(TimedRobot&& rhs)
: IterativeRobotBase(std::move(rhs)),
m_expirationTime(std::move(rhs.m_expirationTime)) {
std::swap(m_notifier, rhs.m_notifier);
}
TimedRobot& TimedRobot::operator=(TimedRobot&& rhs) {
IterativeRobotBase::operator=(std::move(rhs));
ErrorBase::operator=(std::move(rhs));
std::swap(m_notifier, rhs.m_notifier);
m_expirationTime = std::move(rhs.m_expirationTime);
return *this;
}
void TimedRobot::UpdateAlarm() {
int32_t status = 0;
HAL_UpdateNotifierAlarm(

View File

@@ -119,7 +119,9 @@ void Ultrasonic::SetAutomaticMode(bool enabling) {
// m_task.SetPriority(kPriority);
} else {
// Wait for background task to stop running
m_thread.join();
if (m_thread.joinable()) {
m_thread.join();
}
// Clear all the counters (data now invalid) since automatic mode is
// disabled. No synchronization is needed because the background task is

View File

@@ -7,6 +7,8 @@
#include "frc/smartdashboard/SendableBase.h"
#include <utility>
#include "frc/livewindow/LiveWindow.h"
using namespace frc;
@@ -17,6 +19,20 @@ SendableBase::SendableBase(bool addLiveWindow) {
SendableBase::~SendableBase() { LiveWindow::GetInstance()->Remove(this); }
SendableBase::SendableBase(SendableBase&& rhs) {
m_name = std::move(rhs.m_name);
m_subsystem = std::move(rhs.m_subsystem);
}
SendableBase& SendableBase::operator=(SendableBase&& rhs) {
Sendable::operator=(std::move(rhs));
m_name = std::move(rhs.m_name);
m_subsystem = std::move(rhs.m_subsystem);
return *this;
}
std::string SendableBase::GetName() const {
std::lock_guard<wpi::mutex> lock(m_mutex);
return m_name;