Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. |
| 3 | */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | #pragma once |
| 8 | |
| 9 | #include "Base.h" |
| 10 | #include "Error.h" |
| 11 | |
| 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_setError(code) wpi_setErrorWithContext(code, "") |
| 29 | #define wpi_setStaticErrorWithContext(object, code, context) \ |
| 30 | do { \ |
| 31 | if ((code) != 0) \ |
| 32 | object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ |
| 33 | } while (0) |
| 34 | #define wpi_setStaticError(object, code) \ |
| 35 | wpi_setStaticErrorWithContext(object, code, "") |
| 36 | #define wpi_setGlobalErrorWithContext(code, context) \ |
| 37 | do { \ |
| 38 | if ((code) != 0) \ |
| 39 | ErrorBase::SetGlobalError((code), (context), __FILE__, __FUNCTION__, \ |
| 40 | __LINE__); \ |
| 41 | } while (0) |
| 42 | #define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "") |
| 43 | #define wpi_setWPIErrorWithContext(error, context) \ |
| 44 | this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \ |
| 45 | (context), __FILE__, __FUNCTION__, __LINE__) |
| 46 | #define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, "")) |
| 47 | #define wpi_setStaticWPIErrorWithContext(object, error, context) \ |
| 48 | object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \ |
| 49 | __FUNCTION__, __LINE__) |
| 50 | #define wpi_setStaticWPIError(object, error) \ |
| 51 | wpi_setStaticWPIErrorWithContext(object, error, "") |
| 52 | #define wpi_setGlobalWPIErrorWithContext(error, context) \ |
| 53 | ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), __FILE__, \ |
| 54 | __FUNCTION__, __LINE__) |
| 55 | #define wpi_setGlobalWPIError(error) \ |
| 56 | wpi_setGlobalWPIErrorWithContext(error, "") |
| 57 | |
| 58 | /** |
| 59 | * Base class for most objects. |
| 60 | * ErrorBase is the base class for most objects since it holds the generated |
| 61 | * error |
| 62 | * for that object. In addition, there is a single instance of a global error |
| 63 | * object |
| 64 | */ |
| 65 | class ErrorBase { |
| 66 | // TODO: Consider initializing instance variables and cleanup in destructor |
| 67 | public: |
| 68 | ErrorBase() = default; |
| 69 | virtual ~ErrorBase() = default; |
| 70 | |
| 71 | ErrorBase(const ErrorBase&) = delete; |
| 72 | ErrorBase& operator=(const ErrorBase&) = delete; |
| 73 | |
| 74 | virtual Error& GetError(); |
| 75 | virtual const Error& GetError() const; |
| 76 | virtual void SetErrnoError(llvm::StringRef contextMessage, |
| 77 | llvm::StringRef filename, llvm::StringRef function, |
| 78 | uint32_t lineNumber) const; |
| 79 | virtual void SetImaqError(int success, llvm::StringRef contextMessage, |
| 80 | llvm::StringRef filename, llvm::StringRef function, |
| 81 | uint32_t lineNumber) const; |
| 82 | virtual void SetError(Error::Code code, llvm::StringRef contextMessage, |
| 83 | llvm::StringRef filename, llvm::StringRef function, |
| 84 | uint32_t lineNumber) const; |
| 85 | virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code, |
| 86 | llvm::StringRef contextMessage, |
| 87 | llvm::StringRef filename, llvm::StringRef function, |
| 88 | uint32_t lineNumber) const; |
| 89 | virtual void CloneError(const ErrorBase& rhs) const; |
| 90 | virtual void ClearError() const; |
| 91 | virtual bool StatusIsFatal() const; |
| 92 | static void SetGlobalError(Error::Code code, llvm::StringRef contextMessage, |
| 93 | llvm::StringRef filename, llvm::StringRef function, |
| 94 | uint32_t lineNumber); |
| 95 | static void SetGlobalWPIError(llvm::StringRef errorMessage, |
| 96 | llvm::StringRef contextMessage, |
| 97 | llvm::StringRef filename, |
| 98 | llvm::StringRef function, uint32_t lineNumber); |
| 99 | static Error& GetGlobalError(); |
| 100 | |
| 101 | protected: |
| 102 | mutable Error m_error; |
| 103 | // TODO: Replace globalError with a global list of all errors. |
| 104 | static priority_mutex _globalErrorMutex; |
| 105 | static Error _globalError; |
| 106 | }; |