Alexandria  2.27.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Helpers.h
Go to the documentation of this file.
1 
19 #ifndef PYSTON_HELPERS_H
20 #define PYSTON_HELPERS_H
21 
22 #include "Pyston/Graph/Cast.h"
23 #include "Pyston/Graph/Function.h"
24 #include <boost/python.hpp>
25 #include <boost/mpl/vector.hpp>
26 #include <memory>
27 
28 namespace Pyston {
29 
30 template <typename Signature>
32 
36 template <typename R, typename... Args>
37 struct makeFunctionHelper<R(const Context&, Args...)> {
38  static boost::python::object make(const std::string& repr, std::function<R(const Context&, Args...)> functor) {
39  return boost::python::make_function(
40  FunctionFactory<R(Args...)>(repr, functor), boost::python::default_call_policies(),
41  boost::mpl::vector<std::shared_ptr<Node<R>>, const std::shared_ptr<Node<Args>>&...>());
42  }
43 };
44 
49 template <typename R, typename... Args>
50 struct makeFunctionHelper<R(Args...)> {
51  static boost::python::object make(const std::string& repr, std::function<R(Args...)> functor) {
52  auto wrapped = [functor](const Context&, Args... args) { return functor(args...); };
53  return boost::python::make_function(
54  FunctionFactory<R(Args...)>(repr, wrapped), boost::python::default_call_policies(),
55  boost::mpl::vector<std::shared_ptr<Node<R>>, const std::shared_ptr<Node<Args>>&...>());
56  }
57 };
58 
59 template <typename Signature>
61 
70 template <typename R, typename T>
71 struct makeBinaryFunctionHelper<R(T, T)> {
72 
73  static boost::python::object make(const std::string& repr, std::function<R(T, T)> functor, bool reversed) {
74  FunctionFactory<R(T, T)> factory(repr, [functor](const Context&, T l, T r) { return functor(l, r); });
75 
76  return boost::python::make_function(
77  [factory, reversed](const std::shared_ptr<Node<T>>& left, const std::shared_ptr<Node<T>>& right) {
78  if (reversed)
79  return factory(right, left);
80  return factory(left, right);
81  },
82  boost::python::default_call_policies(),
83  boost::mpl::vector<std::shared_ptr<Node<R>>, const std::shared_ptr<Node<T>>&,
84  const std::shared_ptr<Node<T>>&>());
85  }
86 };
87 
103 template <typename Signature>
104 static boost::python::object makeFunction(const std::string& repr, std::function<Signature> functor) {
105  return makeFunctionHelper<Signature>::make(repr, functor);
106 }
107 
123 template <typename Signature>
124 static boost::python::object makeBinaryFunction(const std::string& repr, std::function<Signature> functor,
125  bool reversed = false) {
126  return makeBinaryFunctionHelper<Signature>::make(repr, functor, reversed);
127 }
128 
129 } // namespace Pyston
130 
131 #endif // PYSTON_HELPERS_H
static boost::python::object makeBinaryFunction(const std::string &repr, std::function< Signature > functor, bool reversed=false)
Definition: Helpers.h:124
static boost::python::object makeFunction(const std::string &repr, std::function< Signature > functor)
Definition: Helpers.h:104
STL class.
STL class.
static boost::python::object make(const std::string &repr, std::function< R(const Context &, Args...)> functor)
Definition: Helpers.h:38
static boost::python::object make(const std::string &repr, std::function< R(T, T)> functor, bool reversed)
Definition: Helpers.h:73
static boost::python::object make(const std::string &repr, std::function< R(Args...)> functor)
Definition: Helpers.h:51