|
| 1 | +// Copyright David Abrahams 2003. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#ifndef PURE_VIRTUAL_DWA2003810_HPP |
| 7 | +# define PURE_VIRTUAL_DWA2003810_HPP |
| 8 | + |
| 9 | +# include <boost/python/def_visitor.hpp> |
| 10 | +# include <boost/python/default_call_policies.hpp> |
| 11 | +# include <boost/python/arg_from_python.hpp> |
| 12 | +# include <boost/mpl/push_front.hpp> |
| 13 | +# include <boost/mpl/pop_front.hpp> |
| 14 | + |
| 15 | +# include <boost/python/detail/nullary_function_adaptor.hpp> |
| 16 | + |
| 17 | +namespace boost { namespace python { |
| 18 | + |
| 19 | +namespace detail |
| 20 | +{ |
| 21 | + // |
| 22 | + // { Helpers for pure_virtual_visitor, below. |
| 23 | + // |
| 24 | + |
| 25 | + // Raises a Python RuntimeError reporting that a pure virtual |
| 26 | + // function was called. |
| 27 | + void BOOST_PYTHON_DECL pure_virtual_called(); |
| 28 | + |
| 29 | + // Replace the two front elements of S with T1 and T2 |
| 30 | + template <class S, class T1, class T2> |
| 31 | + struct replace_front2 |
| 32 | + { |
| 33 | + // Metafunction forwarding seemed to confound vc6 |
| 34 | + typedef typename mpl::push_front< |
| 35 | + typename mpl::push_front< |
| 36 | + typename mpl::pop_front< |
| 37 | + typename mpl::pop_front< |
| 38 | + S |
| 39 | + >::type |
| 40 | + >::type |
| 41 | + , T2 |
| 42 | + >::type |
| 43 | + , T1 |
| 44 | + >::type type; |
| 45 | + }; |
| 46 | + |
| 47 | + // Given an MPL sequence representing a signature, returns a new MPL |
| 48 | + // sequence whose return type is replaced by void, and whose first |
| 49 | + // argument is replaced by C&. |
| 50 | + template <class C, class S> |
| 51 | + typename replace_front2<S,void,C&>::type |
| 52 | + error_signature(S BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(C)) |
| 53 | + { |
| 54 | + typedef typename replace_front2<S,void,C&>::type r; |
| 55 | + return r(); |
| 56 | + } |
| 57 | + |
| 58 | + // |
| 59 | + // } Helpers for pure_virtual_visitor |
| 60 | + // |
| 61 | + |
| 62 | + // |
| 63 | + // A def_visitor which defines a method as usual, then adds a |
| 64 | + // corresponding function which raises a "pure virtual called" |
| 65 | + // exception unless it's been overridden. |
| 66 | + // |
| 67 | + template <class PointerToMemberFunction> |
| 68 | + struct pure_virtual_visitor |
| 69 | + : def_visitor<pure_virtual_visitor<PointerToMemberFunction> > |
| 70 | + { |
| 71 | + pure_virtual_visitor(PointerToMemberFunction pmf) |
| 72 | + : m_pmf(pmf) |
| 73 | + {} |
| 74 | + |
| 75 | + private: |
| 76 | + friend class def_visitor_access; |
| 77 | + |
| 78 | + template <class C_, class Options> |
| 79 | + void visit(C_& c, char const* name, Options& options) const |
| 80 | + { |
| 81 | + // This should probably be a nicer error message |
| 82 | + BOOST_STATIC_ASSERT(!Options::has_default_implementation); |
| 83 | + |
| 84 | + // Add the virtual function dispatcher |
| 85 | + c.def( |
| 86 | + name |
| 87 | + , m_pmf |
| 88 | + , options.doc() |
| 89 | + , options.keywords() |
| 90 | + , options.policies() |
| 91 | + ); |
| 92 | + |
| 93 | + typedef BOOST_DEDUCED_TYPENAME C_::select_holder::held_type held_t; |
| 94 | + |
| 95 | + // Add the default implementation which raises the exception |
| 96 | + c.def( |
| 97 | + name |
| 98 | + , detail::make_function_aux( |
| 99 | + detail::nullary_function_adaptor<void(*)()>(pure_virtual_called) |
| 100 | + , default_call_policies() |
| 101 | + , args_from_python() |
| 102 | + , detail::error_signature<held_t>(detail::get_signature(m_pmf)) |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + private: // data members |
| 108 | + PointerToMemberFunction m_pmf; |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +// |
| 113 | +// Passed a pointer to member function, generates a def_visitor which |
| 114 | +// creates a method that only dispatches to Python if the function has |
| 115 | +// been overridden, either in C++ or in Python, raising a "pure |
| 116 | +// virtual called" exception otherwise. |
| 117 | +// |
| 118 | +template <class PointerToMemberFunction> |
| 119 | +detail::pure_virtual_visitor<PointerToMemberFunction> |
| 120 | +pure_virtual(PointerToMemberFunction pmf) |
| 121 | +{ |
| 122 | + return detail::pure_virtual_visitor<PointerToMemberFunction>(pmf); |
| 123 | +} |
| 124 | + |
| 125 | +}} // namespace boost::python |
| 126 | + |
| 127 | +#endif // PURE_VIRTUAL_DWA2003810_HPP |
0 commit comments