Skip to content

Commit

Permalink
Migrate to std::optional for boost::regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Jan 17, 2025
1 parent dc878b2 commit 1312e4e
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ void LookupTableValidator::sortInPlaceByThetaThenTitleMatcher(LookupTableRows &l
if (*lhs.thetaOrWildcard() != *rhs.thetaOrWildcard()) {
return *lhs.thetaOrWildcard() < *rhs.thetaOrWildcard();
}
auto const lhsTitle = lhs.titleMatcher().get_value_or(boost::regex());
auto const rhsTitle = rhs.titleMatcher().get_value_or(boost::regex());
auto const lhsTitle = lhs.titleMatcher().value_or(boost::regex());
auto const rhsTitle = rhs.titleMatcher().value_or(boost::regex());
return lhsTitle < rhsTitle;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Experiment::Experiment()
m_polarizationCorrections(PolarizationCorrections(PolarizationCorrectionType::None)),
m_floodCorrections(FloodCorrections(FloodCorrectionType::Workspace)), m_transmissionStitchOptions(),
m_stitchParameters(std::map<std::string, std::string>()),
m_lookupTable(LookupTable({LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none, RangeInQ(),
m_lookupTable(LookupTable({LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none, RangeInQ(),
boost::none, ProcessingInstructions(), boost::none, boost::none)})) {}

Experiment::Experiment(AnalysisMode analysisMode, ReductionType reductionType, SummationType summationType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ constexpr double EPSILON = std::numeric_limits<double>::epsilon();

namespace MantidQt::CustomInterfaces::ISISReflectometry {

LookupRow::LookupRow(boost::optional<double> theta, boost::optional<boost::regex> titleMatcher,
LookupRow::LookupRow(boost::optional<double> theta, std::optional<boost::regex> titleMatcher,
TransmissionRunPair transmissionRuns,
boost::optional<ProcessingInstructions> transmissionProcessingInstructions, RangeInQ qRange,
boost::optional<double> scaleFactor,
Expand All @@ -30,11 +30,11 @@ LookupRow::LookupRow(boost::optional<double> theta, boost::optional<boost::regex

TransmissionRunPair const &LookupRow::transmissionWorkspaceNames() const { return m_transmissionRuns; }

bool LookupRow::isWildcard() const { return !m_theta.is_initialized() && !m_titleMatcher.is_initialized(); }
bool LookupRow::isWildcard() const { return !m_theta.is_initialized() && !m_titleMatcher.has_value(); }

boost::optional<double> LookupRow::thetaOrWildcard() const { return m_theta; }

boost::optional<boost::regex> LookupRow::titleMatcher() const { return m_titleMatcher; }
std::optional<boost::regex> LookupRow::titleMatcher() const { return m_titleMatcher; }

RangeInQ const &LookupRow::qRange() const { return m_qRange; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL LookupRow {
ROI_DETECTOR_IDS = 11
};

LookupRow(boost::optional<double> theta, boost::optional<boost::regex> titleMatcher,
LookupRow(boost::optional<double> theta, std::optional<boost::regex> titleMatcher,
TransmissionRunPair tranmissionRuns,
boost::optional<ProcessingInstructions> transmissionProcessingInstructions, RangeInQ qRange,
boost::optional<double> scaleFactor, boost::optional<ProcessingInstructions> processingInstructions,
Expand All @@ -57,7 +57,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL LookupRow {
TransmissionRunPair const &transmissionWorkspaceNames() const;
bool isWildcard() const;
boost::optional<double> thetaOrWildcard() const;
boost::optional<boost::regex> titleMatcher() const;
std::optional<boost::regex> titleMatcher() const;
RangeInQ const &qRange() const;
boost::optional<double> scaleFactor() const;
boost::optional<ProcessingInstructions> transmissionProcessingInstructions() const;
Expand All @@ -73,7 +73,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL LookupRow {

private:
boost::optional<double> m_theta;
boost::optional<boost::regex> m_titleMatcher;
std::optional<boost::regex> m_titleMatcher;
TransmissionRunPair m_transmissionRuns;
RangeInQ m_qRange;
boost::optional<double> m_scaleFactor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ std::vector<LookupRow> LookupTable::findMatchingRegexes(std::string const &title
auto results = std::vector<LookupRow>();
std::copy_if(m_lookupRows.cbegin(), m_lookupRows.cend(), std::back_inserter(results),
[&title](auto const &candidate) {
return candidate.titleMatcher() && boost::regex_search(title, candidate.titleMatcher().get());
return candidate.titleMatcher() && boost::regex_search(title, candidate.titleMatcher().value());
});
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ boost::optional<double> parseTheta(std::string const &theta) {
return boost::none;
}

boost::optional<boost::regex> parseTitleMatcher(std::string const &titleMatcher) {
std::optional<boost::regex> parseTitleMatcher(std::string const &titleMatcher) {
if (isEntirelyWhitespace(titleMatcher)) {
return boost::none;
return std::nullopt;
}
try {
return boost::regex(titleMatcher);
} catch (boost::regex_error const &) {
return boost::none;
return std::nullopt;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parseRunNumberOrWhitespace(std::string const &runNumberString);

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<double> parseTheta(std::string const &theta);

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<boost::regex> parseTitleMatcher(std::string const &titleMatcher);
MANTIDQT_ISISREFLECTOMETRY_DLL std::optional<boost::regex> parseTitleMatcher(std::string const &titleMatcher);

MANTIDQT_ISISREFLECTOMETRY_DLL
boost::variant<TransmissionRunPair, std::vector<int>> parseTransmissionRuns(std::string const &firstTransmissionRun,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ ValidatorT<boost::optional<double>> LookupRowValidator::parseThetaOrWhitespace(C
return boost::none;
}

ValidatorT<boost::optional<boost::regex>> LookupRowValidator::parseTitleMatcherOrWhitespace(CellText const &cellText) {
ValidatorT<std::optional<boost::regex>> LookupRowValidator::parseTitleMatcherOrWhitespace(CellText const &cellText) {
auto const &text = cellText[LookupRow::Column::TITLE];
if (isEntirelyWhitespace(text)) {
// Mark validator as passed, but the enclosed value empty
return boost::optional<boost::regex>(boost::none);
return boost::make_optional<std::optional<boost::regex>>(std::nullopt);
}

// This check relies on us checking for whitespace chars before calling parseTitleMatcher
Expand Down Expand Up @@ -131,7 +131,7 @@ void LookupRowValidator::validateThetaAndRegex() {
return;

// Check we have a theta value, when we have a titleMatcher
if (m_titleMatcherOrInvalid.get().is_initialized() && !m_thetaOrInvalid.get().is_initialized()) {
if (m_titleMatcherOrInvalid.get().has_value() && !m_thetaOrInvalid.get().is_initialized()) {
m_invalidColumns.insert(LookupRow::Column::THETA);
m_invalidColumns.insert(LookupRow::Column::TITLE);
m_thetaOrInvalid = boost::none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL LookupRowValidator {

private:
ValidatorT<boost::optional<double>> parseThetaOrWhitespace(LookupRow::ValueArray const &cellText);
ValidatorT<boost::optional<boost::regex>> parseTitleMatcherOrWhitespace(LookupRow::ValueArray const &cellText);
ValidatorT<std::optional<boost::regex>> parseTitleMatcherOrWhitespace(LookupRow::ValueArray const &cellText);
ValidatorT<TransmissionRunPair> parseTransmissionRuns(LookupRow::ValueArray const &cellText);
ValidatorT<boost::optional<std::string>>
parseTransmissionProcessingInstructions(LookupRow::ValueArray const &cellText);
Expand All @@ -46,7 +46,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL LookupRowValidator {

std::unordered_set<int> m_invalidColumns;
ValidatorT<boost::optional<double>> m_thetaOrInvalid;
ValidatorT<boost::optional<boost::regex>> m_titleMatcherOrInvalid;
ValidatorT<std::optional<boost::regex>> m_titleMatcherOrInvalid;
};

ValidationResult<LookupRow, std::unordered_set<int>> validateLookupRow(LookupRow::ValueArray const &cellText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,27 +345,27 @@ ReductionJobs oneGroupWithTwoRowsWithOutputNamesModel() {

/* Experiment */

LookupRow makeLookupRow(boost::optional<double> angle, boost::optional<boost::regex> titleMatcher) {
LookupRow makeLookupRow(boost::optional<double> angle, std::optional<boost::regex> titleMatcher) {
return LookupRow(
std::move(angle), std::move(titleMatcher),
TransmissionRunPair(std::vector<std::string>{"22348", "22349"}, std::vector<std::string>{"22358", "22359"}),
ProcessingInstructions("4"), RangeInQ(0.009, 0.03, 1.3), 0.9, ProcessingInstructions("4-6"),
ProcessingInstructions("2-3,7-8"), ProcessingInstructions("3-22"));
}

LookupRow makeWildcardLookupRow() { return makeLookupRow(boost::none, boost::none); }
LookupRow makeWildcardLookupRow() { return makeLookupRow(boost::none, std::nullopt); }

LookupTable makeEmptyLookupTable() { return LookupTable{}; }

LookupTable makeLookupTable() {
auto lookupRow =
LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none,
LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none,
RangeInQ(std::nullopt, std::nullopt, std::nullopt), boost::none, boost::none, boost::none, boost::none);
return LookupTable{std::move(lookupRow)};
}

LookupTable makeLookupTableWithTwoAngles() {
return LookupTable{LookupRow(0.5, boost::none, TransmissionRunPair("22347", ""), boost::none,
return LookupTable{LookupRow(0.5, std::nullopt, TransmissionRunPair("22347", ""), boost::none,
RangeInQ(0.008, 0.02, 1.2), 0.8, ProcessingInstructions("2-3"), boost::none,
boost::none),
makeLookupRow(2.3)};
Expand All @@ -374,11 +374,11 @@ LookupTable makeLookupTableWithTwoAngles() {
LookupTable makeLookupTableWithTwoAnglesAndWildcard() {
return LookupTable{
// wildcard row with no angle
LookupRow(boost::none, boost::none, TransmissionRunPair("22345", "22346"), ProcessingInstructions("5-6"),
LookupRow(boost::none, std::nullopt, TransmissionRunPair("22345", "22346"), ProcessingInstructions("5-6"),
RangeInQ(0.007, 0.01, 1.1), 0.7, ProcessingInstructions("1"), ProcessingInstructions("3,7"),
ProcessingInstructions("3-22")),
// two angle rows
LookupRow(0.5, boost::none, TransmissionRunPair("22347", ""), boost::none, RangeInQ(0.008, 0.02, 1.2), 0.8,
LookupRow(0.5, std::nullopt, TransmissionRunPair("22347", ""), boost::none, RangeInQ(0.008, 0.02, 1.2), 0.8,
ProcessingInstructions("2-3"), boost::none, boost::none),
LookupRow(makeLookupRow(2.3))};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ReductionJobs oneGroupWithTwoRowsWithOutputNamesModel();
/* Experiment */
MANTIDQT_ISISREFLECTOMETRY_DLL LookupRow makeWildcardLookupRow();
MANTIDQT_ISISREFLECTOMETRY_DLL LookupRow makeLookupRow(boost::optional<double> angle,
boost::optional<boost::regex> titleMatcher = boost::none);
std::optional<boost::regex> titleMatcher = std::nullopt);
MANTIDQT_ISISREFLECTOMETRY_DLL LookupTable makeEmptyLookupTable();
MANTIDQT_ISISREFLECTOMETRY_DLL LookupTable makeLookupTable();
MANTIDQT_ISISREFLECTOMETRY_DLL LookupTable makeLookupTableWithTwoAngles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ExperimentOptionDefaultsTest : public CxxTest::TestSuite {

void testDefaultLookupRowOptions() {
auto result = getDefaults();
auto expected = LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none,
auto expected = LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none,
RangeInQ(std::nullopt, std::nullopt, std::nullopt), boost::none, boost::none, boost::none,
boost::none);
auto foundLookupRows = result.lookupTableRows();
Expand All @@ -83,7 +83,7 @@ class ExperimentOptionDefaultsTest : public CxxTest::TestSuite {

void testValidLookupRowOptionsFromParamsFile() {
auto result = getDefaultsFromParamsFile("Experiment");
auto expected = LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none, RangeInQ(0.01, 0.03, 0.2),
auto expected = LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none, RangeInQ(0.01, 0.03, 0.2),
0.7, std::string("390-415"), std::string("370-389,416-430"), boost::none);
auto foundLookupRows = result.lookupTableRows();
TS_ASSERT_EQUALS(foundLookupRows.size(), 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ class ExperimentPresenterTest : public CxxTest::TestSuite {
}

void testInstrumentChangedUpdatesLookupRowInView() {
auto lookupRow = LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none, RangeInQ(0.01, 0.03, 0.2),
auto lookupRow = LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none, RangeInQ(0.01, 0.03, 0.2),
0.7, std::string("390-415"), std::string("370-389,416-430"), boost::none);
auto model = makeModelWithLookupRow(std::move(lookupRow));
auto defaultOptions = expectDefaults(model);
Expand All @@ -670,13 +670,13 @@ class ExperimentPresenterTest : public CxxTest::TestSuite {
}

void testInstrumentChangedUpdatesLookupRowInModel() {
auto model = makeModelWithLookupRow(LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none,
auto model = makeModelWithLookupRow(LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none,
RangeInQ(0.01, 0.03, 0.2), 0.7, std::string("390-415"),
std::string("370-389,416-430"), boost::none));
auto defaultOptions = expectDefaults(model);
auto presenter = makePresenter(std::move(defaultOptions));
presenter.notifyInstrumentChanged("POLREF");
auto expected = LookupRow(boost::none, boost::none, TransmissionRunPair(), boost::none, RangeInQ(0.01, 0.03, 0.2),
auto expected = LookupRow(boost::none, std::nullopt, TransmissionRunPair(), boost::none, RangeInQ(0.01, 0.03, 0.2),
0.7, std::string("390-415"), std::string("370-389,416-430"), boost::none);
auto lookupRows = presenter.experiment().lookupTableRows();
TS_ASSERT_EQUALS(lookupRows.size(), 1);
Expand Down Expand Up @@ -1140,13 +1140,13 @@ class ExperimentPresenterTest : public CxxTest::TestSuite {
// either as an input array of strings or an output model
OptionsRow optionsRowWithFirstAngle() { return {"0.5", "", "13463", ""}; }
LookupRow defaultsWithFirstAngle() {
return LookupRow(0.5, boost::none, TransmissionRunPair("13463", ""), boost::none, RangeInQ(), boost::none,
return LookupRow(0.5, std::nullopt, TransmissionRunPair("13463", ""), boost::none, RangeInQ(), boost::none,
boost::none, boost::none, boost::none);
}

OptionsRow optionsRowWithSecondAngle() { return {"2.3", "", "13463", "13464"}; }
LookupRow defaultsWithSecondAngle() {
return LookupRow(2.3, boost::none, TransmissionRunPair("13463", "13464"), boost::none, RangeInQ(), boost::none,
return LookupRow(2.3, std::nullopt, TransmissionRunPair("13463", "13464"), boost::none, RangeInQ(), boost::none,
boost::none, boost::none, boost::none);
}
OptionsRow optionsRowWithWildcard() { return {"", "", "13463", "13464"}; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class LookupTableValidatorTest : public CxxTest::TestSuite {
TS_ASSERT(results[0].thetaOrWildcard().is_initialized());
TS_ASSERT(results[1].thetaOrWildcard().is_initialized());
TS_ASSERT_EQUALS(results[0].thetaOrWildcard().get(), results[1].thetaOrWildcard().get());
TS_ASSERT_EQUALS(results[0].titleMatcher().get().expression(), title1);
TS_ASSERT_EQUALS(results[1].titleMatcher().get().expression(), title2);
TS_ASSERT_EQUALS(results[0].titleMatcher().value().expression(), title1);
TS_ASSERT_EQUALS(results[1].titleMatcher().value().expression(), title2);
}

void testDuplicateAnglesAndTitleMatchersAreInvalid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class LookupTableTest : public CxxTest::TestSuite {

void test_searching_by_title_matches_empty_regex() {
auto constexpr angle = 2.3;
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto table = LookupTable{emptyRegexRow};

auto group = Group("En Oh", {ModelCreationHelper::makeRow(angle)});
Expand All @@ -302,7 +302,7 @@ class LookupTableTest : public CxxTest::TestSuite {

void test_searching_by_title_matches_empty_regex_for_preview_row() {
auto constexpr angle = 2.3;
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto table = LookupTable{emptyRegexRow};

auto row = ModelCreationHelper::makePreviewRow(angle, "En Oh"s);
Expand All @@ -313,7 +313,7 @@ class LookupTableTest : public CxxTest::TestSuite {

void test_empty_title_matches_only_empty_regex() {
auto constexpr angle = 2.3;
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto regexRow = ModelCreationHelper::makeLookupRow(angle, boost::regex("Ay"));
auto table = LookupTable{emptyRegexRow, regexRow};

Expand All @@ -325,7 +325,7 @@ class LookupTableTest : public CxxTest::TestSuite {

void test_empty_title_matches_only_empty_regex_for_preview_row() {
auto constexpr angle = 2.3;
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto regexRow = ModelCreationHelper::makeLookupRow(angle, boost::regex("Ay"));
auto table = LookupTable{emptyRegexRow, regexRow};

Expand All @@ -337,7 +337,7 @@ class LookupTableTest : public CxxTest::TestSuite {

void test_no_loaded_ws_matches_only_empty_regex_for_preview_row() {
auto constexpr angle = 2.3;
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto emptyRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto regexRow = ModelCreationHelper::makeLookupRow(angle, boost::regex("Ay"));
auto table = LookupTable{emptyRegexRow, regexRow};

Expand Down Expand Up @@ -370,7 +370,7 @@ class LookupTableTest : public CxxTest::TestSuite {
void test_searching_with_no_matching_title_but_matching_theta_with_matching_title_present() {
auto angle = 0.7;
auto regexRow = ModelCreationHelper::makeLookupRow(2.3, boost::regex("Ay"));
auto nonRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto nonRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto table = LookupTable{regexRow, nonRegexRow};

auto group = Group("Ay Bee", {ModelCreationHelper::makeRow(angle)});
Expand All @@ -382,7 +382,7 @@ class LookupTableTest : public CxxTest::TestSuite {
void test_searching_with_no_matching_title_but_matching_theta_with_matching_title_present_for_preview_row() {
auto angle = 0.7;
auto regexRow = ModelCreationHelper::makeLookupRow(2.3, boost::regex("Ay"));
auto nonRegexRow = ModelCreationHelper::makeLookupRow(angle, boost::none);
auto nonRegexRow = ModelCreationHelper::makeLookupRow(angle, std::nullopt);
auto table = LookupTable{regexRow, nonRegexRow};

auto row = ModelCreationHelper::makePreviewRow(angle, "Ay Bee"s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ class ParseReflectometryStringsTest : public CxxTest::TestSuite {

void testParseTitleMatcherEmpty() {
auto result = parseTitleMatcher(" \t ");
TS_ASSERT(!result.is_initialized());
TS_ASSERT(!result.has_value());
}

void testParseTitleMatcher() {
auto result = parseTitleMatcher(".*");
TS_ASSERT(result.is_initialized());
TS_ASSERT(result.has_value());
}

void testParseTitleMatcherHandlesInvalidRegex() {
auto result = parseTitleMatcher("[");
TS_ASSERT(!result.is_initialized());
TS_ASSERT(!result.has_value());
}

void testParseOptions() {
Expand Down
Loading

0 comments on commit 1312e4e

Please sign in to comment.