|
| 1 | +// Copyright David Abrahams 2002. 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 CLASS_DWA200216_HPP |
| 7 | +# define CLASS_DWA200216_HPP |
| 8 | + |
| 9 | +# include <boost/python/module.hpp> |
| 10 | +# include <boost/python/reference.hpp> |
| 11 | +# include <boost/python/object/class.hpp> |
| 12 | +# include <boost/python/converter/type_id.hpp> |
| 13 | +# include <boost/python/detail/wrap_function.hpp> |
| 14 | +# include <boost/mpl/type_list.hpp> |
| 15 | +# include <boost/python/object/class_converters.hpp> |
| 16 | +# include <boost/mpl/size.hpp> |
| 17 | +# include <boost/mpl/for_each.hpp> |
| 18 | +# include <boost/mpl/type_list.hpp> |
| 19 | + |
| 20 | +namespace // put some convenience classes into the unnamed namespace for the user |
| 21 | +{ |
| 22 | + // A type list for specifying bases |
| 23 | + template < BOOST_MPL_LIST_DEFAULT_PARAMETERS(typename B, ::boost::mpl::null_argument) > |
| 24 | + struct bases : ::boost::mpl::type_list< BOOST_MPL_LIST_PARAMETERS(B) >::type |
| 25 | + {}; |
| 26 | + |
| 27 | + // A type list for specifying arguments |
| 28 | + template < BOOST_MPL_LIST_DEFAULT_PARAMETERS(typename A, ::boost::mpl::null_argument) > |
| 29 | + struct args : ::boost::mpl::type_list< BOOST_MPL_LIST_PARAMETERS(A) >::type |
| 30 | + {}; |
| 31 | +} |
| 32 | + |
| 33 | +namespace boost { namespace python { |
| 34 | + |
| 35 | +// Forward declarations |
| 36 | +namespace objects |
| 37 | +{ |
| 38 | + struct value_holder_generator; |
| 39 | +} |
| 40 | + |
| 41 | +namespace detail |
| 42 | +{ |
| 43 | + // This is an mpl BinaryMetaFunction object with a runtime behavior, |
| 44 | + // which is to write the id of the type which is passed as its 2nd |
| 45 | + // compile-time argument into the iterator pointed to by its runtime |
| 46 | + // argument |
| 47 | + struct write_type_id |
| 48 | + { |
| 49 | + // The first argument is Ignored because mpl::for_each is still |
| 50 | + // currently an accumulate (reduce) implementation. |
| 51 | + template <class Ignored, class T> struct apply |
| 52 | + { |
| 53 | + // also an artifact of accumulate-based for_each |
| 54 | + typedef void type; |
| 55 | + |
| 56 | + // Here's the runtime behavior |
| 57 | + static void execute(converter::undecorated_type_id_t** p) |
| 58 | + { |
| 59 | + *(*p)++ = converter::undecorated_type_id<T>(); |
| 60 | + } |
| 61 | + }; |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +// |
| 66 | +// class_<T,Bases,HolderGenerator> |
| 67 | +// |
| 68 | +// This is the primary mechanism through which users will expose |
| 69 | +// C++ classes to Python. The three template arguments are: |
| 70 | +// |
| 71 | +// T - The class being exposed to Python |
| 72 | +// |
| 73 | +// Bases - An MPL sequence of base classes |
| 74 | +// |
| 75 | +// HolderGenerator - |
| 76 | +// An optional type generator for the "holder" which |
| 77 | +// maintains the C++ object inside the Python instance. The |
| 78 | +// default just holds the object "by-value", but other |
| 79 | +// holders can be substituted which will hold the C++ object |
| 80 | +// by smart pointer, for example. |
| 81 | +// |
| 82 | +template < |
| 83 | + class T // class being wrapped |
| 84 | + , class Bases = mpl::type_list<>::type |
| 85 | + , class HolderGenerator = objects::value_holder_generator |
| 86 | + > |
| 87 | +class class_ : objects::class_base |
| 88 | +{ |
| 89 | + typedef class_<T,Bases,HolderGenerator> self; |
| 90 | + public: |
| 91 | + |
| 92 | + // Construct with the module and class name |
| 93 | + class_(module&, char const* name); |
| 94 | + |
| 95 | + // Wrap a member function or a non-member function which can take |
| 96 | + // a T, T cv&, or T cv* as its first parameter, or a callable |
| 97 | + // python object. |
| 98 | + template <class F> |
| 99 | + self& def(F f, char const* name) |
| 100 | + { |
| 101 | + // Use function::add_to_namespace to achieve overloading if |
| 102 | + // appropriate. |
| 103 | + objects::function::add_to_namespace(this->object(), name, detail::wrap_function(f)); |
| 104 | + return *this; |
| 105 | + } |
| 106 | + |
| 107 | + // Define the constructor with the given Args, which should be an |
| 108 | + // MPL sequence of types. |
| 109 | + template <class Args> |
| 110 | + self& def_init(Args const& = Args()) |
| 111 | + { |
| 112 | + def(make_constructor<T,Args,HolderGenerator>(), "__init__"); |
| 113 | + return *this; |
| 114 | + } |
| 115 | + |
| 116 | + // Define the default constructor. |
| 117 | + self& def_init() |
| 118 | + { |
| 119 | + this->def_init(mpl::type_list<>::type()); |
| 120 | + return *this; |
| 121 | + } |
| 122 | + |
| 123 | + private: // types |
| 124 | + typedef objects::class_id class_id; |
| 125 | + |
| 126 | + // A helper class which will contain an array of id objects to be |
| 127 | + // passed to the base class constructor |
| 128 | + struct id_vector |
| 129 | + { |
| 130 | + id_vector() |
| 131 | + { |
| 132 | + // Stick the derived class id into the first element of the array |
| 133 | + ids[0] = converter::undecorated_type_id<T>(); |
| 134 | + |
| 135 | + // Write the rest of the elements into succeeding positions. |
| 136 | + class_id* p = ids + 1; |
| 137 | + mpl::for_each<Bases, void, detail::write_type_id>::execute(&p); |
| 138 | + } |
| 139 | + |
| 140 | + BOOST_STATIC_CONSTANT( |
| 141 | + std::size_t, size = mpl::size<Bases>::value + 1); |
| 142 | + class_id ids[size]; |
| 143 | + }; |
| 144 | + |
| 145 | + private: // helper functions |
| 146 | + void initialize_converters(); |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | +// |
| 151 | +// implementations |
| 152 | +// |
| 153 | +template <class T, class Bases, class HolderGenerator> |
| 154 | +inline class_<T, Bases, HolderGenerator>::class_( |
| 155 | + module& m, char const* name = typeid(T).name()) |
| 156 | + : class_base(m, name, id_vector::size, id_vector().ids) |
| 157 | +{ |
| 158 | + // Bring the class converters into existence. This static object |
| 159 | + // will survive until the shared library this module lives in is |
| 160 | + // unloaded (that doesn't happen until Python terminates). |
| 161 | + static objects::class_converters<T,Bases> converters(object()); |
| 162 | +} |
| 163 | + |
| 164 | +}} // namespace boost::python |
| 165 | + |
| 166 | +#endif // CLASS_DWA200216_HPP |
0 commit comments