From ca590970755c37a98118386ef40f93b847f2ab57 Mon Sep 17 00:00:00 2001 From: Stefano Avallone Date: Sat, 14 May 2022 08:45:04 -0700 Subject: [PATCH] core: Use std::optional to return an optional value --- src/core/model/length.cc | 35 ++++++++++-------------------- src/core/model/length.h | 27 ++++++++++------------- src/core/test/length-test-suite.cc | 19 +++++----------- 3 files changed, 29 insertions(+), 52 deletions(-) diff --git a/src/core/model/length.cc b/src/core/model/length.cc index 0174c39e3..5cf77b918 100644 --- a/src/core/model/length.cc +++ b/src/core/model/length.cc @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -235,24 +234,19 @@ NS_LOG_COMPONENT_DEFINE ("Length"); // Implement the attribute helper ATTRIBUTE_HELPER_CPP (Length); -std::tuple +std::optional 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 +std::optional FromString (std::string unitString) { using UnitTable = std::unordered_map; @@ -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& diff --git a/src/core/model/length.h b/src/core/model/length.h index 4534866c9..dc8025c16 100644 --- a/src/core/model/length.h +++ b/src/core/model/length.h @@ -31,9 +31,9 @@ #include #include +#include #include #include -#include /** * \file @@ -161,7 +161,7 @@ namespace ns3 { * Addition is between two Length instances * * \code - * std::cout << Length(1, Length::Unit::Meter) + Length (2, Length::Unit::Meter); + * std::cout << Length(1, Length::Unit::Meter) + Length (2, Length::Unit::Meter); * // output: "3 m" * \endcode * @@ -170,7 +170,7 @@ namespace ns3 { * Subtraction is between two Length instances * * \code - * std::cout << Length(3, Length::Unit::Meter) - Length (2, Length::Unit::Meter); + * std::cout << Length(3, Length::Unit::Meter) - Length (2, Length::Unit::Meter); * // output: "1 m" * \endcode * @@ -234,8 +234,8 @@ namespace ns3 { * \code * Length m(5, Length::Unit::Meter); * - * std::cout << m << ", " - * << m.As(Length::Unit::Kilometer) << ", " + * std::cout << m << ", " + * << m.As(Length::Unit::Kilometer) << ", " * << m.As(Length::Unit::Foot); * //output: 5 m, 0.005 km, 16.4042 ft * \endcode @@ -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 TryParse (double value, const std::string& unit); + static std::optional 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 FromString (std::string unitString); +std::optional FromString (std::string unitString); /** * \ingroup length diff --git a/src/core/test/length-test-suite.cc b/src/core/test/length-test-suite.cc index fe950905b..92de92037 100644 --- a/src/core/test/length-test-suite.cc +++ b/src/core/test/length-test-suite.cc @@ -39,7 +39,6 @@ #include #include #include -#include /** * \file @@ -443,7 +442,7 @@ LengthTestCase::TestConstructLengthFromMeterString () { const double value = 5; - TestConstructLengthFromString (value, value, 0, + TestConstructLengthFromString (value, value, 0, {"m", "meter", "meters", "metre", "metres"}); } @@ -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 ()); } }