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
{
diff --git a/phosphor-regulators/test/actions/action_environment_tests.cpp b/phosphor-regulators/test/actions/action_environment_tests.cpp
index 5561705..efed4d3 100644
--- a/phosphor-regulators/test/actions/action_environment_tests.cpp
+++ b/phosphor-regulators/test/actions/action_environment_tests.cpp
@@ -50,8 +50,7 @@
EXPECT_EQ(env.getDevice().getID(), "regulator1");
EXPECT_EQ(env.getDeviceID(), "regulator1");
EXPECT_EQ(env.getRuleDepth(), 0);
- EXPECT_THROW(env.getVolts(), std::logic_error);
- EXPECT_EQ(env.hasVolts(), false);
+ EXPECT_EQ(env.getVolts().has_value(), false);
}
catch (const std::exception& error)
{
@@ -186,42 +185,10 @@
{
IDMap idMap{};
ActionEnvironment env{idMap, ""};
- EXPECT_EQ(env.hasVolts(), false);
-
- // Test where a volts value has not been set
- try
- {
- env.getVolts();
- }
- catch (const std::logic_error& l_error)
- {
- EXPECT_STREQ(l_error.what(), "No volts value has been set.");
- }
- catch (const std::exception& error)
- {
- ADD_FAILURE() << "Should not have caught exception.";
- }
-
- // Test where a volts value has been set
+ EXPECT_EQ(env.getVolts().has_value(), false);
env.setVolts(1.31);
- try
- {
- double volts = env.getVolts();
- EXPECT_EQ(volts, 1.31);
- }
- catch (const std::exception& error)
- {
- ADD_FAILURE() << "Should not have caught exception.";
- }
-}
-
-TEST(ActionEnvironmentTests, HasVolts)
-{
- IDMap idMap{};
- ActionEnvironment env{idMap, ""};
- EXPECT_EQ(env.hasVolts(), false);
- env.setVolts(1.31);
- EXPECT_EQ(env.hasVolts(), true);
+ EXPECT_EQ(env.getVolts().has_value(), true);
+ EXPECT_EQ(env.getVolts().value(), 1.31);
}
TEST(ActionEnvironmentTests, IncrementRuleDepth)
@@ -275,10 +242,10 @@
{
IDMap idMap{};
ActionEnvironment env{idMap, ""};
- EXPECT_EQ(env.hasVolts(), false);
+ EXPECT_EQ(env.getVolts().has_value(), false);
env.setVolts(2.35);
- EXPECT_EQ(env.hasVolts(), true);
- EXPECT_EQ(env.getVolts(), 2.35);
+ EXPECT_EQ(env.getVolts().has_value(), true);
+ EXPECT_EQ(env.getVolts().value(), 2.35);
}
catch (const std::exception& error)
{