From 7e76c855356009ceb5d589a9fe8bcb382169d498 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Fri, 8 Mar 2002 16:13:32 +0000 Subject: [PATCH] initial checkin [SVN r13139] --- include/boost/python/converter/callback.hpp | 113 ++++++++++++++ include/boost/python/returning.hpp | 159 ++++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100644 include/boost/python/converter/callback.hpp create mode 100644 include/boost/python/returning.hpp diff --git a/include/boost/python/converter/callback.hpp b/include/boost/python/converter/callback.hpp new file mode 100644 index 00000000..3e1e462e --- /dev/null +++ b/include/boost/python/converter/callback.hpp @@ -0,0 +1,113 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +#ifndef CALLBACK_DWA2002228_HPP +# define CALLBACK_DWA2002228_HPP + +# include +# include +# include +# include +# include +# include +# include + +namespace boost { namespace python { namespace converter { + +namespace detail +{ + template + struct pointer_callback_from_python + { + pointer_callback_from_python(); + T operator()(PyObject*) const; + }; + + template + struct reference_callback_from_python + { + reference_callback_from_python(); + T operator()(PyObject*) const; + }; + + template + struct rvalue_callback_from_python + { + rvalue_callback_from_python(); + T const& operator()(PyObject*); + private: + rvalue_data m_data; + }; + + template + struct select_callback_from_python + { + BOOST_STATIC_CONSTANT( + bool, ptr = is_pointer::value); + + BOOST_STATIC_CONSTANT( + bool, ref = is_reference::value); + + typedef typename mpl::select_type< + ptr + , pointer_callback_from_python + , typename mpl::select_type< + ref + , reference_callback_from_python + , rvalue_callback_from_python + >::type + >::type type; + }; + + + template + struct reference_callback_to_python + { + + }; +} + +template +struct callback_from_python + : detail::select_callback_from_python::type +{ +}; + +template +struct callback_to_python : detail::callback_to_python_base +{ + public: // member functions + // Throw an exception if the conversion can't succeed + callback_to_python(T const&); +}; + +// +// Implementations +// +namespace detail +{ + template + rvalue_callback_from_python::rvalue_callback_from_python() + : m_data(rvalue_from_python_chain::value) + { + throw_if_not_registered(m_data.stage1); + } + + template + T const& rvalue_callback_from_python::operator()(PyObject* obj) + { + return *(T*)convert_rvalue(obj, m_data.stage1, m_data.storage.bytes); + } +} + +template +callback_to_python::callback_to_python(T const& x) + : callback_to_python_base(&x, to_python_function::value) +{ +} + +}}} // namespace boost::python::converter + +#endif // CALLBACK_DWA2002228_HPP diff --git a/include/boost/python/returning.hpp b/include/boost/python/returning.hpp new file mode 100644 index 00000000..c34f97f6 --- /dev/null +++ b/include/boost/python/returning.hpp @@ -0,0 +1,159 @@ +// (C) Copyright David Abrahams 2001,2002. Permission to copy, use, modify, sell +// and distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// This work was funded in part by Lawrence Berkeley National Labs +// +// This file generated for 5-argument member functions and 6-argument free +// functions by gen_returning.py + +#ifndef RETURNING_DWA20020228_HPP +# define RETURNING_DWA20020228_HPP + +# include +# include +# include +# include + +namespace boost { namespace python { + +// Calling C++ from Python +template +struct returning +{ + static R call(char const* name, PyObject* self) + { + converter::callback_from_python cr; + return cr(PyEval_CallMethod( + self + , const_cast(name) + , const_cast("()") + )); + } + + static R call(PyObject* self) + { + converter::callback_from_python cr; + return cr(PyEval_CallFunction(self, const_cast("()") + )); + } + + template + static R call_method(PyObject* self, const char* name, A1 const& a1) + { + converter::callback_from_python cr; + return cr(PyEval_CallMethod( + self + , const_cast(name) + , const_cast("(O)") + , converter::callback_to_python(a1).get() + )); + } + + template + static R call(PyObject* self, A1 const& a1) + { + converter::callback_from_python cr; + return cr(PyEval_CallFunction( + self + , const_cast("(O)") + , converter::callback_to_python(a1).get() + )); + } + + template + static R call_method(PyObject* self, const char* name, A1 const& a1, A2 const& a2) + { + converter::callback_from_python cr; + return cr(PyEval_CallMethod( + self + , const_cast(name) + , const_cast("(O)") + , converter::callback_to_python(a1).get() + , converter::callback_to_python(a2).get() + )); + } + + template + static R call(PyObject* self, A1 const& a1, A2 const& a2) + { + converter::callback_from_python cr; + return cr(PyEval_CallFunction( + self + , const_cast("(O)") + , converter::callback_to_python(a1).get() + , converter::callback_to_python(a2).get() + )); + } + +}; + +template <> +struct returning +{ + typedef void R; + static R call(char const* name, PyObject* self) + { + ref x(PyEval_CallMethod( + self + , const_cast(name) + , const_cast("()") + )); + } + + static R call(PyObject* self) + { + ref x(PyEval_CallFunction(self, const_cast("()") + )); + } + + template + static R call_method(PyObject* self, const char* name, A1 const& a1) + { + ref x(PyEval_CallMethod( + self + , const_cast(name) + , const_cast("(O)") + , converter::callback_to_python(a1).get() + )); + } + + template + static R call(PyObject* self, A1 const& a1) + { + ref x(PyEval_CallFunction( + self + , const_cast("(O)") + , converter::callback_to_python(a1).get() + )); + } + + template + static R call_method(PyObject* self, const char* name, A1 const& a1, A2 const& a2) + { + ref x(PyEval_CallMethod( + self + , const_cast(name) + , const_cast("(O)") + , converter::callback_to_python(a1).get() + , converter::callback_to_python(a2).get() + )); + } + + template + static R call(PyObject* self, A1 const& a1, A2 const& a2) + { + ref x(PyEval_CallFunction( + self + , const_cast("(O)") + , converter::callback_to_python(a1).get() + , converter::callback_to_python(a2).get() + )); + } +}; + +}} // namespace boost::python + +#endif // RETURNING_DWA20020228_HPP +