Alexandria  2.25.0
SDC-CH common library for the Euclid project
Function.icpp
Go to the documentation of this file.
1 /**
2  * @copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifdef PYSTON_GRAPH_FUNCTION_IMPL
20 
21 namespace Pyston {
22 
23 template <typename R, size_t N, typename... Evaluated>
24 struct FunctionEvalHelper {
25  template <typename Functor, typename ChildrenSet>
26  static R eval(const Context& context, const Arguments& args, const Functor& functor, const ChildrenSet& children,
27  Evaluated&&... evaluated) {
28  auto a = std::get<N - 1>(children)->eval(context, args);
29  return FunctionEvalHelper<R, N - 1, decltype(a), Evaluated...>::eval(
30  context, args, functor, children, std::forward<decltype(a)>(a), std::forward<Evaluated>(evaluated)...);
31  }
32 };
33 
34 template <typename R, typename... Evaluated>
35 struct FunctionEvalHelper<R, 0, Evaluated...> {
36  template <typename Functor, typename ChildrenSet>
37  static R eval(const Context& context, const Arguments&, const Functor& functor, const ChildrenSet&,
38  Evaluated&&... evaluated) {
39  return functor(context, std::forward<Evaluated>(evaluated)...);
40  }
41 };
42 
43 template <size_t N>
44 struct FunctionVisitHelper {
45  template <typename ChildrenSet>
46  static void visit(Visitor& visitor, const ChildrenSet& children) {
47  FunctionVisitHelper<N - 1>::visit(visitor, children);
48  std::get<N - 1>(children)->visit(visitor);
49  }
50 };
51 
52 template <>
53 struct FunctionVisitHelper<0> {
54  template <typename ChildrenSet>
55  static void visit(Visitor&, const ChildrenSet&) {}
56 };
57 
58 template <typename R, typename... Args>
59 R Function<R, Args...>::eval(const Context& context, const Arguments& args) const {
60  return FunctionEvalHelper<R, sizeof...(Args)>::eval(context, args, m_functor, m_children);
61 }
62 
63 template <typename R, typename... Args>
64 void Function<R, Args...>::visit(Visitor& visitor) const {
65  visitor.enter(this);
66  FunctionVisitHelper<sizeof...(Args)>::visit(visitor, m_children);
67  visitor.exit(this);
68 }
69 
70 } // end of namespace Pyston
71 
72 #endif