Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
Meta.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <cstddef>
22 #include <utility>
23 
24 namespace Reaktoro {
25 namespace detail {
26 
27 template<size_t i>
28 struct Counter
29 {
30  constexpr static size_t value = i;
31  constexpr operator const size_t() const { return value; }
32  constexpr operator size_t() { return value; }
33 };
34 
35 template<size_t i, size_t ibegin, size_t iend, typename Function>
36 constexpr auto AuxFor(Function&& f)
37 {
38  if constexpr (i < iend) {
39  f(Counter<i>{});
40  AuxFor<i + 1, ibegin, iend>(std::forward<Function>(f));
41  }
42 }
43 
44 } // namespace detail
45 
47 template<size_t ibegin, size_t iend, typename Function>
48 constexpr auto For(Function&& f)
49 {
50  detail::AuxFor<ibegin, ibegin, iend>(std::forward<Function>(f));
51 }
52 
54 template<size_t iend, typename Function>
55 constexpr auto For(Function&& f)
56 {
57  For<0, iend>(std::forward<Function>(f));
58 }
59 
61 template<typename Function>
62 constexpr auto ForEach(Function const& f) -> void
63 {
64 }
65 
67 template<typename Function, typename Arg, typename... Args>
68 constexpr auto ForEach(Function const& f, Arg const& arg, Args const&... args) -> void
69 {
70  f(arg);
71  ForEach(f, args...);
72 }
73 
74 } // namespace Reaktoro
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
constexpr auto ForEach(Function const &f) -> void
Generate evaluation statements f(arg0); f(arg1); ...; f(argn); at compile time.
Definition: Meta.hpp:62
constexpr auto For(Function &&f)
Generate evaluation statements f(ibegin); f(ibegin + 1); ...; f(iend-1); at compile time.
Definition: Meta.hpp:48
Definition: Meta.hpp:29