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"
12#include <boost/lexical_cast.hpp>
13#include <boost/optional.hpp>
14#include <string>
15
16namespace Mantid {
17namespace API {
37template <typename T>
38T checkForMandatoryInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
40 const std::string &idf_name) {
41 auto algProperty = alg->getPointerToProperty(propName);
42 if (algProperty->isDefault()) {
43 auto defaults = instrument->getNumberParameter(idf_name);
44 if (defaults.empty()) {
45 throw std::runtime_error("No data could be retrieved from the parameters "
46 "and argument wasn't provided: " +
47 propName);
48 }
49 return static_cast<T>(defaults[0]);
50 } else {
51 return static_cast<T>(boost::lexical_cast<double, std::string>(algProperty->value()));
52 }
53}
54
68template <typename T>
69boost::optional<T> checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
71 const std::string &idf_name) {
72 auto algProperty = alg->getPointerToProperty(propName);
73 if (algProperty->isDefault()) {
74 auto defaults = instrument->getNumberParameter(idf_name);
75 if (!defaults.empty()) {
76 return boost::optional<T>(static_cast<T>(defaults[0]));
77 } else {
78 return boost::optional<T>();
79 }
80 } else {
81 double value = boost::lexical_cast<double, std::string>(algProperty->value());
82 return boost::optional<T>(static_cast<T>(value));
83 }
84}
85
89template <>
90MANTID_API_DLL std::string checkForMandatoryInstrumentDefault(Mantid::API::Algorithm *const alg,
91 const std::string &propName,
93 const std::string &idf_name);
94
95template <>
96MANTID_API_DLL boost::optional<std::string>
97checkForOptionalInstrumentDefault(Mantid::API::Algorithm *const alg, const std::string &propName,
99 const std::string &idf_name);
100} // namespace API
101} // 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:85
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
Definition: Algorithm.cpp:2033
T checkForMandatoryInstrumentDefault(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...
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)
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.