Chris@16: // Copyright David Abrahams 2003. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: #ifndef PURE_VIRTUAL_DWA2003810_HPP Chris@16: # define PURE_VIRTUAL_DWA2003810_HPP Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: Chris@16: namespace boost { namespace python { Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: // Chris@16: // @group Helpers for pure_virtual_visitor. { Chris@16: // Chris@16: Chris@16: // Raises a Python RuntimeError reporting that a pure virtual Chris@16: // function was called. Chris@16: void BOOST_PYTHON_DECL pure_virtual_called(); Chris@16: Chris@16: // Replace the two front elements of S with T1 and T2 Chris@16: template Chris@16: struct replace_front2 Chris@16: { Chris@16: // Metafunction forwarding seemed to confound vc6 Chris@16: typedef typename mpl::push_front< Chris@16: typename mpl::push_front< Chris@16: typename mpl::pop_front< Chris@16: typename mpl::pop_front< Chris@16: S Chris@16: >::type Chris@16: >::type Chris@16: , T2 Chris@16: >::type Chris@16: , T1 Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: // Given an MPL sequence representing a member function [object] Chris@16: // signature, returns a new MPL sequence whose return type is Chris@16: // replaced by void, and whose first argument is replaced by C&. Chris@16: template Chris@16: typename replace_front2::type Chris@16: error_signature(S BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(C)) Chris@16: { Chris@16: typedef typename replace_front2::type r; Chris@16: return r(); Chris@16: } Chris@16: Chris@16: // Chris@16: // } Chris@16: // Chris@16: Chris@16: // Chris@16: // A def_visitor which defines a method as usual, then adds a Chris@16: // corresponding function which raises a "pure virtual called" Chris@16: // exception unless it's been overridden. Chris@16: // Chris@16: template Chris@16: struct pure_virtual_visitor Chris@16: : def_visitor > Chris@16: { Chris@16: pure_virtual_visitor(PointerToMemberFunction pmf) Chris@16: : m_pmf(pmf) Chris@16: {} Chris@16: Chris@16: private: Chris@16: friend class python::def_visitor_access; Chris@16: Chris@16: template Chris@16: void visit(C_& c, char const* name, Options& options) const Chris@16: { Chris@16: // This should probably be a nicer error message Chris@16: BOOST_STATIC_ASSERT(!Options::has_default_implementation); Chris@16: Chris@16: // Add the virtual function dispatcher Chris@16: c.def( Chris@16: name Chris@16: , m_pmf Chris@16: , options.doc() Chris@16: , options.keywords() Chris@16: , options.policies() Chris@16: ); Chris@16: Chris@16: typedef BOOST_DEDUCED_TYPENAME C_::metadata::held_type held_type; Chris@16: Chris@16: // Add the default implementation which raises the exception Chris@16: c.def( Chris@16: name Chris@16: , make_function( Chris@16: detail::nullary_function_adaptor(pure_virtual_called) Chris@16: , default_call_policies() Chris@16: , detail::error_signature(detail::get_signature(m_pmf)) Chris@16: ) Chris@16: ); Chris@16: } Chris@16: Chris@16: private: // data members Chris@16: PointerToMemberFunction m_pmf; Chris@16: }; Chris@16: } Chris@16: Chris@16: // Chris@16: // Passed a pointer to member function, generates a def_visitor which Chris@16: // creates a method that only dispatches to Python if the function has Chris@16: // been overridden, either in C++ or in Python, raising a "pure Chris@16: // virtual called" exception otherwise. Chris@16: // Chris@16: template Chris@16: detail::pure_virtual_visitor Chris@16: pure_virtual(PointerToMemberFunction pmf) Chris@16: { Chris@16: return detail::pure_virtual_visitor(pmf); Chris@16: } Chris@16: Chris@16: }} // namespace boost::python Chris@16: Chris@16: #endif // PURE_VIRTUAL_DWA2003810_HPP