Mantid
Loading...
Searching...
No Matches
BoostOptionalToAlgorithmProperty.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2016 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
10#include "MantidAPI/DllConfig.h"
13#include <boost/lexical_cast.hpp>
14#include <optional>
15#include <string>
16#include <type_traits>
17
18namespace Mantid {
19namespace API {
39template <typename T>
40T checkForMandatoryInstrumentDefault(const Mantid::API::Algorithm *const alg, const std::string &propName,
42 const std::string &idf_name) {
43 const auto *algProperty = alg->getPointerToProperty(propName);
44 if (algProperty->isDefault()) {
45 auto defaults = instrument->getNumberParameter(idf_name);
46 if (defaults.empty()) {
47 throw std::runtime_error("No data could be retrieved from the parameters "
48 "and argument wasn't provided: " +
49 propName);
50 }
51 return static_cast<T>(defaults[0]);
52 } else {
53 return static_cast<T>(boost::lexical_cast<double, std::string>(algProperty->value()));
54 }
55}
56
70template <typename T>
71std::optional<T> checkForOptionalInstrumentDefault(const Mantid::API::Algorithm *const alg, const std::string &propName,
73 const std::string &idf_name) {
74 const auto *algProperty = alg->getPointerToProperty(propName);
75 if (algProperty->isDefault()) {
76 auto defaults = instrument->getNumberParameter(idf_name);
77 if (!defaults.empty()) {
78 return std::optional<T>(static_cast<T>(defaults[0]));
79 } else {
80 return std::nullopt;
81 }
82 } else {
83 double value = boost::lexical_cast<double, std::string>(algProperty->value());
84 return std::optional<T>(static_cast<T>(value));
85 }
86}
87
99template <typename T>
100T checkForMandatoryInstrumentDefault(Mantid::API::Algorithm const *const alg, std::string const &propName,
101 Mantid::Geometry::ComponentInfo const &componentInfo,
102 std::string const &idf_name) {
103 auto const *algProperty = alg->getPointerToProperty(propName);
104 if constexpr (std::is_same_v<T, std::string>) {
105 if (!algProperty->isDefault()) {
106 return algProperty->value();
107 }
108 auto const defaults = componentInfo.getStringParameter(componentInfo.root(), idf_name);
109 if (defaults.empty()) {
110 throw std::runtime_error("No data could be retrieved from the parameters "
111 "and argument wasn't provided: " +
112 propName);
113 }
114 return defaults[0];
115 } else {
116 if (!algProperty->isDefault()) {
117 return static_cast<T>(boost::lexical_cast<double, std::string>(algProperty->value()));
118 }
119 auto const defaults = componentInfo.getNumberParameter(componentInfo.root(), idf_name);
120 if (defaults.empty()) {
121 throw std::runtime_error("No data could be retrieved from the parameters "
122 "and argument wasn't provided: " +
123 propName);
124 }
125 return static_cast<T>(defaults[0]);
126 }
127}
128
140template <typename T>
141std::optional<T> checkForOptionalInstrumentDefault(Mantid::API::Algorithm const *const alg, std::string const &propName,
142 Mantid::Geometry::ComponentInfo const &componentInfo,
143 std::string const &idf_name) {
144 auto const *algProperty = alg->getPointerToProperty(propName);
145 if constexpr (std::is_same_v<T, std::string>) {
146 if (!algProperty->isDefault()) {
147 return algProperty->value();
148 }
149 auto const defaults = componentInfo.getStringParameter(componentInfo.root(), idf_name);
150 if (defaults.empty()) {
151 return std::nullopt;
152 }
153 return defaults[0];
154 } else {
155 if (!algProperty->isDefault()) {
156 return static_cast<T>(boost::lexical_cast<double, std::string>(algProperty->value()));
157 }
158 auto const defaults = componentInfo.getNumberParameter(componentInfo.root(), idf_name);
159 if (defaults.empty()) {
160 return std::nullopt;
161 }
162 return static_cast<T>(defaults[0]);
163 }
164}
165
169template <>
170MANTID_API_DLL std::string checkForMandatoryInstrumentDefault(const Mantid::API::Algorithm *const alg,
171 const std::string &propName,
173 const std::string &idf_name);
174
175template <>
176MANTID_API_DLL std::optional<std::string>
177checkForOptionalInstrumentDefault(const Mantid::API::Algorithm *const alg, const std::string &propName,
179 const std::string &idf_name);
180} // namespace API
181} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
ComponentInfo : Provides a component centric view on to the instrument.
std::vector< std::string > getStringParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
std::vector< double > getNumberParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
virtual std::string value() const =0
Returns the value of the property as a string.
T checkForMandatoryInstrumentDefault(const Mantid::API::Algorithm *const alg, const std::string &propName, const Mantid::Geometry::Instrument_const_sptr &instrument, const std::string &idf_name)
BoostOptionalToAlgorithmProperty : Checks for default values of an algorithm property if the user has...
std::optional< T > checkForOptionalInstrumentDefault(const Mantid::API::Algorithm *const alg, const std::string &propName, const Mantid::Geometry::Instrument_const_sptr &instrument, const std::string &idf_name)
Checks for the default values of an optional algorithm property associated with an instrument compone...
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
Helper class which provides the Collimation Length for SANS instruments.