From 0d699bcfe9acd95fd6f68038de27238629bcadb4 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 19 Apr 2020 05:37:43 -0700 Subject: [PATCH] Improve unexpected (#1323) * use type instead of value * fix remaining initializer-list users * impruv * last consumer? * consumer Co-authored-by: Matthew Kennedy --- firmware/controllers/sensors/converters/linear_func.cpp | 6 +++++- firmware/controllers/sensors/function_pointer_sensor.h | 6 +++++- firmware/controllers/sensors/redundant_sensor.cpp | 7 +++---- firmware/controllers/sensors/sensor.cpp | 2 +- firmware/controllers/sensors/stored_value_sensor.h | 4 ++-- firmware/util/expected.h | 7 +++++-- unit_tests/tests/sensor/func_sensor.cpp | 7 ++++--- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/firmware/controllers/sensors/converters/linear_func.cpp b/firmware/controllers/sensors/converters/linear_func.cpp index 0f99583439..f54531a6f3 100644 --- a/firmware/controllers/sensors/converters/linear_func.cpp +++ b/firmware/controllers/sensors/converters/linear_func.cpp @@ -19,7 +19,11 @@ SensorResult LinearFunc::convert(float inputValue) const { // Bounds check 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 { diff --git a/firmware/controllers/sensors/function_pointer_sensor.h b/firmware/controllers/sensors/function_pointer_sensor.h index 58e29f5b29..98243f1773 100644 --- a/firmware/controllers/sensors/function_pointer_sensor.h +++ b/firmware/controllers/sensors/function_pointer_sensor.h @@ -26,7 +26,11 @@ public: // check for NaN bool valid = !(result != result); - return {valid, result}; + if (!valid) { + return unexpected; + } + + return result; } void showInfo(Logging* logger, const char* sensorName) const override {} diff --git a/firmware/controllers/sensors/redundant_sensor.cpp b/firmware/controllers/sensors/redundant_sensor.cpp index 16be61d751..e4412b66e6 100644 --- a/firmware/controllers/sensors/redundant_sensor.cpp +++ b/firmware/controllers/sensors/redundant_sensor.cpp @@ -26,17 +26,16 @@ SensorResult RedundantSensor::get() const { // If either result is invalid, return invalid. if (!result1.Valid || !result2.Valid) { - return { false, 0 }; + return unexpected; } // If both are valid, check that they're near one another float delta = absF(result1.Value - result2.Value); if (delta > m_maxDifference) { - return { false, 0 }; + return unexpected; } // Both sensors are valid, and their readings are close. All is well. // Return the average - float avg = (result1.Value + result2.Value) / 2; - return { true, avg }; + return (result1.Value + result2.Value) / 2; } diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index 33796d950a..c0127c5305 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -87,7 +87,7 @@ bool Sensor::Register() { // Check if this is a valid sensor entry if (!entry) { - return {false, 0.0f}; + return unexpected; } // Next check for mock diff --git a/firmware/controllers/sensors/stored_value_sensor.h b/firmware/controllers/sensors/stored_value_sensor.h index 5e101c8d62..c5b89f00d2 100644 --- a/firmware/controllers/sensors/stored_value_sensor.h +++ b/firmware/controllers/sensors/stored_value_sensor.h @@ -34,11 +34,11 @@ public: float value = m_value; if (!valid) { - return {false, value}; + return unexpected; } if (getTimeNowNt() - m_timeoutPeriod > m_lastUpdate) { - return {false, value}; + return unexpected; } return value; diff --git a/firmware/util/expected.h b/firmware/util/expected.h index 66a0230eff..e444e9c58d 100644 --- a/firmware/util/expected.h +++ b/firmware/util/expected.h @@ -15,12 +15,15 @@ #pragma once +struct unexpected_t {}; + template struct expected { const bool Valid; 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 behaves like a T) constexpr expected(TValue validValue) @@ -40,4 +43,4 @@ struct expected { } }; -constexpr expected unexpected = {false, 0.0f}; +constexpr unexpected_t unexpected{}; diff --git a/unit_tests/tests/sensor/func_sensor.cpp b/unit_tests/tests/sensor/func_sensor.cpp index 81f2cdd947..b5551dd403 100644 --- a/unit_tests/tests/sensor/func_sensor.cpp +++ b/unit_tests/tests/sensor/func_sensor.cpp @@ -5,10 +5,11 @@ struct DoublerFunc final : public SensorConverter { SensorResult convert(float input) const { - bool valid = input > 0; - float value = input * 2; + if (input <= 0) { + return unexpected; + } - return {valid, value}; + return input * 2; } };