Improve unexpected (#1323)

* use type instead of value

* fix remaining initializer-list users

* impruv

* last consumer?

* consumer

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-04-19 05:37:43 -07:00 committed by GitHub
parent 7f60384c75
commit 0d699bcfe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 14 deletions

View File

@ -19,7 +19,11 @@ SensorResult LinearFunc::convert(float inputValue) const {
// Bounds check // Bounds check
bool isValid = result <= m_maxOutput && result >= m_minOutput; bool isValid = result <= m_maxOutput && result >= m_minOutput;
return {isValid, result}; if (!isValid) {
return unexpected;
}
return result;
} }
void LinearFunc::showInfo(Logging* logger, float testRawValue) const { void LinearFunc::showInfo(Logging* logger, float testRawValue) const {

View File

@ -26,7 +26,11 @@ public:
// check for NaN // check for NaN
bool valid = !(result != result); bool valid = !(result != result);
return {valid, result}; if (!valid) {
return unexpected;
}
return result;
} }
void showInfo(Logging* logger, const char* sensorName) const override {} void showInfo(Logging* logger, const char* sensorName) const override {}

View File

@ -26,17 +26,16 @@ SensorResult RedundantSensor::get() const {
// If either result is invalid, return invalid. // If either result is invalid, return invalid.
if (!result1.Valid || !result2.Valid) { if (!result1.Valid || !result2.Valid) {
return { false, 0 }; return unexpected;
} }
// If both are valid, check that they're near one another // If both are valid, check that they're near one another
float delta = absF(result1.Value - result2.Value); float delta = absF(result1.Value - result2.Value);
if (delta > m_maxDifference) { if (delta > m_maxDifference) {
return { false, 0 }; return unexpected;
} }
// Both sensors are valid, and their readings are close. All is well. // Both sensors are valid, and their readings are close. All is well.
// Return the average // Return the average
float avg = (result1.Value + result2.Value) / 2; return (result1.Value + result2.Value) / 2;
return { true, avg };
} }

View File

@ -87,7 +87,7 @@ bool Sensor::Register() {
// Check if this is a valid sensor entry // Check if this is a valid sensor entry
if (!entry) { if (!entry) {
return {false, 0.0f}; return unexpected;
} }
// Next check for mock // Next check for mock

View File

@ -34,11 +34,11 @@ public:
float value = m_value; float value = m_value;
if (!valid) { if (!valid) {
return {false, value}; return unexpected;
} }
if (getTimeNowNt() - m_timeoutPeriod > m_lastUpdate) { if (getTimeNowNt() - m_timeoutPeriod > m_lastUpdate) {
return {false, value}; return unexpected;
} }
return value; return value;

View File

@ -15,12 +15,15 @@
#pragma once #pragma once
struct unexpected_t {};
template <class TValue> template <class TValue>
struct expected { struct expected {
const bool Valid; const bool Valid;
const TValue Value; const TValue Value;
constexpr expected(bool valid, TValue value) : Valid(valid), Value(value) {} // Implicit constructor to construct in the invalid state
constexpr expected(const unexpected_t&) : Valid(false), Value{} {}
// Implicit constructor to convert from TValue (for valid values, so an expected<T> behaves like a T) // Implicit constructor to convert from TValue (for valid values, so an expected<T> behaves like a T)
constexpr expected(TValue validValue) constexpr expected(TValue validValue)
@ -40,4 +43,4 @@ struct expected {
} }
}; };
constexpr expected<float> unexpected = {false, 0.0f}; constexpr unexpected_t unexpected{};

View File

@ -5,10 +5,11 @@
struct DoublerFunc final : public SensorConverter { struct DoublerFunc final : public SensorConverter {
SensorResult convert(float input) const { SensorResult convert(float input) const {
bool valid = input > 0; if (input <= 0) {
float value = input * 2; return unexpected;
}
return {valid, value}; return input * 2;
} }
}; };