WPILibC++  2020.3.2-60-g3011ebe
ErrorBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 FIRST. 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 <vector>
11 
12 #include <wpi/StringRef.h>
13 #include <wpi/Twine.h>
14 #include <wpi/mutex.h>
15 
16 #include "frc/Base.h"
17 #include "frc/Error.h"
18 
19 // Forward declared manually to avoid needing to pull in entire HAL header.
20 extern "C" const char* HAL_GetErrorMessage(int32_t code);
21 
22 #define wpi_setErrnoErrorWithContext(context) \
23  this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__)
24 #define wpi_setErrnoError() wpi_setErrnoErrorWithContext("")
25 #define wpi_setImaqErrorWithContext(code, context) \
26  do { \
27  if ((code) != 0) \
28  this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
29  } while (0)
30 #define wpi_setErrorWithContext(code, context) \
31  do { \
32  if ((code) != 0) \
33  this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
34  } while (0)
35 #define wpi_setErrorWithContextRange(code, min, max, req, context) \
36  do { \
37  if ((code) != 0) \
38  this->SetErrorRange((code), (min), (max), (req), (context), __FILE__, \
39  __FUNCTION__, __LINE__); \
40  } while (0)
41 
42 #define wpi_setHALError(code) \
43  do { \
44  if ((code) != 0) \
45  this->SetError((code), HAL_GetErrorMessage(code), __FILE__, \
46  __FUNCTION__, __LINE__); \
47  } while (0)
48 
49 #define wpi_setHALErrorWithRange(code, min, max, req) \
50  do { \
51  if ((code) != 0) \
52  this->SetErrorRange((code), (min), (max), (req), \
53  HAL_GetErrorMessage(code), __FILE__, __FUNCTION__, \
54  __LINE__); \
55  } while (0)
56 
57 #define wpi_setError(code) wpi_setErrorWithContext(code, "")
58 #define wpi_setStaticErrorWithContext(object, code, context) \
59  do { \
60  if ((code) != 0) \
61  object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
62  } while (0)
63 #define wpi_setStaticError(object, code) \
64  wpi_setStaticErrorWithContext(object, code, "")
65 
66 #define wpi_setGlobalErrorWithContext(code, context) \
67  do { \
68  if ((code) != 0) \
69  ::frc::ErrorBase::SetGlobalError((code), (context), __FILE__, \
70  __FUNCTION__, __LINE__); \
71  } while (0)
72 
73 #define wpi_setGlobalHALError(code) \
74  do { \
75  if ((code) != 0) \
76  ::frc::ErrorBase::SetGlobalError((code), HAL_GetErrorMessage(code), \
77  __FILE__, __FUNCTION__, __LINE__); \
78  } while (0)
79 
80 #define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "")
81 #define wpi_setWPIErrorWithContext(error, context) \
82  this->SetWPIError(wpi_error_s_##error(), wpi_error_value_##error(), \
83  (context), __FILE__, __FUNCTION__, __LINE__)
84 #define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, ""))
85 #define wpi_setStaticWPIErrorWithContext(object, error, context) \
86  object->SetWPIError(wpi_error_s_##error(), (context), __FILE__, \
87  __FUNCTION__, __LINE__)
88 #define wpi_setStaticWPIError(object, error) \
89  wpi_setStaticWPIErrorWithContext(object, error, "")
90 #define wpi_setGlobalWPIErrorWithContext(error, context) \
91  ::frc::ErrorBase::SetGlobalWPIError(wpi_error_s_##error(), (context), \
92  __FILE__, __FUNCTION__, __LINE__)
93 #define wpi_setGlobalWPIError(error) wpi_setGlobalWPIErrorWithContext(error, "")
94 
95 namespace frc {
96 
104 class ErrorBase {
105  // TODO: Consider initializing instance variables and cleanup in destructor
106  public:
107  ErrorBase();
108  virtual ~ErrorBase() = default;
109 
110  ErrorBase(const ErrorBase&) = default;
111  ErrorBase& operator=(const ErrorBase&) = default;
112  ErrorBase(ErrorBase&&) = default;
113  ErrorBase& operator=(ErrorBase&&) = default;
114 
120  virtual Error& GetError();
121 
127  virtual const Error& GetError() const;
128 
132  virtual void ClearError() const;
133 
143  virtual void SetErrnoError(const wpi::Twine& contextMessage,
144  wpi::StringRef filename, wpi::StringRef function,
145  int lineNumber) const;
146 
157  virtual void SetImaqError(int success, const wpi::Twine& contextMessage,
158  wpi::StringRef filename, wpi::StringRef function,
159  int lineNumber) const;
160 
170  virtual void SetError(Error::Code code, const wpi::Twine& contextMessage,
171  wpi::StringRef filename, wpi::StringRef function,
172  int lineNumber) const;
173 
187  virtual void SetErrorRange(Error::Code code, int32_t minRange,
188  int32_t maxRange, int32_t requestedValue,
189  const wpi::Twine& contextMessage,
190  wpi::StringRef filename, wpi::StringRef function,
191  int lineNumber) const;
192 
202  virtual void SetWPIError(const wpi::Twine& errorMessage, Error::Code code,
203  const wpi::Twine& contextMessage,
204  wpi::StringRef filename, wpi::StringRef function,
205  int lineNumber) const;
206 
207  virtual void CloneError(const ErrorBase& rhs) const;
208 
214  virtual bool StatusIsFatal() const;
215 
216  static void SetGlobalError(Error::Code code, const wpi::Twine& contextMessage,
217  wpi::StringRef filename, wpi::StringRef function,
218  int lineNumber);
219 
220  static void SetGlobalWPIError(const wpi::Twine& errorMessage,
221  const wpi::Twine& contextMessage,
222  wpi::StringRef filename,
223  wpi::StringRef function, int lineNumber);
224 
228  static Error GetGlobalError();
229 
233  static std::vector<Error> GetGlobalErrors();
234 
238  void ClearGlobalErrors();
239 
240  protected:
241  mutable Error m_error;
242 };
243 
244 } // namespace frc
frc::ErrorBase::SetErrorRange
virtual void SetErrorRange(Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
frc::ErrorBase::GetError
virtual Error & GetError()
Retrieve the current error.
frc::ErrorBase::SetWPIError
virtual void SetWPIError(const wpi::Twine &errorMessage, Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
frc::ErrorBase::ClearGlobalErrors
void ClearGlobalErrors()
Clear global errors.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc::ErrorBase::SetError
virtual void SetError(Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
frc::ErrorBase::ClearError
virtual void ClearError() const
Clear the current error information associated with this sensor.
frc::Error
Error object represents a library error.
Definition: Error.h:31
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
frc::ErrorBase::GetGlobalErrors
static std::vector< Error > GetGlobalErrors()
Retrieve all global errors.
frc::ErrorBase::SetImaqError
virtual void SetImaqError(int success, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
Set the current error information associated from the nivision Imaq API.
frc::ErrorBase::SetErrnoError
virtual void SetErrnoError(const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
Set error information associated with a C library call that set an error to the "errno" global variab...
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
HAL_GetErrorMessage
const char * HAL_GetErrorMessage(int32_t code)
Gets the error message for a specific status code.
frc::ErrorBase::StatusIsFatal
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
frc::ErrorBase::GetGlobalError
static Error GetGlobalError()
Retrieve the last global error.