Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include "Base.h" |
| 11 | #include "Error.h" |
| 12 | #include "HAL/cpp/priority_mutex.h" |
| 13 | #include "llvm/StringRef.h" |
| 14 | |
| 15 | #define wpi_setErrnoErrorWithContext(context) \ |
| 16 | this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__) |
| 17 | #define wpi_setErrnoError() wpi_setErrnoErrorWithContext("") |
| 18 | #define wpi_setImaqErrorWithContext(code, context) \ |
| 19 | do { \ |
| 20 | if ((code) != 0) \ |
| 21 | this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ |
| 22 | } while (0) |
| 23 | #define wpi_setErrorWithContext(code, context) \ |
| 24 | do { \ |
| 25 | if ((code) != 0) \ |
| 26 | this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ |
| 27 | } while (0) |
| 28 | #define wpi_setErrorWithContextRange(code, min, max, req, context) \ |
| 29 | do { \ |
| 30 | if ((code) != 0) \ |
| 31 | this->SetErrorRange((code), (min), (max), (req), (context), __FILE__, \ |
| 32 | __FUNCTION__, __LINE__); \ |
| 33 | } while (0) |
| 34 | #define wpi_setError(code) wpi_setErrorWithContext(code, "") |
| 35 | #define wpi_setStaticErrorWithContext(object, code, context) \ |
| 36 | do { \ |
| 37 | if ((code) != 0) \ |
| 38 | object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ |
| 39 | } while (0) |
| 40 | #define wpi_setStaticError(object, code) \ |
| 41 | wpi_setStaticErrorWithContext(object, code, "") |
| 42 | #define wpi_setGlobalErrorWithContext(code, context) \ |
| 43 | do { \ |
| 44 | if ((code) != 0) \ |
| 45 | ::frc::ErrorBase::SetGlobalError((code), (context), __FILE__, \ |
| 46 | __FUNCTION__, __LINE__); \ |
| 47 | } while (0) |
| 48 | #define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "") |
| 49 | #define wpi_setWPIErrorWithContext(error, context) \ |
| 50 | this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \ |
| 51 | (context), __FILE__, __FUNCTION__, __LINE__) |
| 52 | #define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, "")) |
| 53 | #define wpi_setStaticWPIErrorWithContext(object, error, context) \ |
| 54 | object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \ |
| 55 | __FUNCTION__, __LINE__) |
| 56 | #define wpi_setStaticWPIError(object, error) \ |
| 57 | wpi_setStaticWPIErrorWithContext(object, error, "") |
| 58 | #define wpi_setGlobalWPIErrorWithContext(error, context) \ |
| 59 | ::frc::ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), \ |
| 60 | __FILE__, __FUNCTION__, __LINE__) |
| 61 | #define wpi_setGlobalWPIError(error) wpi_setGlobalWPIErrorWithContext(error, "") |
| 62 | |
| 63 | namespace frc { |
| 64 | |
| 65 | /** |
| 66 | * Base class for most objects. |
| 67 | * ErrorBase is the base class for most objects since it holds the generated |
| 68 | * error |
| 69 | * for that object. In addition, there is a single instance of a global error |
| 70 | * object |
| 71 | */ |
| 72 | class ErrorBase { |
| 73 | // TODO: Consider initializing instance variables and cleanup in destructor |
| 74 | public: |
| 75 | ErrorBase() = default; |
| 76 | virtual ~ErrorBase() = default; |
| 77 | |
| 78 | ErrorBase(const ErrorBase&) = delete; |
| 79 | ErrorBase& operator=(const ErrorBase&) = delete; |
| 80 | |
| 81 | virtual Error& GetError(); |
| 82 | virtual const Error& GetError() const; |
| 83 | virtual void SetErrnoError(llvm::StringRef contextMessage, |
| 84 | llvm::StringRef filename, llvm::StringRef function, |
| 85 | int lineNumber) const; |
| 86 | virtual void SetImaqError(int success, llvm::StringRef contextMessage, |
| 87 | llvm::StringRef filename, llvm::StringRef function, |
| 88 | int lineNumber) const; |
| 89 | virtual void SetError(Error::Code code, llvm::StringRef contextMessage, |
| 90 | llvm::StringRef filename, llvm::StringRef function, |
| 91 | int lineNumber) const; |
| 92 | virtual void SetErrorRange(Error::Code code, int32_t minRange, |
| 93 | int32_t maxRange, int32_t requestedValue, |
| 94 | llvm::StringRef contextMessage, |
| 95 | llvm::StringRef filename, llvm::StringRef function, |
| 96 | int lineNumber) const; |
| 97 | virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code, |
| 98 | llvm::StringRef contextMessage, |
| 99 | llvm::StringRef filename, llvm::StringRef function, |
| 100 | int lineNumber) const; |
| 101 | virtual void CloneError(const ErrorBase& rhs) const; |
| 102 | virtual void ClearError() const; |
| 103 | virtual bool StatusIsFatal() const; |
| 104 | static void SetGlobalError(Error::Code code, llvm::StringRef contextMessage, |
| 105 | llvm::StringRef filename, llvm::StringRef function, |
| 106 | int lineNumber); |
| 107 | static void SetGlobalWPIError(llvm::StringRef errorMessage, |
| 108 | llvm::StringRef contextMessage, |
| 109 | llvm::StringRef filename, |
| 110 | llvm::StringRef function, int lineNumber); |
| 111 | static Error& GetGlobalError(); |
| 112 | |
| 113 | protected: |
| 114 | mutable Error m_error; |
| 115 | // TODO: Replace globalError with a global list of all errors. |
| 116 | static priority_mutex _globalErrorMutex; |
| 117 | static Error _globalError; |
| 118 | }; |
| 119 | |
| 120 | } // namespace frc |