WPILibC++  unspecified
ErrorBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 <llvm/StringRef.h>
11 #include <llvm/Twine.h>
12 #include <support/mutex.h>
13 
14 #include "Base.h"
15 #include "Error.h"
16 
17 #define wpi_setErrnoErrorWithContext(context) \
18  this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__)
19 #define wpi_setErrnoError() wpi_setErrnoErrorWithContext("")
20 #define wpi_setImaqErrorWithContext(code, context) \
21  do { \
22  if ((code) != 0) \
23  this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
24  } while (0)
25 #define wpi_setErrorWithContext(code, context) \
26  do { \
27  if ((code) != 0) \
28  this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
29  } while (0)
30 #define wpi_setErrorWithContextRange(code, min, max, req, context) \
31  do { \
32  if ((code) != 0) \
33  this->SetErrorRange((code), (min), (max), (req), (context), __FILE__, \
34  __FUNCTION__, __LINE__); \
35  } while (0)
36 #define wpi_setError(code) wpi_setErrorWithContext(code, "")
37 #define wpi_setStaticErrorWithContext(object, code, context) \
38  do { \
39  if ((code) != 0) \
40  object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
41  } while (0)
42 #define wpi_setStaticError(object, code) \
43  wpi_setStaticErrorWithContext(object, code, "")
44 #define wpi_setGlobalErrorWithContext(code, context) \
45  do { \
46  if ((code) != 0) \
47  ::frc::ErrorBase::SetGlobalError((code), (context), __FILE__, \
48  __FUNCTION__, __LINE__); \
49  } while (0)
50 #define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "")
51 #define wpi_setWPIErrorWithContext(error, context) \
52  this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \
53  (context), __FILE__, __FUNCTION__, __LINE__)
54 #define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, ""))
55 #define wpi_setStaticWPIErrorWithContext(object, error, context) \
56  object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \
57  __FUNCTION__, __LINE__)
58 #define wpi_setStaticWPIError(object, error) \
59  wpi_setStaticWPIErrorWithContext(object, error, "")
60 #define wpi_setGlobalWPIErrorWithContext(error, context) \
61  ::frc::ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), \
62  __FILE__, __FUNCTION__, __LINE__)
63 #define wpi_setGlobalWPIError(error) wpi_setGlobalWPIErrorWithContext(error, "")
64 
65 namespace frc {
66 
74 class ErrorBase {
75  // TODO: Consider initializing instance variables and cleanup in destructor
76  public:
77  ErrorBase();
78  virtual ~ErrorBase() = default;
79 
80  ErrorBase(const ErrorBase&) = delete;
81  ErrorBase& operator=(const ErrorBase&) = delete;
82 
83  virtual Error& GetError();
84  virtual const Error& GetError() const;
85  virtual void SetErrnoError(const llvm::Twine& contextMessage,
86  llvm::StringRef filename, llvm::StringRef function,
87  int lineNumber) const;
88  virtual void SetImaqError(int success, const llvm::Twine& contextMessage,
89  llvm::StringRef filename, llvm::StringRef function,
90  int lineNumber) const;
91  virtual void SetError(Error::Code code, const llvm::Twine& contextMessage,
92  llvm::StringRef filename, llvm::StringRef function,
93  int lineNumber) const;
94  virtual void SetErrorRange(Error::Code code, int32_t minRange,
95  int32_t maxRange, int32_t requestedValue,
96  const llvm::Twine& contextMessage,
97  llvm::StringRef filename, llvm::StringRef function,
98  int lineNumber) const;
99  virtual void SetWPIError(const llvm::Twine& errorMessage, Error::Code code,
100  const llvm::Twine& contextMessage,
101  llvm::StringRef filename, llvm::StringRef function,
102  int lineNumber) const;
103  virtual void CloneError(const ErrorBase& rhs) const;
104  virtual void ClearError() const;
105  virtual bool StatusIsFatal() const;
106  static void SetGlobalError(Error::Code code,
107  const llvm::Twine& contextMessage,
108  llvm::StringRef filename, llvm::StringRef function,
109  int lineNumber);
110  static void SetGlobalWPIError(const llvm::Twine& errorMessage,
111  const llvm::Twine& contextMessage,
112  llvm::StringRef filename,
113  llvm::StringRef function, int lineNumber);
114  static Error& GetGlobalError();
115 
116  protected:
117  mutable Error m_error;
118 
119  // TODO: Replace globalError with a global list of all errors.
120  static wpi::mutex _globalErrorMutex;
121  static Error _globalError;
122 };
123 
124 } // namespace frc
virtual void SetErrorRange(Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, const llvm::Twine &contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:141
Definition: RobotController.cpp:14
virtual void SetWPIError(const llvm::Twine &errorMessage, Error::Code code, const llvm::Twine &contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:172
virtual void SetErrnoError(const llvm::Twine &contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set error information associated with a C library call that set an error to the "errno" global variab...
Definition: ErrorBase.cpp:52
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:196
virtual void SetError(Error::Code code, const llvm::Twine &contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:112
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
virtual void SetImaqError(int success, const llvm::Twine &contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated from the nivision Imaq API.
Definition: ErrorBase.cpp:86
virtual Error & GetError()
Retrieve the current error.
Definition: ErrorBase.cpp:34
Base class for most objects.
Definition: ErrorBase.h:74
virtual void ClearError() const
Clear the current error information associated with this sensor.
Definition: ErrorBase.cpp:41
Error object represents a library error.
Definition: Error.h:32
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
static Error & GetGlobalError()
Retrieve the current global error.
Definition: ErrorBase.cpp:227