core: Use std::optional to return an optional value

This commit is contained in:
Stefano Avallone
2022-05-14 08:45:04 -07:00
committed by Tom Henderson
parent cc68be51b3
commit ca59097075
3 changed files with 29 additions and 52 deletions

View File

@@ -32,7 +32,6 @@
#include <ratio>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <vector>
@@ -235,24 +234,19 @@ NS_LOG_COMPONENT_DEFINE ("Length");
// Implement the attribute helper
ATTRIBUTE_HELPER_CPP (Length);
std::tuple<bool, Length>
std::optional<Length>
Length::TryParse (double value, const std::string& unitString)
{
NS_LOG_FUNCTION (value << unitString);
bool validUnit = false;
Length::Unit unit;
auto unit = FromString (unitString);
std::tie (validUnit, unit) = FromString (unitString);
Length length;
if (validUnit)
if (unit.has_value ())
{
length = Length (value, unit);
return Length (value, *unit);
}
return std::make_tuple (validUnit, length);
return std::nullopt;
}
Length::Length ()
@@ -276,19 +270,16 @@ Length::Length (double value, const std::string& unitString)
{
NS_LOG_FUNCTION (this << value << unitString);
bool validUnit;
Length::Unit unit;
auto unit = FromString (unitString);
std::tie (validUnit, unit) = FromString (unitString);
if (!validUnit)
if (!unit.has_value ())
{
NS_FATAL_ERROR ("A Length object could not be constructed from the unit "
"string '" << unitString << "', because the string is not associated "
"with a Length::Unit entry");
}
m_value = Convert (value, unit, Length::Unit::Meter);
m_value = Convert (value, *unit, Length::Unit::Meter);
}
Length::Length (double value, Length::Unit unit)
@@ -577,7 +568,7 @@ ToName (Length::Unit unit, bool plural /*=false*/)
return std::get<0> (iter->second);
}
std::tuple<bool, Length::Unit>
std::optional<Length::Unit>
FromString (std::string unitString)
{
using UnitTable = std::unordered_map<std::string, Length::Unit>;
@@ -654,16 +645,12 @@ FromString (std::string unitString)
auto iter = UNITS.find (unitString);
bool valid = false;
Length::Unit unit;
if (iter != UNITS.end ())
{
valid = true;
unit = iter->second;
return iter->second;
}
return std::make_tuple (valid, unit);
return std::nullopt;
}
std::ostream&

View File

@@ -31,9 +31,9 @@
#include <istream>
#include <limits>
#include <optional>
#include <ostream>
#include <string>
#include <tuple>
/**
* \file
@@ -356,12 +356,11 @@ private:
* \param value Numeric value of the new length
* \param unit Unit that the value represents
*
* \return A tuple containing the success or failure of the parsing and a Length
* object. If the boolean element is true, then the Length element contains the
* parsed result. If the boolean element is false, the value of the Length
* element is undefined.
* \return A std::optional object containing the Length object constructed from
* the given value and unit, if the attempt to construct the Length object was
* successful.
*/
static std::tuple<bool, Length> TryParse (double value, const std::string& unit);
static std::optional<Length> TryParse (double value, const std::string& unit);
/**
* Default Constructor
@@ -667,12 +666,10 @@ std::string ToName (Length::Unit unit, bool plural = false);
*
* \param unitString String containing the symbol or name of a length unit
*
* \return A tuple containing a boolean and a Length::Unit. When the boolean
* is true, the Length::Unit contains a valid value. When the boolean is false,
* a match for the string could not be found and the Length::Unit value is
* undefined
* \return A std::optional object containing a Length::Unit if a match for the
* string could be found
*/
std::tuple<bool, Length::Unit> FromString (std::string unitString);
std::optional<Length::Unit> FromString (std::string unitString);
/**
* \ingroup length

View File

@@ -39,7 +39,6 @@
#include <map>
#include <sstream>
#include <string>
#include <tuple>
/**
* \file
@@ -651,12 +650,9 @@ LengthTestCase::TestBuilderFreeFunctions ()
void
LengthTestCase::TestTryParseReturnsFalse ()
{
bool result;
Length l;
auto l = Length::TryParse (1, "");
std::tie (result, l) = Length::TryParse (1, "");
AssertFalse (result, "TryParse returned true on bad input");
AssertFalse (l.has_value (), "TryParse returned true on bad input");
}
void
@@ -676,18 +672,15 @@ LengthTestCase::TestTryParseReturnsTrue ()
TestInput input = entry.first;
TestArgs args = entry.second;
bool result;
Length l;
auto l = Length::TryParse (input.first, input.second);
std::tie (result, l) = Length::TryParse (input.first, input.second);
AssertTrue (result, "TryParse returned false when expecting true");
AssertTrue (l.has_value (), "TryParse returned false when expecting true");
std::stringstream stream;
stream << "Parsing input (" << input.first << ", " << input.second
<< ") returned the wrong value";
NS_TEST_ASSERT_MSG_EQ_TOL (l.GetDouble (), args.first, args.second, stream.str ());
NS_TEST_ASSERT_MSG_EQ_TOL (l->GetDouble (), args.first, args.second, stream.str ());
}
}