Mantid
Loading...
Searching...
No Matches
WorkspaceCreationHelper.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/*********************************************************************************
8 * PLEASE READ THIS!!!!!!!
9 *
10 * This header MAY NOT be included in any test from a package below DataObjects
11 * (e.g. Kernel, Geometry, API).
12 * Conversely, this file (and its cpp) MAY NOT be modified to use anything from
13 *a
14 * package higher than DataObjects (e.g. any algorithm), even if via the
15 *factory.
16 *********************************************************************************/
17
18#pragma once
19
20#include <functional>
21#include <tuple>
22
24
25namespace impl {
26
27using Mantid::HistogramData::Histogram;
28using Histogram_sptr = std::shared_ptr<Histogram>;
29// A generalized function type returning a histogram.
30template <typename... Args> using HistogramFunc = std::function<Histogram_sptr(Args...)>;
31
32template <typename... Args, std::size_t... I>
33Histogram_sptr call_function_impl(HistogramFunc<Args...> f, std::tuple<Args...> args, std::index_sequence<I...>) {
34 return f(std::get<I>(args)...);
35}
36
37/*
38 * Unpack a std::tuple and present it as an argument pack to a function.
39 */
40template <typename... Args> Histogram_sptr call_function(HistogramFunc<Args...> f, std::tuple<Args...> args) {
41 return call_function_impl(f, args, std::make_index_sequence<sizeof...(Args)>());
42}
43} // namespace impl
44
52template <typename... Args>
55 std::initializer_list<std::tuple<Args...>> argss) {
56 auto ws = Mantid::API::createWorkspace<Mantid::DataObjects::Workspace2D>(
57 argss.size(), *impl::call_function(spectrumFunc, *argss.begin()));
58
59 // spectrum(0) has already been initialized
60 auto itArgs = argss.begin();
61 ++itArgs;
62 for (size_t n = 1; itArgs != argss.end(); ++itArgs, ++n) {
63 auto args = *itArgs;
64 auto test = impl::call_function(spectrumFunc, args);
65 ws->getSpectrum(n).setHistogram(*impl::call_function(spectrumFunc, args));
66 }
67
68 return ws;
69}
70
71template <typename... Args>
74 const std::vector<std::tuple<Args...>> &argss) {
75 auto ws = Mantid::API::createWorkspace<Mantid::DataObjects::Workspace2D>(
76 argss.size(), *impl::call_function(spectrumFunc, argss.front()));
77
78 // spectrum(0) has already been initialized
79 auto itArgs = argss.cbegin();
80 ++itArgs;
81 for (size_t n = 1; itArgs != argss.cend(); ++itArgs, ++n) {
82 auto args = *itArgs;
83 auto test = impl::call_function(spectrumFunc, args);
84 ws->getSpectrum(n).setHistogram(*impl::call_function(spectrumFunc, args));
85 }
86
87 return ws;
88}
89
90} // namespace WorkspaceCreationHelper
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
std::function< Histogram_sptr(Args...)> HistogramFunc
Histogram_sptr call_function(HistogramFunc< Args... > f, std::tuple< Args... > args)
std::shared_ptr< Histogram > Histogram_sptr
Histogram_sptr call_function_impl(HistogramFunc< Args... > f, std::tuple< Args... > args, std::index_sequence< I... >)
Mantid::DataObjects::Workspace2D_sptr create2DWorkspaceFromFunctionAndArgsList_(impl::HistogramFunc< Args... > spectrumFunc, const std::vector< std::tuple< Args... > > &argss)
Mantid::DataObjects::Workspace2D_sptr create2DWorkspaceFromFunctionAndArgsList(impl::HistogramFunc< Args... > spectrumFunc, std::initializer_list< std::tuple< Args... > > argss)
Creates a 2D workspace from a function object, and a list of args instantiations.