artf4154: Get rid of raw pointers in C++.

This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.

There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
  which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
  for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
  the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
  existing.
-This doesn't fall directly under raw pointer stuff,
  but move syntax and rvalue references could be introduced
  in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
  in wpilibC++). Someone should figure out a more
  permanent fix (eg, just renaming them), then doing
  what I did (making a new namespace for one of them,
  essentially the same as renaming it).

A few other things:
-I created a NullDeleter class which is marked as deprecated.
  What this does is it can be passed as the deleter to a
  std::shared_ptr so that when you are converting raw pointers
  to shared_ptrs the shared_ptr doesn't do any deletion if
  someone else owns the raw pointer. This should only be
  used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
  emit errors when deprecated functions called deprecated
  functions. Unfortunately, gradle doesn't appear to be
  actually printing out gcc warnigns for some reason.
  The best way I have found to fix this is to patch
  the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
  so that a deprecated function calling a deprecated
  function is fine but a non-deprecated function calling
  a deprecated function will throw a warning (which we
  then elevate with -Werror). I believe that clang
  deals with this properly, although I have not
  tried it myself.

Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
This commit is contained in:
James Kuszmaul
2015-06-30 15:01:20 -04:00
parent fd4c169658
commit 534ea134a4
164 changed files with 1278 additions and 1110 deletions

View File

@@ -36,11 +36,11 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
case k4X: {
m_encodingScale = 4;
if (m_aSource->StatusIsFatal()) {
CloneError(m_aSource);
CloneError(*m_aSource);
return;
}
if (m_bSource->StatusIsFatal()) {
CloneError(m_bSource);
CloneError(*m_bSource);
return;
}
int32_t status = 0;
@@ -58,8 +58,8 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
case k1X:
case k2X: {
m_encodingScale = encodingType == k1X ? 1 : 2;
m_counter =
new Counter(m_encodingType, m_aSource, m_bSource, reverseDirection);
m_counter = ::std::make_unique<Counter>(m_encodingType, m_aSource,
m_bSource, reverseDirection);
m_index = m_counter->GetFPGAIndex();
break;
}
@@ -69,7 +69,7 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
}
HALReport(HALUsageReporting::kResourceType_Encoder, m_index, encodingType);
LiveWindow::GetInstance()->AddSensor("Encoder",
LiveWindow::GetInstance().AddSensor("Encoder",
m_aSource->GetChannelForRouting(), this);
}
@@ -98,21 +98,16 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
*/
Encoder::Encoder(uint32_t aChannel, uint32_t bChannel, bool reverseDirection,
EncodingType encodingType) {
m_aSource = new DigitalInput(aChannel);
m_bSource = new DigitalInput(bChannel);
m_aSource = ::std::make_shared<DigitalInput>(aChannel);
m_bSource = ::std::make_shared<DigitalInput>(bChannel);
InitEncoder(reverseDirection, encodingType);
m_allocatedASource = true;
m_allocatedBSource = true;
}
/**
* Encoder constructor.
* Construct a Encoder given a and b channels as digital inputs. This is used in
* the case
* where the digital inputs are shared. The Encoder class will not allocate the
* digital inputs
* and assume that they already are counted.
*
* the case where the digital inputs are shared. The Encoder class will not
* allocate the digital inputs and assume that they already are counted.
* The counter will start counting immediately.
*
* @param aSource The source that should be used for the a channel.
@@ -131,11 +126,19 @@ Encoder::Encoder(uint32_t aChannel, uint32_t bChannel, bool reverseDirection,
* or be double (2x) the spec'd count.
*/
Encoder::Encoder(DigitalSource *aSource, DigitalSource *bSource,
bool reverseDirection, EncodingType encodingType) {
m_aSource = aSource;
m_bSource = bSource;
m_allocatedASource = false;
m_allocatedBSource = false;
bool reverseDirection, EncodingType encodingType)
: m_aSource(aSource, NullDeleter<DigitalSource>()),
m_bSource(bSource, NullDeleter<DigitalSource>()) {
if (m_aSource == nullptr || m_bSource == nullptr)
wpi_setWPIError(NullParameter);
else
InitEncoder(reverseDirection, encodingType);
}
Encoder::Encoder(::std::shared_ptr<DigitalSource> aSource,
::std::shared_ptr<DigitalSource> bSource,
bool reverseDirection, EncodingType encodingType)
: m_aSource(aSource), m_bSource(bSource) {
if (m_aSource == nullptr || m_bSource == nullptr)
wpi_setWPIError(NullParameter);
else
@@ -168,11 +171,10 @@ Encoder::Encoder(DigitalSource *aSource, DigitalSource *bSource,
* or be double (2x) the spec'd count.
*/
Encoder::Encoder(DigitalSource &aSource, DigitalSource &bSource,
bool reverseDirection, EncodingType encodingType) {
m_aSource = &aSource;
m_bSource = &bSource;
m_allocatedASource = false;
m_allocatedBSource = false;
bool reverseDirection, EncodingType encodingType)
: m_aSource(&aSource, NullDeleter<DigitalSource>()),
m_bSource(&bSource, NullDeleter<DigitalSource>())
{
InitEncoder(reverseDirection, encodingType);
}
@@ -181,11 +183,7 @@ Encoder::Encoder(DigitalSource &aSource, DigitalSource &bSource,
* Frees the FPGA resources associated with an Encoder.
*/
Encoder::~Encoder() {
if (m_allocatedASource) delete m_aSource;
if (m_allocatedBSource) delete m_bSource;
if (m_counter) {
delete m_counter;
} else {
if (!m_counter) {
int32_t status = 0;
freeEncoder(m_encoder, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
@@ -524,15 +522,7 @@ void Encoder::SetIndexSource(uint32_t channel, Encoder::IndexingType type) {
*/
void Encoder::SetIndexSource(DigitalSource *source,
Encoder::IndexingType type) {
int32_t status = 0;
bool activeHigh = (type == kResetWhileHigh) || (type == kResetOnRisingEdge);
bool edgeSensitive =
(type == kResetOnFallingEdge) || (type == kResetOnRisingEdge);
setEncoderIndexSource(m_encoder, source->GetChannelForRouting(),
source->GetAnalogTriggerForRouting(), activeHigh,
edgeSensitive, &status);
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
SetIndexSource(*source, type);
}
/**
@@ -542,9 +532,17 @@ void Encoder::SetIndexSource(DigitalSource *source,
* @param channel A digital source to set as the encoder index
* @param type The state that will cause the encoder to reset
*/
void Encoder::SetIndexSource(DigitalSource &source,
void Encoder::SetIndexSource(const DigitalSource &source,
Encoder::IndexingType type) {
SetIndexSource(&source, type);
int32_t status = 0;
bool activeHigh = (type == kResetWhileHigh) || (type == kResetOnRisingEdge);
bool edgeSensitive =
(type == kResetOnFallingEdge) || (type == kResetOnRisingEdge);
setEncoderIndexSource(m_encoder, source.GetChannelForRouting(),
source.GetAnalogTriggerForRouting(), activeHigh,
edgeSensitive, &status);
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
}
void Encoder::UpdateTable() {
@@ -566,9 +564,9 @@ std::string Encoder::GetSmartDashboardType() const {
return "Encoder";
}
void Encoder::InitTable(ITable *subTable) {
void Encoder::InitTable(::std::shared_ptr<ITable> subTable) {
m_table = subTable;
UpdateTable();
}
ITable *Encoder::GetTable() const { return m_table; }
::std::shared_ptr<ITable> Encoder::GetTable() const { return m_table; }