regulators: Use std::optional in ActionEnvironment
Use the std::optional data type in the ActionEnvironment class to
represent an optional volts value setting.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Ib66f7586a49d78d8e0241fb8e7e4ecd881d1ac01
diff --git a/phosphor-regulators/src/actions/action_environment.hpp b/phosphor-regulators/src/actions/action_environment.hpp
index ff6259a..f94345a 100644
--- a/phosphor-regulators/src/actions/action_environment.hpp
+++ b/phosphor-regulators/src/actions/action_environment.hpp
@@ -18,6 +18,7 @@
#include "id_map.hpp"
#include <cstddef> // for size_t
+#include <optional>
#include <stdexcept>
#include <string>
@@ -130,34 +131,16 @@
}
/**
- * Returns the current volts value.
- *
- * Call hasVolts() first to check whether a volts value has been set.
- *
- * Throws logic_error if no volts value has been set.
+ * Returns the current volts value, if set.
*
* @return current volts value
*/
- double getVolts() const
+ std::optional<double> getVolts() const
{
- if (!hasVoltsValue)
- {
- throw std::logic_error{"No volts value has been set."};
- }
return volts;
}
/**
- * Returns whether a volts value has been set.
- *
- * @return true if a volts value has been set, false otherwise
- */
- bool hasVolts() const
- {
- return hasVoltsValue;
- }
-
- /**
* Increments the rule call stack depth by one.
*
* Should be used when a rule is called.
@@ -196,7 +179,6 @@
void setVolts(double volts)
{
this->volts = volts;
- hasVoltsValue = true;
}
private:
@@ -211,14 +193,9 @@
std::string deviceID{};
/**
- * Indicates whether a volts value has been set.
- */
- bool hasVoltsValue{false};
-
- /**
* Current volts value (if set).
*/
- double volts{0};
+ std::optional<double> volts{};
/**
* Rule call stack depth.
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
index ae5ac4d..238e2f2 100644
--- a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
@@ -131,10 +131,10 @@
// A volts value is defined for this action
voltsValue = volts.value();
}
- else if (environment.hasVolts())
+ else if (environment.getVolts().has_value())
{
// A volts value is defined in the ActionEnvironment
- voltsValue = environment.getVolts();
+ voltsValue = environment.getVolts().value();
}
else
{