Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Simon Heybrock 5 : #pragma once 6 : 7 : #include <array> 8 : 9 : #include "scipp/core/dtype.h" 10 : #include "scipp/core/except.h" 11 : 12 : namespace scipp::core { 13 : 14 : template <template <class> class Callable, class... Ts, class... Args> 15 45481 : static auto callDType(const std::tuple<Ts...> &, const DType dtype, 16 : Args &&...args) { 17 45481 : std::array funcs{Callable<Ts>::apply...}; 18 45481 : std::array<DType, sizeof...(Ts)> dtypes{scipp::core::dtype<Ts>...}; 19 91431 : for (size_t i = 0; i < dtypes.size(); ++i) 20 91427 : if (dtypes[i] == dtype) 21 90561 : return funcs[i](std::forward<Args>(args)...); 22 4 : throw except::TypeError("Unsupported dtype."); 23 : } 24 : 25 : /// Apply Callable<T> to args, for any dtype T in Ts, determined by the 26 : /// runtime dtype given by `dtype`. 27 : template <class... Ts> struct CallDType { 28 : template <template <class> class Callable, class... Args> 29 43525 : static auto apply(const DType dtype, Args &&...args) { 30 86682 : return callDType<Callable>(std::tuple<Ts...>{}, dtype, 31 129721 : std::forward<Args>(args)...); 32 : } 33 : }; 34 : 35 : } // namespace scipp::core