WPILibC++  unspecified
ErrorBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 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 <mutex>
11 
12 #include <llvm/StringRef.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(llvm::StringRef contextMessage,
86  llvm::StringRef filename, llvm::StringRef function,
87  int lineNumber) const;
88  virtual void SetImaqError(int success, llvm::StringRef contextMessage,
89  llvm::StringRef filename, llvm::StringRef function,
90  int lineNumber) const;
91  virtual void SetError(Error::Code code, llvm::StringRef 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  llvm::StringRef contextMessage,
97  llvm::StringRef filename, llvm::StringRef function,
98  int lineNumber) const;
99  virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code,
100  llvm::StringRef 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, llvm::StringRef contextMessage,
107  llvm::StringRef filename, llvm::StringRef function,
108  int lineNumber);
109  static void SetGlobalWPIError(llvm::StringRef errorMessage,
110  llvm::StringRef contextMessage,
111  llvm::StringRef filename,
112  llvm::StringRef function, int lineNumber);
113  static Error& GetGlobalError();
114 
115  protected:
116  mutable Error m_error;
117  // TODO: Replace globalError with a global list of all errors.
118  static std::mutex _globalErrorMutex;
119  static Error _globalError;
120 };
121 
122 } // namespace frc
Definition: Timer.cpp:18
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:204
virtual void SetErrorRange(Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:145
virtual void SetError(Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:116
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
virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:179
virtual void SetErrnoError(llvm::StringRef 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 void SetImaqError(int success, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const
Set the current error information associated from the nivision Imaq API.
Definition: ErrorBase.cpp:87
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:235