WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
ErrorBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2016. 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 
13 #include "HAL/cpp/priority_mutex.h"
14 #include "llvm/StringRef.h"
15 
16 #define wpi_setErrnoErrorWithContext(context) \
17  this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__)
18 #define wpi_setErrnoError() wpi_setErrnoErrorWithContext("")
19 #define wpi_setImaqErrorWithContext(code, context) \
20  do { \
21  if ((code) != 0) \
22  this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
23  } while (0)
24 #define wpi_setErrorWithContext(code, context) \
25  do { \
26  if ((code) != 0) \
27  this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
28  } while (0)
29 #define wpi_setError(code) wpi_setErrorWithContext(code, "")
30 #define wpi_setStaticErrorWithContext(object, code, context) \
31  do { \
32  if ((code) != 0) \
33  object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
34  } while (0)
35 #define wpi_setStaticError(object, code) \
36  wpi_setStaticErrorWithContext(object, code, "")
37 #define wpi_setGlobalErrorWithContext(code, context) \
38  do { \
39  if ((code) != 0) \
40  ErrorBase::SetGlobalError((code), (context), __FILE__, __FUNCTION__, \
41  __LINE__); \
42  } while (0)
43 #define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "")
44 #define wpi_setWPIErrorWithContext(error, context) \
45  this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \
46  (context), __FILE__, __FUNCTION__, __LINE__)
47 #define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, ""))
48 #define wpi_setStaticWPIErrorWithContext(object, error, context) \
49  object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \
50  __FUNCTION__, __LINE__)
51 #define wpi_setStaticWPIError(object, error) \
52  wpi_setStaticWPIErrorWithContext(object, error, "")
53 #define wpi_setGlobalWPIErrorWithContext(error, context) \
54  ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), __FILE__, \
55  __FUNCTION__, __LINE__)
56 #define wpi_setGlobalWPIError(error) \
57  wpi_setGlobalWPIErrorWithContext(error, "")
58 
66 class ErrorBase {
67  // TODO: Consider initializing instance variables and cleanup in destructor
68  public:
69  ErrorBase() = default;
70  virtual ~ErrorBase() = default;
71 
72  ErrorBase(const ErrorBase&) = delete;
73  ErrorBase& operator=(const ErrorBase&) = delete;
74 
75  virtual Error& GetError();
76  virtual const Error& GetError() const;
77  virtual void SetErrnoError(llvm::StringRef contextMessage,
78  llvm::StringRef filename, llvm::StringRef function,
79  uint32_t lineNumber) const;
80  virtual void SetImaqError(int success, llvm::StringRef contextMessage,
81  llvm::StringRef filename, llvm::StringRef function,
82  uint32_t lineNumber) const;
83  virtual void SetError(Error::Code code, llvm::StringRef contextMessage,
84  llvm::StringRef filename, llvm::StringRef function,
85  uint32_t lineNumber) const;
86  virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code,
87  llvm::StringRef contextMessage,
88  llvm::StringRef filename, llvm::StringRef function,
89  uint32_t lineNumber) const;
90  virtual void CloneError(const ErrorBase& rhs) const;
91  virtual void ClearError() const;
92  virtual bool StatusIsFatal() const;
93  static void SetGlobalError(Error::Code code, llvm::StringRef contextMessage,
94  llvm::StringRef filename, llvm::StringRef function,
95  uint32_t lineNumber);
96  static void SetGlobalWPIError(llvm::StringRef errorMessage,
97  llvm::StringRef contextMessage,
98  llvm::StringRef filename,
99  llvm::StringRef function, uint32_t lineNumber);
100  static Error& GetGlobalError();
101 
102  protected:
103  mutable Error m_error;
104  // TODO: Replace globalError with a global list of all errors.
105  static priority_mutex _globalErrorMutex;
106  static Error _globalError;
107 };
Error object represents a library error.
Definition: Error.h:28
virtual void SetError(Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:106
virtual void ClearError() const
Clear the current error information associated with this sensor.
Definition: ErrorBase.cpp:31
virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:131
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:156
Base class for most objects.
Definition: ErrorBase.h:66
Definition: priority_mutex.h:53
virtual Error & GetError()
Retrieve the current error.
Definition: ErrorBase.cpp:24
virtual void SetErrnoError(llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
Set error information associated with a C library call that set an error to the "errno" global variab...
Definition: ErrorBase.cpp:42
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39
virtual void SetImaqError(int success, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
Set the current error information associated from the nivision Imaq API.
Definition: ErrorBase.cpp:78
static Error & GetGlobalError()
Retrieve the current global error.
Definition: ErrorBase.cpp:188