ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
integral_parameter.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21#ifndef ESPRESSO_INTEGRAL_PARAMETER_HPP
22#define ESPRESSO_INTEGRAL_PARAMETER_HPP
23
24#include <cstddef>
25#include <stdexcept>
26#include <utility>
27
28namespace Utils {
29namespace detail {
30template <typename T, template <T> class F, T I, T N>
31struct integral_parameter_impl {
32 template <class... Args> static decltype(auto) eval(T i, Args &&...args) {
33 if (i == I)
34 return F<I>{}(std::forward<Args>(args)...);
35
36 return integral_parameter_impl<T, F, I + T{1}, N>::eval(
37 i, std::forward<Args>(args)...);
38 }
39};
40
41template <typename T, template <T> class F, T N>
42struct integral_parameter_impl<T, F, N, N> {
43 template <class... Args> static decltype(auto) eval(T i, Args &&...args) {
44 if (i == N)
45 return F<N>{}(std::forward<Args>(args)...);
46
47 throw std::runtime_error("Invalid parameter value");
48 }
49};
50} // namespace detail
51
52/**
53 * @brief Generate a call table for an integral non-type template parameter.
54 * Can be used to dynamically create a switch statement for contiguous values.
55 */
56template <typename T, template <T> class F, T min, T max, class... Args>
57decltype(auto) integral_parameter(T i, Args &&...args) {
58 return detail::integral_parameter_impl<T, F, min, max>::eval(
59 i, std::forward<Args>(args)...);
60}
61
62} // namespace Utils
63
64#endif // ESPRESSO_INTEGRAL_PARAMETER_HPP
float N[3]
decltype(auto) integral_parameter(T i, Args &&...args)
Generate a call table for an integral non-type template parameter.
int min(int i, unsigned int j)