ErrorBase and WPIError improvements (#1727)

* ErrorBase: Use magic static singleton for globals
* ErrorBase: Add testability features for global errors
* Make WPIError definitions inline functions
(This works around cross-DLL variable issues on Windows)

Fixes #1726.
This commit is contained in:
Peter Johnson
2019-06-23 20:36:52 -07:00
committed by GitHub
parent 372ca4f456
commit 258bba0c2d
3 changed files with 136 additions and 96 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -7,6 +7,8 @@
#pragma once
#include <vector>
#include <wpi/StringRef.h>
#include <wpi/Twine.h>
#include <wpi/mutex.h>
@@ -49,16 +51,16 @@
} while (0)
#define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "")
#define wpi_setWPIErrorWithContext(error, context) \
this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \
this->SetWPIError(wpi_error_s_##error(), wpi_error_value_##error(), \
(context), __FILE__, __FUNCTION__, __LINE__)
#define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, ""))
#define wpi_setStaticWPIErrorWithContext(object, error, context) \
object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \
object->SetWPIError(wpi_error_s_##error(), (context), __FILE__, \
__FUNCTION__, __LINE__)
#define wpi_setStaticWPIError(object, error) \
wpi_setStaticWPIErrorWithContext(object, error, "")
#define wpi_setGlobalWPIErrorWithContext(error, context) \
::frc::ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), \
::frc::ErrorBase::SetGlobalWPIError(wpi_error_s_##error(), (context), \
__FILE__, __FUNCTION__, __LINE__)
#define wpi_setGlobalWPIError(error) wpi_setGlobalWPIErrorWithContext(error, "")
@@ -191,9 +193,19 @@ class ErrorBase {
wpi::StringRef function, int lineNumber);
/**
* Retrieve the current global error.
* Retrieve the last global error.
*/
static const Error& GetGlobalError();
static Error GetGlobalError();
/**
* Retrieve all global errors.
*/
static std::vector<Error> GetGlobalErrors();
/**
* Clear global errors.
*/
void ClearGlobalErrors();
protected:
mutable Error m_error;