WPILibC++  2019.1.1-beta-1-15-gd03b020
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 <wpi/StringRef.h>
11 #include <wpi/Twine.h>
12 #include <wpi/mutex.h>
13 
14 #include "frc/Base.h"
15 #include "frc/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(ErrorBase&&) = default;
81  ErrorBase& operator=(ErrorBase&&) = default;
82 
88  virtual Error& GetError();
89 
95  virtual const Error& GetError() const;
96 
100  virtual void ClearError() const;
101 
111  virtual void SetErrnoError(const wpi::Twine& contextMessage,
112  wpi::StringRef filename, wpi::StringRef function,
113  int lineNumber) const;
114 
125  virtual void SetImaqError(int success, const wpi::Twine& contextMessage,
126  wpi::StringRef filename, wpi::StringRef function,
127  int lineNumber) const;
128 
138  virtual void SetError(Error::Code code, const wpi::Twine& contextMessage,
139  wpi::StringRef filename, wpi::StringRef function,
140  int lineNumber) const;
141 
155  virtual void SetErrorRange(Error::Code code, int32_t minRange,
156  int32_t maxRange, int32_t requestedValue,
157  const wpi::Twine& contextMessage,
158  wpi::StringRef filename, wpi::StringRef function,
159  int lineNumber) const;
160 
170  virtual void SetWPIError(const wpi::Twine& errorMessage, Error::Code code,
171  const wpi::Twine& contextMessage,
172  wpi::StringRef filename, wpi::StringRef function,
173  int lineNumber) const;
174 
175  virtual void CloneError(const ErrorBase& rhs) const;
176 
182  virtual bool StatusIsFatal() const;
183 
184  static void SetGlobalError(Error::Code code, const wpi::Twine& contextMessage,
185  wpi::StringRef filename, wpi::StringRef function,
186  int lineNumber);
187 
188  static void SetGlobalWPIError(const wpi::Twine& errorMessage,
189  const wpi::Twine& contextMessage,
190  wpi::StringRef filename,
191  wpi::StringRef function, int lineNumber);
192 
196  static const Error& GetGlobalError();
197 
198  protected:
199  mutable Error m_error;
200 };
201 
202 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
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.
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...
virtual void ClearError() const
Clear the current error information associated with this sensor.
virtual Error & GetError()
Retrieve the current error.
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.
Base class for most objects.
Definition: ErrorBase.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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.
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.
static const Error & GetGlobalError()
Retrieve the current global error.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Error object represents a library error.
Definition: Error.h:31