22#ifdef PYSTON_EXPRESSIONTREEBUILDER_IMPL
29template <
unsigned pos,
typename... AN>
30struct placeholderHelper;
32template <
unsigned pos,
typename A0,
typename... AN>
33struct placeholderHelper<pos, A0, AN...> {
35 template <
typename... Prototypes>
36 static void append(boost::python::list& placeholders, Prototypes&&... prototypes) {
37 placeholders.append(
std::make_shared<Placeholder<A0>>(boost::python::len(placeholders)));
42template <
unsigned pos,
typename... AN>
45 template <
typename P1,
typename... Prototypes>
46 static void append(boost::python::list& placeholders,
const P1& p1, Prototypes&&... prototypes) {
47 placeholders.append(
std::make_shared<Placeholder<AttributeSet>>(boost::python::len(placeholders), p1));
51 template <
typename... Prototypes>
52 static void append(boost::python::list&) {
53 static_assert(
sizeof...(Prototypes) > 0,
"AttributeSet on the signature but no prototype provided");
58struct placeholderHelper<0> {
59 static void append(boost::python::list&) {}
62template <
typename R,
typename... Args>
64 const boost::python::list& placeholders) {
65 std::shared_ptr<Node<R>> root;
67 std::shared_ptr<Exception> reason;
71 auto py_comp = pyfunc(*boost::python::tuple(placeholders));
72 root = boost::python::extract<std::shared_ptr<Node<R>>>(py_comp);
75 catch (
const boost::python::error_already_set&) {
86 return ExpressionTree<R(Args...)>(compiled, root,
std::move(reason));
89template <
typename R,
typename T>
93 boost::python::list placeholders;
94 for (
size_t i = 0; i < n; ++i) {
98 return compiledOrWrapped<R, const std::vector<T>&>(pyfunc, placeholders);
101template <
typename R,
typename... Args>
102template <
typename... Prototypes>
104 Prototypes&&... prototypes) {
107 boost::python::list placeholders;
108 placeholderHelper<
sizeof...(Args),
typename std::remove_const<
typename std::remove_reference<Args>::type>::type...>::
109 append(placeholders, std::
forward<Prototypes>(prototypes)...);
111 return compiledOrWrapped<R, Args...>(pyfunc, placeholders);
114template <
typename Signature>
115struct registerHelper;
120template <
typename R,
typename... Args>
121struct registerHelper<R(Args...)> {
122 static void registerFunction(
const std::string& repr, std::function<R(Args...)> functor) {
123 auto ns = boost::python::import(
"pyston");
125 auto function = makeFunction<R(Args...)>(repr, functor);
126 boost::python::objects::add_to_namespace(ns, repr.c_str(), function);
130 boost::python::make_function(functor, boost::python::default_call_policies(), boost::mpl::vector<R, Args...>());
131 boost::python::objects::add_to_namespace(ns, repr.c_str(), direct);
138template <
typename R,
typename... Args>
139struct registerHelper<R(const
Context&, Args...)> {
140 static void registerFunction(
const std::string& repr, std::function<R(
const Context&, Args...)> functor) {
141 auto ns = boost::python::import(
"pyston");
144 boost::python::objects::add_to_namespace(ns, repr.c_str(), function);
147 auto direct = boost::python::make_function(
148 [functor](Args... v) {
150 return functor(sharedContext, v...);
152 boost::python::default_call_policies(), boost::mpl::vector<R, Args...>());
153 boost::python::objects::add_to_namespace(ns, repr.c_str(), direct);
157template <
typename Signature>
159 registerHelper<Signature>::registerFunction(repr, functor);
static ExpressionTree< R(Args...)> compiledOrWrapped(const boost::python::object &pyfunc, const boost::python::list &placeholders)
void registerFunction(const std::string &repr, std::function< Signature > functor)
PyObject * pyRecoverableError
static boost::python::object makeFunction(const std::string &repr, std::function< Signature > functor)
std::map< std::string, boost::any > Context
std::map< std::string, Attribute > AttributeSet