blob: 756442dbe5fc61d2ff19d0c1d86b052178f7a199 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#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
59/**
60 * Base class for most objects.
61 * ErrorBase is the base class for most objects since it holds the generated
62 * error
63 * for that object. In addition, there is a single instance of a global error
64 * object
65 */
66class 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};