BoostOptionalToAlgorithmProperty.h 3.7 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2016 ISIS Rutherford Appleton Laboratory UKRI,
//     NScD Oak Ridge National Laboratory, European Spallation Source
//     & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
#include "MantidAPI/Algorithm.h"
Gemma Guest's avatar
Gemma Guest committed
10
#include "MantidAPI/DllConfig.h"
11
12
#include "MantidGeometry/Instrument.h"
#include <boost/lexical_cast.hpp>
13
#include <boost/optional.hpp>
14
#include <string>
15

16
namespace Mantid {
Gemma Guest's avatar
Gemma Guest committed
17
18
19
20
21
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.
22
23
24
*/

/**
Matt King's avatar
Matt King committed
25
26
27
 * Checks for the default values of a mandatory algorithm property associated
 * with
 * an instrument component. i.e MonitorIndex
28
29
30
31
32
33
34
35
36
 *
 * @param alg : A pointer to the algorithm to which the property belongs
 * @param propName : The name of the property in the algorithm
 * @param instrument : A pointer to the instrument
 * @param idf_name : The name of the property in the Instrument Defintion
 * @return A value of type T that is either the default value or the user
 * supplied value.
 *
 */
37
template <typename T>
38
39
40
T checkForMandatoryInstrumentDefault(
    Mantid::API::Algorithm *const alg, std::string propName,
    Mantid::Geometry::Instrument_const_sptr instrument, std::string idf_name) {
41
42
43
  auto algProperty = alg->getPointerToProperty(propName);
  if (algProperty->isDefault()) {
    auto defaults = instrument->getNumberParameter(idf_name);
44
    if (defaults.empty()) {
45
46
47
48
49
50
      throw std::runtime_error("No data could be retrieved from the parameters "
                               "and argument wasn't provided: " +
                               propName);
    }
    return static_cast<T>(defaults[0]);
  } else {
51
52
    return static_cast<T>(
        boost::lexical_cast<double, std::string>(algProperty->value()));
53
54
55
  }
}

56
/**
LamarMoore's avatar
LamarMoore committed
57
58
59
60
61
62
63
64
65
66
67
68
 * Checks for the default values of an optional algorithm property associated
 * with
 * an instrument component. i.e MonitorIndex
 *
 * @param alg : A pointer to the algorithm to which the property belongs
 * @param propName : The name of the property in the algorithm
 * @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.
 *
 */
69
template <typename T>
70
71
72
boost::optional<T> checkForOptionalInstrumentDefault(
    Mantid::API::Algorithm *const alg, std::string propName,
    Mantid::Geometry::Instrument_const_sptr instrument, std::string idf_name) {
73
74
75
  auto algProperty = alg->getPointerToProperty(propName);
  if (algProperty->isDefault()) {
    auto defaults = instrument->getNumberParameter(idf_name);
76
    if (!defaults.empty()) {
77
78
79
80
81
82
83
84
85
86
      return boost::optional<T>(static_cast<T>(defaults[0]));
    } else {
      return boost::optional<T>();
    }
  } else {
    double value =
        boost::lexical_cast<double, std::string>(algProperty->value());
    return boost::optional<T>(static_cast<T>(value));
  }
}
Gemma Guest's avatar
Gemma Guest committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100

/**
 * Specializations for std::string
 */
template <>
DLLExport std::string checkForMandatoryInstrumentDefault(
    Mantid::API::Algorithm *const alg, std::string propName,
    Mantid::Geometry::Instrument_const_sptr instrument, std::string idf_name);

template <>
DLLExport boost::optional<std::string> checkForOptionalInstrumentDefault(
    Mantid::API::Algorithm *const alg, std::string propName,
    Mantid::Geometry::Instrument_const_sptr instrument, std::string idf_name);
} // namespace API
LamarMoore's avatar
LamarMoore committed
101
} // namespace Mantid
102