mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
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:
@@ -12,6 +12,7 @@
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
|
||||
priority_mutex ErrorBase::_globalErrorMutex;
|
||||
Error ErrorBase::_globalError;
|
||||
@@ -42,15 +43,19 @@ void ErrorBase::ClearError() const { m_error.Clear(); }
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetErrnoError(const char* contextMessage, const char* filename,
|
||||
const char* function, uint32_t lineNumber) const {
|
||||
char err[256];
|
||||
void ErrorBase::SetErrnoError(const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) const {
|
||||
std::string err;
|
||||
int errNo = errno;
|
||||
if (errNo == 0) {
|
||||
sprintf(err, "OK: %s", contextMessage);
|
||||
err = "OK: " + contextMessage;
|
||||
} else {
|
||||
snprintf(err, 256, "%s (0x%08X): %s", strerror(errNo), errNo,
|
||||
contextMessage);
|
||||
char buf[256];
|
||||
snprintf(buf, 256, "%s (0x%08X): %s", strerror(errNo), errNo,
|
||||
contextMessage.c_str());
|
||||
err = buf;
|
||||
}
|
||||
|
||||
// Set the current error information for this object.
|
||||
@@ -73,16 +78,17 @@ void ErrorBase::SetErrnoError(const char* contextMessage, const char* filename,
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetImaqError(int success, const char* contextMessage,
|
||||
const char* filename, const char* function,
|
||||
void ErrorBase::SetImaqError(int success, const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) const {
|
||||
// If there was an error
|
||||
if (success <= 0) {
|
||||
char err[256];
|
||||
sprintf(err, "%i: %s", success, contextMessage);
|
||||
std::stringstream err;
|
||||
err << success << ": " << contextMessage;
|
||||
|
||||
// Set the current error information for this object.
|
||||
m_error.Set(success, err, filename, function, lineNumber, this);
|
||||
m_error.Set(success, err.str(), filename, function, lineNumber, this);
|
||||
|
||||
// Update the global error if there is not one already set.
|
||||
std::unique_lock<priority_mutex> mutex(_globalErrorMutex);
|
||||
@@ -101,8 +107,9 @@ void ErrorBase::SetImaqError(int success, const char* contextMessage,
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetError(Error::Code code, const char* contextMessage,
|
||||
const char* filename, const char* function,
|
||||
void ErrorBase::SetError(Error::Code code, const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) const {
|
||||
// If there was an error
|
||||
if (code != 0) {
|
||||
@@ -126,11 +133,12 @@ void ErrorBase::SetError(Error::Code code, const char* contextMessage,
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetWPIError(const char* errorMessage, Error::Code code,
|
||||
const char* contextMessage, const char* filename,
|
||||
const char* function, uint32_t lineNumber) const {
|
||||
char err[256];
|
||||
sprintf(err, "%s: %s", errorMessage, contextMessage);
|
||||
void ErrorBase::SetWPIError(const std::string& errorMessage, Error::Code code,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) const {
|
||||
std::string err = errorMessage + ": " + contextMessage;
|
||||
|
||||
// Set the current error information for this object.
|
||||
m_error.Set(code, err, filename, function, lineNumber, this);
|
||||
@@ -142,8 +150,8 @@ void ErrorBase::SetWPIError(const char* errorMessage, Error::Code code,
|
||||
}
|
||||
}
|
||||
|
||||
void ErrorBase::CloneError(ErrorBase* rhs) const {
|
||||
m_error.Clone(rhs->GetError());
|
||||
void ErrorBase::CloneError(const ErrorBase& rhs) const {
|
||||
m_error.Clone(rhs.GetError());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,8 +161,10 @@ void ErrorBase::CloneError(ErrorBase* rhs) const {
|
||||
*/
|
||||
bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; }
|
||||
|
||||
void ErrorBase::SetGlobalError(Error::Code code, const char* contextMessage,
|
||||
const char* filename, const char* function,
|
||||
void ErrorBase::SetGlobalError(Error::Code code,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) {
|
||||
// If there was an error
|
||||
if (code != 0) {
|
||||
@@ -166,12 +176,12 @@ void ErrorBase::SetGlobalError(Error::Code code, const char* contextMessage,
|
||||
}
|
||||
}
|
||||
|
||||
void ErrorBase::SetGlobalWPIError(const char* errorMessage,
|
||||
const char* contextMessage,
|
||||
const char* filename, const char* function,
|
||||
void ErrorBase::SetGlobalWPIError(const std::string& errorMessage,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) {
|
||||
char err[256];
|
||||
sprintf(err, "%s: %s", errorMessage, contextMessage);
|
||||
std::string err = errorMessage + ": " + contextMessage;
|
||||
|
||||
std::unique_lock<priority_mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() != 0) {
|
||||
|
||||
Reference in New Issue
Block a user