Mantid
Loading...
Searching...
No Matches
Algorithm.hxx
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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
12#include "MantidIndexing/SpectrumIndexSet.h"
13
14#include <memory>
15#include <type_traits>
16
17namespace {
18template <typename T1, typename T2>
19void setWorkspaceProperty(Mantid::API::WorkspaceProperty<T1> *wsProp, const T2 &wksp, const std::true_type &) {
20 *wsProp = wksp;
21}
22
23template <typename T1, typename T2>
24void setWorkspaceProperty(Mantid::API::WorkspaceProperty<T1> *wsProp, const T2 &wksp, const std::false_type &) {
25 wsProp->setValue(wksp);
26}
27} // namespace
28
29namespace Mantid {
30namespace API {
41template <typename T, const int AllowedIndexTypes, typename... WSPropArgs, typename>
42void Algorithm::declareWorkspaceInputProperties(const std::string &propertyName, const std::string &doc,
43 WSPropArgs &&...wsPropArgs) {
44 auto wsProp = std::make_unique<WorkspaceProperty<T>>(propertyName, "", Kernel::Direction::Input,
45 std::forward<WSPropArgs>(wsPropArgs)...);
46 const auto &wsPropRef = *wsProp;
47 declareProperty(std::move(wsProp), doc);
48
49 auto indexTypePropName = IndexTypeProperty::generatePropertyName(propertyName);
50 auto indexTypeProp = std::make_unique<IndexTypeProperty>(indexTypePropName, AllowedIndexTypes);
51 const auto &indexTypePropRef = *indexTypeProp;
52
53 declareProperty(std::move(indexTypeProp), "The type of indices in the optional index set; For optimal "
54 "performance WorkspaceIndex should be preferred;");
55
56 auto indexPropName = IndexProperty::generatePropertyName(propertyName);
57 declareProperty(std::make_unique<IndexProperty>(indexPropName, wsPropRef, indexTypePropRef),
58 "An optional set of spectra that will be processed by the "
59 "algorithm; If not set, all spectra will be processed; The "
60 "indices in this list can be workspace indices or possibly "
61 "spectrum numbers, depending on the selection made for the "
62 "index type; Indices are entered as a comma-separated list "
63 "of values, and/or ranges; For example, '4,6,10-20,1000';");
64
65 m_reservedList.emplace_back(propertyName);
66 m_reservedList.emplace_back(indexTypePropName);
67 m_reservedList.emplace_back(indexPropName);
68}
69
70template <typename T1, typename T2, typename WsType>
71void Algorithm::doSetInputProperties(const std::string &name, const T1 &wksp, IndexType type, const T2 &list) {
73 throw std::runtime_error("Algorithm::setWorkspaceInputProperties can only be used "
74 "with properties declared using "
75 "declareWorkspaceInputProperties.");
76
77 auto *wsProp = dynamic_cast<WorkspaceProperty<WsType> *>(getPointerToProperty(name));
78 auto *indexTypeProp =
81
82 setWorkspaceProperty<WsType, T1>(wsProp, wksp, std::is_convertible<T1, std::shared_ptr<WsType>>());
83
84 *indexTypeProp = type;
85
86 *indexProp = list;
87}
88
98template <typename T1, typename T2, typename, typename>
99void Algorithm::setWorkspaceInputProperties(const std::string &name, const std::shared_ptr<T1> &wksp, IndexType type,
100 const T2 &list) {
101 doSetInputProperties<std::shared_ptr<T1>, T2, T1>(name, wksp, type, list);
102}
103
113template <typename T1, typename T2, typename, typename>
114void Algorithm::setWorkspaceInputProperties(const std::string &name, const std::string &wsName, IndexType type,
115 const T2 &list) {
116 doSetInputProperties<std::string, T2, T1>(name, wsName, type, list);
117}
118
125template <typename T, typename>
126std::tuple<std::shared_ptr<T>, Indexing::SpectrumIndexSet>
127Algorithm::getWorkspaceAndIndices(const std::string &name) const {
129 throw std::runtime_error("Algorithm::getWorkspacesAndIndices can only be used "
130 "with properties declared using "
131 "declareWorkspaceInputProperties.");
132
133 std::shared_ptr<T> ws = getProperty(name);
134
135 // Not able to use the regular getProperty mechanism because types, in this
136 // case SpectrumIndexSet, need to be known upfront. Since SpectrumIndexSet is
137 // not declared at a level where it can be used in Kernel, this will not work.
138 // There is an issue which has been created which may solve this and other
139 // problems related to the property mechanism in the future:
140 // https://github.com/mantidproject/mantid/issues/20092
141
143 Indexing::SpectrumIndexSet indexSet = *indexProp;
144
145 return std::make_tuple(ws, indexSet);
146}
147} // namespace API
148} // namespace Mantid
std::string name
Definition Run.cpp:60
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
void doSetInputProperties(const std::string &name, const T1 &wksp, IndexType type, const T2 &list)
Definition Algorithm.hxx:71
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
std::vector< std::string > m_reservedList
Reserved property names.
Definition Algorithm.h:522
void declareWorkspaceInputProperties(const std::string &propertyName, const std::string &doc, WSPropArgs &&...wsPropArgs)
Declare a property which defines the workspace and allowed index types, as well as a property for cap...
Definition Algorithm.hxx:42
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void setWorkspaceInputProperties(const std::string &name, const std::shared_ptr< T1 > &wksp, IndexType type, const T2 &list)
Mechanism for setting the index property with a workspace shared pointer.
Definition Algorithm.hxx:99
bool isCompoundProperty(const std::string &name) const
std::tuple< std::shared_ptr< T >, Indexing::SpectrumIndexSet > getWorkspaceAndIndices(const std::string &name) const
Mechanism for retriving the index property.
const std::string name() const override=0
function to return a name of the algorithm, must be overridden in all algorithms
IndexProperty : Implementation of a property type which returns a SpectrumIndexSet provided an input ...
static std::string generatePropertyName(const std::string &name="")
IndexTypeProperty : Implementation of a property which stores the type of input indices users require...
static std::string generatePropertyName(const std::string &name="")
A property class for workspaces.
std::string setValue(const std::string &value) override
Set the name of the workspace.
Helper class which provides the Collimation Length for SANS instruments.
@ Input
An input workspace.
Definition Property.h:53