/* PyEO Copyright (C) 2003 Maarten Keijzer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef MAKE_ABSTRACT_FUNCTOR_H #define MAKE_ABSTRACT_FUNCTOR_H #include // DEFINES for call #define WC1 boost::python::with_custodian_and_ward<1,2>() #define WC2 boost::python::with_custodian_and_ward<1,2, with_custodian_and_ward<1,3> >() namespace eoutils { using namespace boost::python; template class ProcWrapper : public Proc { public: PyObject* self; ProcWrapper(PyObject* s) : self(s) {} typename Proc::result_type operator()(void) { return boost::python::call_method(self, "__call__"); } }; template void make_abstract_functor(std::string name, typename eoFunctorBase::procedure_tag) { typedef ProcWrapper Wrapper; boost::python::class_(name.c_str(), boost::python::init<>() ) .def("__call__", &Wrapper::operator()); } template void make_abstract_functor_ref(std::string name, typename eoFunctorBase::procedure_tag) { typedef ProcWrapper Wrapper; boost::python::class_(name.c_str(), boost::python::init<>() ) .def("__call__", &Wrapper::operator(), boost::python::return_internal_reference<>()); } template class UnaryWrapper : public Unary { public: PyObject* self; UnaryWrapper(PyObject* s) : self(s) {} typename Unary::result_type operator()(typename Unary::argument_type a) { return boost::python::call_method(self, "__call__", boost::ref(a) ); } }; template void make_abstract_functor(std::string name, typename eoFunctorBase::unary_function_tag) { typedef UnaryWrapper Wrapper; boost::python::class_(name.c_str(), boost::python::init<>() ) .def("__call__", &Wrapper::operator()) ; } template void make_abstract_functor_ref(std::string name, typename eoFunctorBase::unary_function_tag) { typedef UnaryWrapper Wrapper; boost::python::class_(name.c_str(), boost::python::init<>() ) .def("__call__", &Wrapper::operator(), boost::python::return_internal_reference<>() ) ; } template class BinaryWrapper : public Binary { public: PyObject* self; BinaryWrapper(PyObject* s) : self(s) {} typename Binary::result_type operator()(typename Binary::first_argument_type a1, typename Binary::second_argument_type a2) { return boost::python::call_method< typename Binary::result_type>(self, "__call__", boost::ref(a1), boost::ref(a2) ); } }; template void make_abstract_functor(std::string name, typename eoFunctorBase::binary_function_tag) { typedef BinaryWrapper Wrapper; boost::python::class_(name.c_str(), boost::python::init<>() ) .def("__call__", &Wrapper::operator()); } template void make_abstract_functor_ref(std::string name, typename eoFunctorBase::binary_function_tag) { typedef BinaryWrapper Wrapper; boost::python::class_(name.c_str(), boost::python::init<>() ) .def("__call__", &Wrapper::operator(), boost::python::return_internal_reference<>() ); } }// namespace eoutils template void def_abstract_functor(std::string name) { eoutils::make_abstract_functor(name, Functor::functor_category()); } template void def_abstract_functor_ref(std::string name) { eoutils::make_abstract_functor_ref(name, Functor::functor_category()); } #endif