diff --git a/wpilibc/src/main/native/cpp/Error.cpp b/wpilibc/src/main/native/cpp/Error.cpp index fcd74c5117..03f238fc6d 100644 --- a/wpilibc/src/main/native/cpp/Error.cpp +++ b/wpilibc/src/main/native/cpp/Error.cpp @@ -15,14 +15,30 @@ using namespace frc; -void Error::Clone(const Error& error) { - m_code = error.m_code; - m_message = error.m_message; - m_filename = error.m_filename; - m_function = error.m_function; - m_lineNumber = error.m_lineNumber; - m_originatingObject = error.m_originatingObject; - m_timestamp = error.m_timestamp; +Error::Error(Code code, const wpi::Twine& contextMessage, + wpi::StringRef filename, wpi::StringRef function, int lineNumber, + const ErrorBase* originatingObject) { + Set(code, contextMessage, filename, function, lineNumber, originatingObject); +} + +bool Error::operator<(const Error& rhs) const { + if (m_code < rhs.m_code) { + return true; + } else if (m_message < rhs.m_message) { + return true; + } else if (m_filename < rhs.m_filename) { + return true; + } else if (m_function < rhs.m_function) { + return true; + } else if (m_lineNumber < rhs.m_lineNumber) { + return true; + } else if (m_originatingObject < rhs.m_originatingObject) { + return true; + } else if (m_timestamp < rhs.m_timestamp) { + return true; + } else { + return false; + } } Error::Code Error::GetCode() const { return m_code; } diff --git a/wpilibc/src/main/native/cpp/ErrorBase.cpp b/wpilibc/src/main/native/cpp/ErrorBase.cpp index 5ace13d9c2..d801d5e2d9 100644 --- a/wpilibc/src/main/native/cpp/ErrorBase.cpp +++ b/wpilibc/src/main/native/cpp/ErrorBase.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -21,8 +22,8 @@ using namespace frc; -wpi::mutex ErrorBase::_globalErrorMutex; -Error ErrorBase::_globalError; +static wpi::mutex globalErrorsMutex; +static std::set globalErrors; ErrorBase::ErrorBase() { HAL_Initialize(500, 0); } @@ -50,10 +51,8 @@ void ErrorBase::SetErrnoError(const wpi::Twine& contextMessage, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); - if (_globalError.GetCode() == 0) { - _globalError.Clone(m_error); - } + std::lock_guard mutex(globalErrorsMutex); + globalErrors.insert(m_error); } void ErrorBase::SetImaqError(int success, const wpi::Twine& contextMessage, @@ -66,10 +65,8 @@ void ErrorBase::SetImaqError(int success, const wpi::Twine& contextMessage, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); - if (_globalError.GetCode() == 0) { - _globalError.Clone(m_error); - } + std::lock_guard mutex(globalErrorsMutex); + globalErrors.insert(m_error); } } @@ -82,10 +79,8 @@ void ErrorBase::SetError(Error::Code code, const wpi::Twine& contextMessage, m_error.Set(code, contextMessage, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); - if (_globalError.GetCode() == 0) { - _globalError.Clone(m_error); - } + std::lock_guard mutex(globalErrorsMutex); + globalErrors.insert(m_error); } } @@ -104,10 +99,8 @@ void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); - if (_globalError.GetCode() == 0) { - _globalError.Clone(m_error); - } + std::lock_guard mutex(globalErrorsMutex); + globalErrors.insert(m_error); } } @@ -120,14 +113,12 @@ void ErrorBase::SetWPIError(const wpi::Twine& errorMessage, Error::Code code, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); - if (_globalError.GetCode() == 0) { - _globalError.Clone(m_error); - } + std::lock_guard mutex(globalErrorsMutex); + globalErrors.insert(m_error); } void ErrorBase::CloneError(const ErrorBase& rhs) const { - m_error.Clone(rhs.GetError()); + m_error = rhs.GetError(); } bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; } @@ -138,11 +129,11 @@ void ErrorBase::SetGlobalError(Error::Code code, int lineNumber) { // If there was an error if (code != 0) { - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(globalErrorsMutex); // Set the current error information for this object. - _globalError.Set(code, contextMessage, filename, function, lineNumber, - nullptr); + globalErrors.emplace(code, contextMessage, filename, function, lineNumber, + nullptr); } } @@ -150,15 +141,12 @@ void ErrorBase::SetGlobalWPIError(const wpi::Twine& errorMessage, const wpi::Twine& contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) { - std::lock_guard mutex(_globalErrorMutex); - if (_globalError.GetCode() != 0) { - _globalError.Clear(); - } - _globalError.Set(-1, errorMessage + ": " + contextMessage, filename, function, - lineNumber, nullptr); + std::lock_guard mutex(globalErrorsMutex); + globalErrors.emplace(-1, errorMessage + ": " + contextMessage, filename, + function, lineNumber, nullptr); } -Error& ErrorBase::GetGlobalError() { - std::lock_guard mutex(_globalErrorMutex); - return _globalError; +const Error& ErrorBase::GetGlobalError() { + std::lock_guard mutex(globalErrorsMutex); + return *globalErrors.begin(); } diff --git a/wpilibc/src/main/native/include/Error.h b/wpilibc/src/main/native/include/Error.h index 551b5c6a97..274345112d 100644 --- a/wpilibc/src/main/native/include/Error.h +++ b/wpilibc/src/main/native/include/Error.h @@ -34,11 +34,12 @@ class Error { typedef int Code; Error() = default; + Error(Code code, const wpi::Twine& contextMessage, wpi::StringRef filename, + wpi::StringRef function, int lineNumber, + const ErrorBase* originatingObject); - Error(const Error&) = delete; - Error& operator=(const Error&) = delete; + bool operator<(const Error& rhs) const; - void Clone(const Error& error); Code GetCode() const; std::string GetMessage() const; std::string GetFilename() const; diff --git a/wpilibc/src/main/native/include/ErrorBase.h b/wpilibc/src/main/native/include/ErrorBase.h index 0950df5321..44c913bd67 100644 --- a/wpilibc/src/main/native/include/ErrorBase.h +++ b/wpilibc/src/main/native/include/ErrorBase.h @@ -193,14 +193,10 @@ class ErrorBase { /** * Retrieve the current global error. */ - static Error& GetGlobalError(); + static const Error& GetGlobalError(); protected: mutable Error m_error; - - // TODO: Replace globalError with a global list of all errors. - static wpi::mutex _globalErrorMutex; - static Error _globalError; }; } // namespace frc