Skip to content

Commit

Permalink
Migrate to std::optional for int
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Jan 17, 2025
1 parent 0537c1f commit dc878b2
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 76 deletions.
2 changes: 1 addition & 1 deletion buildconfig/CMake/CppCheck_Suppressions.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ virtualCallInConstructor:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflec
missingOverride:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflectometry/GUI/Save/SaveAlgorithmRunner.h:18
returnByReference:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflectometry/Reduction/Experiment.h:54
missingOverride:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflectometry/Reduction/Group.h:31
missingOverride:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflectometry/Reduction/IGroup.h:18
missingOverride:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflectometry/Reduction/IGroup.h:19
returnByReference:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/ISISReflectometry/Reduction/TransmissionStitchOptions.h:34
returnByReference:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/Indirect/Reduction/ISISEnergyTransferData.h:42
returnByReference:${CMAKE_SOURCE_DIR}/qt/scientific_interfaces/Indirect/Reduction/ISISEnergyTransferData.h:43
Expand Down
10 changes: 5 additions & 5 deletions qt/scientific_interfaces/ISISReflectometry/Common/IndexOf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <algorithm>
#include <boost/optional.hpp>
#include <iterator>
#include <optional>

namespace MantidQt {
namespace CustomInterfaces {
namespace ISISReflectometry {

template <typename Container, typename Predicate>
boost::optional<int> indexOf(Container const &container, Predicate pred) {
std::optional<int> indexOf(Container const &container, Predicate pred) {
auto maybeItemIt = std::find_if(container.cbegin(), container.cend(), pred);
if (maybeItemIt != container.cend())
return static_cast<int>(std::distance(container.cbegin(), maybeItemIt));
else
return boost::none;
return std::nullopt;
}

template <typename Container, typename ValueType>
boost::optional<int> indexOfValue(Container const &container, ValueType value) {
std::optional<int> indexOfValue(Container const &container, ValueType value) {
auto maybeItemIt = std::find(container.cbegin(), container.cend(), value);
if (maybeItemIt != container.cend())
return static_cast<int>(std::distance(container.cbegin(), maybeItemIt));
else
return boost::none;
return std::nullopt;
}
} // namespace ISISReflectometry
} // namespace CustomInterfaces
Expand Down
12 changes: 6 additions & 6 deletions qt/scientific_interfaces/ISISReflectometry/Common/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ template <typename T> std::string valueToString(T value, int precision) {
* @return The value as a string (with specified precision if given)
*
*/
template <typename T> std::string valueToString(T value, boost::optional<int> precision) {
if (precision.is_initialized())
return valueToString(value, precision.get());
template <typename T> std::string valueToString(T value, std::optional<int> precision) {
if (precision.has_value())
return valueToString(value, precision.value());
return std::to_string(value);
}

Expand All @@ -98,10 +98,10 @@ template <typename T> std::string valueToString(T value, boost::optional<int> pr
* string
*
*/
template <typename T> std::string optionalToString(boost::optional<T> maybeValue, boost::optional<int> precision) {
template <typename T> std::string optionalToString(boost::optional<T> maybeValue, std::optional<int> precision) {
if (maybeValue.is_initialized()) {
if (precision.is_initialized()) {
return valueToString(maybeValue.get(), precision.get());
if (precision.has_value()) {
return valueToString(maybeValue.get(), precision.value());
}
return optionalToString(maybeValue);
}
Expand Down
19 changes: 8 additions & 11 deletions qt/scientific_interfaces/ISISReflectometry/GUI/Common/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void Decoder::decodeInstrument(const QtInstrumentView *gui, const QMap<QString,
}

void Decoder::decodeRuns(QtRunsView *gui, ReductionJobs *redJobs, RunsTablePresenter *presenter,
const QMap<QString, QVariant> &map, boost::optional<int> precision,
const QMap<QString, QVariant> &map, std::optional<int> precision,
QtCatalogSearcher *searcher) {
decodeRunsTable(gui->m_tableView, redJobs, presenter, map[QString("runsTable")].toMap(), std::move(precision));
gui->m_ui.comboSearchInstrument->setCurrentIndex(map[QString("comboSearchInstrument")].toInt());
Expand Down Expand Up @@ -228,26 +228,23 @@ MantidWidgets::Batch::Cell qRangeCellOrDefault(RangeInQ const &qRangeInput, Rang
return result;
}

std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, const boost::optional<int> &precision) {
std::optional<int> precisionStd = std::nullopt;
if (precision)
precisionStd = precision.get();
std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, const std::optional<int> &precision) {
return std::vector<MantidQt::MantidWidgets::Batch::Cell>(
{MantidQt::MantidWidgets::Batch::Cell(boost::join(row.runNumbers(), "+")),
MantidQt::MantidWidgets::Batch::Cell(valueToString(row.theta(), precision)),
MantidQt::MantidWidgets::Batch::Cell(row.transmissionWorkspaceNames().firstRunList()),
MantidQt::MantidWidgets::Batch::Cell(row.transmissionWorkspaceNames().secondRunList()),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::min, precisionStd),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::max, precisionStd),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::step, precisionStd),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::min, precision),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::max, precision),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::step, precision),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.scaleFactor(), precision)),
MantidQt::MantidWidgets::Batch::Cell(MantidWidgets::optionsToString(row.reductionOptions())),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.lookupIndex(), precisionStd))});
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.lookupIndex(), precision))});
}
} // namespace

void Decoder::updateRunsTableViewFromModel(QtRunsTableView *view, const ReductionJobs *model,
const boost::optional<int> &precision) {
const std::optional<int> &precision) {
auto jobTreeView = view->m_jobs.get();
auto const &groups = model->groups();
for (auto groupIndex = 0u; groupIndex < groups.size(); ++groupIndex) {
Expand Down Expand Up @@ -277,7 +274,7 @@ void Decoder::updateRunsTableViewFromModel(QtRunsTableView *view, const Reductio
}

void Decoder::decodeRunsTable(QtRunsTableView *gui, ReductionJobs *redJobs, RunsTablePresenter *presenter,
const QMap<QString, QVariant> &map, boost::optional<int> precision) {
const QMap<QString, QVariant> &map, std::optional<int> precision) {
QSignalBlocker signalBlockerView(gui);

m_projectSave = map[QString("projectSave")].toBool();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL Decoder : public MantidQt::API::BaseDecoder
void decodePerAngleDefaultsRows(QTableWidget *tab, int rowsNum, int columnsNum, const QList<QVariant> &list);
void decodeInstrument(const QtInstrumentView *gui, const QMap<QString, QVariant> &map);
void decodeRuns(QtRunsView *gui, ReductionJobs *redJobs, RunsTablePresenter *presenter,
const QMap<QString, QVariant> &map, boost::optional<int> precision, QtCatalogSearcher *searcher);
const QMap<QString, QVariant> &map, std::optional<int> precision, QtCatalogSearcher *searcher);
void decodeRunsTable(QtRunsTableView *gui, ReductionJobs *redJobs, RunsTablePresenter *presenter,
const QMap<QString, QVariant> &map, boost::optional<int> precision);
const QMap<QString, QVariant> &map, std::optional<int> precision);
void decodeRunsTableModel(ReductionJobs *jobs, const QList<QVariant> &list);
MantidQt::CustomInterfaces::ISISReflectometry::Group decodeGroup(const QMap<QString, QVariant> &map);
std::vector<boost::optional<MantidQt::CustomInterfaces::ISISReflectometry::Row>>
Expand All @@ -81,7 +81,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL Decoder : public MantidQt::API::BaseDecoder
void decodeSave(const QtSaveView *gui, const QMap<QString, QVariant> &map);
void decodeEvent(const QtEventView *gui, const QMap<QString, QVariant> &map);
void updateRunsTableViewFromModel(QtRunsTableView *view, const ReductionJobs *model,
const boost::optional<int> &precision);
const std::optional<int> &precision);

bool m_projectSave = false;
size_t m_currentBatchVersion = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ void QtExperimentView::setText(QLineEdit &lineEdit, boost::optional<double> valu
setText(lineEdit, value.get());
}

void QtExperimentView::setText(QLineEdit &lineEdit, boost::optional<int> value) {
void QtExperimentView::setText(QLineEdit &lineEdit, std::optional<int> value) {
if (value)
setText(lineEdit, value.get());
setText(lineEdit, value.value());
}

void QtExperimentView::setText(QLineEdit &lineEdit, boost::optional<std::string> const &text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public slots:
void setText(QLineEdit &lineEdit, int value);
void setText(QLineEdit &lineEdit, double value);
void setText(QLineEdit &lineEdit, std::string const &value);
void setText(QLineEdit &lineEdit, boost::optional<int> value);
void setText(QLineEdit &lineEdit, std::optional<int> value);
void setText(QLineEdit &lineEdit, boost::optional<double> value);
void setText(QLineEdit &lineEdit, boost::optional<std::string> const &value);
std::string textFromCell(QTableWidgetItem const *maybeNullItem) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ void QtInstrumentView::setText(QLineEdit &lineEdit, boost::optional<double> valu
setText(lineEdit, value.get());
}

void QtInstrumentView::setText(QLineEdit &lineEdit, boost::optional<int> value) {
void QtInstrumentView::setText(QLineEdit &lineEdit, std::optional<int> value) {
if (value)
setText(lineEdit, value.get());
setText(lineEdit, value.value());
}

void QtInstrumentView::setText(QLineEdit &lineEdit, boost::optional<std::string> const &text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public slots:
void setText(QLineEdit &lineEdit, int value);
void setText(QLineEdit &lineEdit, double value);
void setText(QLineEdit &lineEdit, std::string const &value);
void setText(QLineEdit &lineEdit, boost::optional<int> value);
void setText(QLineEdit &lineEdit, std::optional<int> value);
void setText(QLineEdit &lineEdit, boost::optional<double> value);
void setText(QLineEdit &lineEdit, boost::optional<std::string> const &value);
void setChecked(QCheckBox &checkBox, bool checked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class IMainWindowPresenter {
virtual bool isProcessPartialGroupPrevented() const = 0;
virtual bool isRoundChecked() const = 0;
virtual int &getRoundPrecision() const = 0;
virtual boost::optional<int> roundPrecision() const = 0;
virtual std::optional<int> roundPrecision() const = 0;
virtual bool isCloseEventPrevented() = 0;
virtual bool isCloseBatchPrevented(int batchIndex) const = 0;
virtual bool isOverwriteBatchPrevented(int tabIndex) const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ int &MainWindowPresenter::getRoundPrecision() const {
return m_optionsDialogPresenter->getIntOption(std::string("RoundPrecision"));
}

boost::optional<int> MainWindowPresenter::roundPrecision() const {
std::optional<int> MainWindowPresenter::roundPrecision() const {
if (isRoundChecked())
return getRoundPrecision();
return boost::none;
return std::nullopt;
}

bool MainWindowPresenter::discardChanges(std::string const &message) const {
Expand Down Expand Up @@ -268,12 +268,12 @@ void MainWindowPresenter::addNewBatch(IBatchView *batchView) {
}

void MainWindowPresenter::initNewBatch(IBatchPresenter *batchPresenter, std::string const &instrument,
boost::optional<int> precision) {
std::optional<int> precision) {

batchPresenter->initInstrumentList(instrument);
batchPresenter->notifyInstrumentChanged(instrument);
if (precision.is_initialized())
batchPresenter->notifySetRoundPrecision(precision.get());
if (precision.has_value())
batchPresenter->notifySetRoundPrecision(precision.value());

// starts in the paused state
batchPresenter->notifyReductionPaused();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL MainWindowPresenter : public MainWindowSubs
bool isAnyBatchUnsaved() const override;
bool isRoundChecked() const override;
int &getRoundPrecision() const override;
boost::optional<int> roundPrecision() const override;
std::optional<int> roundPrecision() const override;
bool isWarnProcessAllChecked() const override;
bool isWarnProcessPartialGroupChecked() const override;
bool isCloseBatchPrevented(int batchIndex) const override;
Expand All @@ -108,7 +108,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL MainWindowPresenter : public MainWindowSubs
void optionsChanged() const;
void showHelp();
void addNewBatch(IBatchView *batchView);
void initNewBatch(IBatchPresenter *batchPresenter, std::string const &instrument, boost::optional<int> precision);
void initNewBatch(IBatchPresenter *batchPresenter, std::string const &instrument, std::optional<int> precision);
void updateInstrument(const std::string &instrumentName);
void setDefaultInstrument(const std::string &newInstrument);
void onInstrumentChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,19 @@ std::optional<size_t> incrementIndex(const Row &row) {
return lookupIndex.has_value() ? std::optional<size_t>(lookupIndex.value() + 1) : std::nullopt;
}

std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, boost::optional<int> precision) {
// convert type for precision
std::optional<int> precisionStd = std::nullopt;
if (precision)
precisionStd = precision.get();

std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, std::optional<int> precision) {
auto lookupIndex = incrementIndex(row);
return std::vector<MantidQt::MantidWidgets::Batch::Cell>(
{MantidQt::MantidWidgets::Batch::Cell(boost::join(row.runNumbers(), "+")),
MantidQt::MantidWidgets::Batch::Cell(valueToString(row.theta(), precision)),
MantidQt::MantidWidgets::Batch::Cell(row.transmissionWorkspaceNames().firstRunList()),
MantidQt::MantidWidgets::Batch::Cell(row.transmissionWorkspaceNames().secondRunList()),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::min, precisionStd),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::max, precisionStd),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::step, precisionStd),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::min, precision),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::max, precision),
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::step, precision),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.scaleFactor(), precision)),
MantidQt::MantidWidgets::Batch::Cell(MantidWidgets::optionsToString(row.reductionOptions())),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(lookupIndex, precisionStd))});
MantidQt::MantidWidgets::Batch::Cell(optionalToString(lookupIndex, precision))});
}
} // namespace

Expand Down Expand Up @@ -85,6 +80,6 @@ void JobsViewUpdater::rowModified(int groupIndex, int rowIndex, Row const &row)

void JobsViewUpdater::setPrecision(const int &precision) { m_precision = precision; }

void JobsViewUpdater::resetPrecision() { m_precision = boost::none; }
void JobsViewUpdater::resetPrecision() { m_precision = std::nullopt; }

} // namespace MantidQt::CustomInterfaces::ISISReflectometry
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class JobsViewUpdater {

private:
MantidQt::MantidWidgets::Batch::IJobTreeView &m_view;
boost::optional<int> m_precision;
std::optional<int> m_precision;
};
} // namespace ISISReflectometry
} // namespace CustomInterfaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,6 @@ RunsTableViewFactory::RunsTableViewFactory(std::vector<std::string> instruments)
QtRunsTableView *RunsTableViewFactory::operator()() const { return new QtRunsTableView(m_instruments); }

int RunsTableViewFactory::indexOfElseFirst(std::string const &instrument) const {
return indexOf(m_instruments, [&instrument](std::string const &inst) { return instrument == inst; }).get_value_or(0);
return indexOf(m_instruments, [&instrument](std::string const &inst) { return instrument == inst; }).value_or(0);
}
} // namespace MantidQt::CustomInterfaces::ISISReflectometry
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ bool groupNameExists(std::string const &groupName, ReductionJobs const &jobs,

// Check if the group name exists in the jobs
auto maybeExistingGroupIndex = jobs.indexOfGroupWithName(groupName);
if (!maybeExistingGroupIndex.is_initialized())
if (!maybeExistingGroupIndex.has_value())
return false;

// If it exists but in one of the roots to ignore, return false
auto existingGroupLocation = MantidWidgets::Batch::RowLocation({maybeExistingGroupIndex.get()});
auto existingGroupLocation = MantidWidgets::Batch::RowLocation({maybeExistingGroupIndex.value()});
if (std::find(rootsToIgnore.cbegin(), rootsToIgnore.cend(), existingGroupLocation) != rootsToIgnore.cend())
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool Group::requiresPostprocessing(bool reprocessFailed) const {

std::string Group::postprocessedWorkspaceName() const { return m_postprocessedWorkspaceName; }

boost::optional<int> Group::indexOfRowWithTheta(double theta, double tolerance) const {
std::optional<int> Group::indexOfRowWithTheta(double theta, double tolerance) const {
return indexOf(m_rows, [theta, tolerance](boost::optional<Row> const &row) -> bool {
return row.is_initialized() && std::abs(row.get().theta() - theta) < tolerance;
});
Expand Down
6 changes: 3 additions & 3 deletions qt/scientific_interfaces/ISISReflectometry/Reduction/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL Group final : public IGroup {

void resetSkipped() override;

boost::optional<int> indexOfRowWithTheta(double angle, double tolerance) const override;
std::optional<int> indexOfRowWithTheta(double angle, double tolerance) const override;

boost::optional<Row> const &operator[](int rowIndex) const override;
std::vector<boost::optional<Row>> const &rows() const override;
Expand Down Expand Up @@ -103,8 +103,8 @@ void mergeRowsInto(Group &intoHere, Group const &fromHere, int groupIndex, doubl
if (maybeRow.is_initialized()) {
auto const &fromRow = maybeRow.get();
auto index = intoHere.indexOfRowWithTheta(fromRow.theta(), thetaTolerance);
if (index.is_initialized()) {
auto const updateAtIndex = index.get();
if (index.has_value()) {
auto const updateAtIndex = index.value();
auto const &intoRow = intoHere[updateAtIndex].get();
auto updatedRow = mergedRow(intoRow, fromRow);
intoHere.updateRow(updateAtIndex, updatedRow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Item.h"
#include "Row.h"
#include <boost/optional.hpp>
#include <optional>
#include <string>
#include <vector>

Expand All @@ -32,7 +33,7 @@ class IGroup : public Item {

virtual void resetSkipped() = 0;

virtual boost::optional<int> indexOfRowWithTheta(double angle, double tolerance) const = 0;
virtual std::optional<int> indexOfRowWithTheta(double angle, double tolerance) const = 0;

virtual boost::optional<Row> const &operator[](int rowIndex) const = 0;
virtual std::vector<boost::optional<Row>> const &rows() const = 0;
Expand Down
Loading

0 comments on commit dc878b2

Please sign in to comment.