Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parial move of reflectometry gui to std::optional - ornl-next #38651

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Framework/API/inc/MantidAPI/BoostOptionalToAlgorithmProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#include "MantidAPI/DllConfig.h"
#include "MantidGeometry/Instrument.h"
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <optional>
#include <string>

namespace Mantid {
namespace API {
/** BoostOptionalToAlgorithmProperty : Checks for default values of an
algorithm property if the user has not supplied the value. If it is a mandatory
property then the value will be returned, if the property is optional then a
value of type boost::optional<T> will be returned.
value of type std::optional<T> will be returned.
*/

/**
Expand Down Expand Up @@ -62,24 +62,24 @@ T checkForMandatoryInstrumentDefault(Mantid::API::Algorithm *const alg, const st
* @param instrument : A pointer to the instrument
* @param idf_name : The name of the property in the Instrument Defintion
* @return A boost optional value of type T that is either the default value,
* the user supplied value or an uninitialized boost::optional.
* the user supplied value or an uninitialized std::optional.
*
*/
template <typename T>
boost::optional<T> checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
const Mantid::Geometry::Instrument_const_sptr &instrument,
const std::string &idf_name) {
std::optional<T> checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
const Mantid::Geometry::Instrument_const_sptr &instrument,
const std::string &idf_name) {
auto algProperty = alg->getPointerToProperty(propName);
if (algProperty->isDefault()) {
auto defaults = instrument->getNumberParameter(idf_name);
if (!defaults.empty()) {
return boost::optional<T>(static_cast<T>(defaults[0]));
return std::optional<T>(static_cast<T>(defaults[0]));
} else {
return boost::optional<T>();
return std::nullopt;
}
} else {
double value = boost::lexical_cast<double, std::string>(algProperty->value());
return boost::optional<T>(static_cast<T>(value));
return std::optional<T>(static_cast<T>(value));
}
}

Expand All @@ -93,7 +93,7 @@ MANTID_API_DLL std::string checkForMandatoryInstrumentDefault(Mantid::API::Algor
const std::string &idf_name);

template <>
MANTID_API_DLL boost::optional<std::string>
MANTID_API_DLL std::optional<std::string>
checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
const Mantid::Geometry::Instrument_const_sptr &instrument,
const std::string &idf_name);
Expand Down
14 changes: 7 additions & 7 deletions Framework/API/src/BoostOptionalToAlgorithmProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ std::string checkForMandatoryInstrumentDefault(Mantid::API::Algorithm *const alg
}

template <>
boost::optional<std::string>
checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
const Mantid::Geometry::Instrument_const_sptr &instrument,
const std::string &idf_name) {
std::optional<std::string> checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg,
const std::string &propName,
const Mantid::Geometry::Instrument_const_sptr &instrument,
const std::string &idf_name) {
auto algProperty = alg->getPointerToProperty(propName);
if (algProperty->isDefault()) {
auto defaults = instrument->getStringParameter(idf_name);
if (!defaults.empty()) {
return boost::optional<std::string>(defaults[0]);
return std::optional<std::string>(defaults[0]);
} else {
return boost::optional<std::string>();
return std::nullopt;
}
} else {
return boost::optional<std::string>(algProperty->value());
return std::optional<std::string>(algProperty->value());
}
}
} // namespace Mantid::API
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
49 changes: 43 additions & 6 deletions qt/scientific_interfaces/ISISReflectometry/Common/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <boost/optional.hpp>
#include <iterator>
#include <optional>
#include <sstream>
#include <type_traits>
#include <vector>
Expand All @@ -33,6 +34,14 @@ boost::optional<Out> map(boost::optional<In> const &in, Transform transform) {
return boost::none;
}

template <typename In, typename Transform, typename Out = typename std::invoke_result<Transform, In>::type>
std::optional<Out> map(std::optional<In> const &in, Transform transform) {
if (in.has_value())
return transform(in.value());
else
return std::nullopt;
}

/** Converts an optional value to string
*
* @param maybeValue optional value
Expand All @@ -44,6 +53,16 @@ template <typename T> std::string optionalToString(boost::optional<T> maybeValue
.get_value_or(std::string());
}

/** Converts an optional value to string
*
* @param maybeValue optional value
* @return The value as a string or an empty string
*
*/
template <typename T> std::string optionalToString(std::optional<T> maybeValue) {
return map(maybeValue, [](T const &value) -> std::string { return std::to_string(value); }).value_or(std::string());
}

/** Converts value to string with specified precision
*
* @param value input value
Expand All @@ -65,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 @@ -79,10 +98,28 @@ 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);
}
return std::string();
}

/** Converts optional value to string with optional precision
*
* @param maybeValue optional input value
* @param precision optional output precision
* @return The value as a string (with specified precision if given) or empty
* string
*
*/
template <typename T> std::string optionalToString(std::optional<T> maybeValue, std::optional<int> precision) {
if (maybeValue.has_value()) {
if (precision.has_value()) {
return valueToString(maybeValue.value(), precision.value());
}
return optionalToString(maybeValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "Common/DllConfig.h"
#include "MantidAPI/BoostOptionalToAlgorithmProperty.h"
#include "MantidGeometry/Instrument_fwd.h"
#include <optional>
#include <string>

namespace MantidQt {
Expand All @@ -25,7 +25,7 @@ class OptionDefaults {
template <typename T>
T getValueOrDefault(std::string const &propertyName, std::string const &parameterName, T defaultValue) const;
template <typename T>
boost::optional<T> getOptionalValue(std::string const &propertyName, std::string const &parameterName) const;
std::optional<T> getOptionalValue(std::string const &propertyName, std::string const &parameterName) const;
template <typename T> T getValue(std::string const &propertyName, std::string const &parameterName) const;

int getIntOrZero(std::string const &propertyName, std::string const &parameterName) const;
Expand All @@ -47,14 +47,14 @@ T OptionDefaults::getValueOrDefault(std::string const &propertyName, std::string
T defaultValue) const {
auto maybeValue =
Mantid::API::checkForOptionalInstrumentDefault<T>(m_algorithm.get(), propertyName, m_instrument, parameterName);
if (maybeValue.is_initialized())
return maybeValue.get();
if (maybeValue.has_value())
return maybeValue.value();
return defaultValue;
}

template <typename T>
boost::optional<T> OptionDefaults::getOptionalValue(std::string const &propertyName,
std::string const &parameterName) const {
std::optional<T> OptionDefaults::getOptionalValue(std::string const &propertyName,
std::string const &parameterName) const {
return Mantid::API::checkForOptionalInstrumentDefault<T>(m_algorithm.get(), propertyName, m_instrument,
parameterName);
}
Expand Down
40 changes: 20 additions & 20 deletions qt/scientific_interfaces/ISISReflectometry/Common/Parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,59 @@ bool isEntirelyWhitespace(std::string const &string) {
return std::all_of(string.cbegin(), string.cend(), [](unsigned char c) { return std::isspace(c); });
}

boost::optional<double> parseDouble(std::string string) {
std::optional<double> parseDouble(std::string string) {
boost::trim(string);
auto end = std::size_t();
try {
auto result = std::stod(string, &end);
if (end == string.size())
return result;
else
return boost::none;
return std::nullopt;
} catch (std::invalid_argument &) {
return boost::none;
return std::nullopt;
} catch (std::out_of_range &) {
return boost::none;
return std::nullopt;
}
}

boost::optional<double> parseNonNegativeDouble(std::string string) {
std::optional<double> parseNonNegativeDouble(std::string string) {
auto maybeNegative = parseDouble(std::move(string));
if (maybeNegative.is_initialized() && maybeNegative.get() >= 0.0)
return maybeNegative.get();
if (maybeNegative.has_value() && maybeNegative.value() >= 0.0)
return maybeNegative.value();
else
return boost::none;
return std::nullopt;
}

boost::optional<double> parseNonNegativeNonZeroDouble(std::string string) {
std::optional<double> parseNonNegativeNonZeroDouble(std::string string) {
auto maybeNegative = parseDouble(std::move(string));
if (maybeNegative.is_initialized() && maybeNegative.get() > 0.0)
return maybeNegative.get();
if (maybeNegative.has_value() && maybeNegative.value() > 0.0)
return maybeNegative.value();
else
return boost::none;
return std::nullopt;
}

boost::optional<int> parseInt(std::string string) {
std::optional<int> parseInt(std::string string) {
boost::trim(string);
auto end = std::size_t();
try {
auto result = std::stoi(string, &end);
if (end == string.size())
return result;
else
return boost::none;
return std::nullopt;
} catch (std::invalid_argument &) {
return boost::none;
return std::nullopt;
} catch (std::out_of_range &) {
return boost::none;
return std::nullopt;
}
}

boost::optional<int> parseNonNegativeInt(std::string string) {
std::optional<int> parseNonNegativeInt(std::string string) {
auto maybeNegative = parseInt(std::move(string));
if (maybeNegative.is_initialized() && maybeNegative.get() >= 0)
return maybeNegative.get();
if (maybeNegative.has_value() && maybeNegative.value() >= 0)
return maybeNegative.value();
else
return boost::none;
return std::nullopt;
}
} // namespace MantidQt::CustomInterfaces::ISISReflectometry
20 changes: 10 additions & 10 deletions qt/scientific_interfaces/ISISReflectometry/Common/Parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
#pragma once
#include "DllConfig.h"
#include <boost/algorithm/string.hpp>
#include <boost/optional.hpp>
#include <optional>
#include <vector>

namespace MantidQt {
namespace CustomInterfaces {
namespace ISISReflectometry {

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<double> parseDouble(std::string string);
MANTIDQT_ISISREFLECTOMETRY_DLL std::optional<double> parseDouble(std::string string);

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<double> parseNonNegativeDouble(std::string string);
MANTIDQT_ISISREFLECTOMETRY_DLL std::optional<double> parseNonNegativeDouble(std::string string);

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<double> parseNonNegativeNonZeroDouble(std::string string);
MANTIDQT_ISISREFLECTOMETRY_DLL std::optional<double> parseNonNegativeNonZeroDouble(std::string string);

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<int> parseInt(std::string string);
MANTIDQT_ISISREFLECTOMETRY_DLL std::optional<int> parseInt(std::string string);

MANTIDQT_ISISREFLECTOMETRY_DLL boost::optional<int> parseNonNegativeInt(std::string string);
MANTIDQT_ISISREFLECTOMETRY_DLL std::optional<int> parseNonNegativeInt(std::string string);

bool MANTIDQT_ISISREFLECTOMETRY_DLL isEntirelyWhitespace(std::string const &string);

template <typename ParseItemFunction>
boost::optional<std::vector<typename std::invoke_result<ParseItemFunction, std::string const &>::type::value_type>>
std::optional<std::vector<typename std::invoke_result<ParseItemFunction, std::string const &>::type::value_type>>
parseList(std::string commaSeparatedValues, ParseItemFunction parseItem) {
using ParsedItem = typename std::invoke_result<ParseItemFunction, std::string const &>::type::value_type;
if (!commaSeparatedValues.empty()) {
Expand All @@ -37,10 +37,10 @@ parseList(std::string commaSeparatedValues, ParseItemFunction parseItem) {
parsedItems.reserve(items.size());
for (auto const &item : items) {
auto maybeParsedItem = parseItem(item);
if (maybeParsedItem.is_initialized())
parsedItems.emplace_back(maybeParsedItem.get());
if (maybeParsedItem.has_value())
parsedItems.emplace_back(maybeParsedItem.value());
else
return boost::none;
return std::nullopt;
}
return parsedItems;
} else {
Expand Down
Loading
Loading