class_metadata.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright David Abrahams 2004. Distributed under the Boost
  2. // Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef CLASS_METADATA_DWA2004719_HPP
  5. # define CLASS_METADATA_DWA2004719_HPP
  6. # include <boost/python/converter/shared_ptr_from_python.hpp>
  7. # include <boost/python/object/inheritance.hpp>
  8. # include <boost/python/object/class_wrapper.hpp>
  9. # include <boost/python/object/make_instance.hpp>
  10. # include <boost/python/object/value_holder.hpp>
  11. # include <boost/python/object/pointer_holder.hpp>
  12. # include <boost/python/object/make_ptr_instance.hpp>
  13. # include <boost/python/detail/force_instantiate.hpp>
  14. # include <boost/python/detail/not_specified.hpp>
  15. # include <boost/python/has_back_reference.hpp>
  16. # include <boost/python/bases.hpp>
  17. # include <boost/type_traits/add_pointer.hpp>
  18. # include <boost/type_traits/is_convertible.hpp>
  19. # include <boost/type_traits/is_polymorphic.hpp>
  20. # include <boost/mpl/if.hpp>
  21. # include <boost/mpl/eval_if.hpp>
  22. # include <boost/mpl/bool.hpp>
  23. # include <boost/mpl/or.hpp>
  24. # include <boost/mpl/identity.hpp>
  25. # include <boost/mpl/for_each.hpp>
  26. # include <boost/mpl/placeholders.hpp>
  27. # include <boost/mpl/single_view.hpp>
  28. # include <boost/mpl/assert.hpp>
  29. # include <boost/type_traits/is_same.hpp>
  30. # include <boost/type_traits/is_convertible.hpp>
  31. # include <boost/noncopyable.hpp>
  32. # include <boost/detail/workaround.hpp>
  33. namespace boost { namespace python { namespace objects {
  34. BOOST_PYTHON_DECL
  35. void copy_class_object(type_info const& src, type_info const& dst);
  36. //
  37. // Support for registering base/derived relationships
  38. //
  39. template <class Derived>
  40. struct register_base_of
  41. {
  42. template <class Base>
  43. inline void operator()(Base*) const
  44. {
  45. BOOST_MPL_ASSERT_NOT((is_same<Base,Derived>));
  46. // Register the Base class
  47. register_dynamic_id<Base>();
  48. // Register the up-cast
  49. register_conversion<Derived,Base>(false);
  50. // Register the down-cast, if appropriate.
  51. this->register_downcast((Base*)0, is_polymorphic<Base>());
  52. }
  53. private:
  54. static inline void register_downcast(void*, mpl::false_) {}
  55. template <class Base>
  56. static inline void register_downcast(Base*, mpl::true_)
  57. {
  58. register_conversion<Base, Derived>(true);
  59. }
  60. };
  61. //
  62. // Preamble of register_class. Also used for callback classes, which
  63. // need some registration of their own.
  64. //
  65. template <class T, class Bases>
  66. inline void register_shared_ptr_from_python_and_casts(T*, Bases)
  67. {
  68. // Constructor performs registration
  69. python::detail::force_instantiate(converter::shared_ptr_from_python<T>());
  70. //
  71. // register all up/downcasts here. We're using the alternate
  72. // interface to mpl::for_each to avoid an MSVC 6 bug.
  73. //
  74. register_dynamic_id<T>();
  75. mpl::for_each(register_base_of<T>(), (Bases*)0, (add_pointer<mpl::_>*)0);
  76. }
  77. //
  78. // Helper for choosing the unnamed held_type argument
  79. //
  80. template <class T, class Prev>
  81. struct select_held_type
  82. : mpl::if_<
  83. mpl::or_<
  84. python::detail::specifies_bases<T>
  85. , is_same<T,noncopyable>
  86. >
  87. , Prev
  88. , T
  89. >
  90. {
  91. };
  92. template <
  93. class T // class being wrapped
  94. , class X1 // = detail::not_specified
  95. , class X2 // = detail::not_specified
  96. , class X3 // = detail::not_specified
  97. >
  98. struct class_metadata
  99. {
  100. //
  101. // Calculate the unnamed template arguments
  102. //
  103. // held_type_arg -- not_specified, [a class derived from] T or a
  104. // smart pointer to [a class derived from] T. Preserving
  105. // not_specified allows us to give class_<T,T> a back-reference.
  106. typedef typename select_held_type<
  107. X1
  108. , typename select_held_type<
  109. X2
  110. , typename select_held_type<
  111. X3
  112. , python::detail::not_specified
  113. >::type
  114. >::type
  115. >::type held_type_arg;
  116. // bases
  117. typedef typename python::detail::select_bases<
  118. X1
  119. , typename python::detail::select_bases<
  120. X2
  121. , typename python::detail::select_bases<
  122. X3
  123. , python::bases<>
  124. >::type
  125. >::type
  126. >::type bases;
  127. typedef mpl::or_<
  128. is_same<X1,noncopyable>
  129. , is_same<X2,noncopyable>
  130. , is_same<X3,noncopyable>
  131. > is_noncopyable;
  132. //
  133. // Holder computation.
  134. //
  135. // Compute the actual type that will be held in the Holder.
  136. typedef typename mpl::if_<
  137. is_same<held_type_arg,python::detail::not_specified>, T, held_type_arg
  138. >::type held_type;
  139. // Determine if the object will be held by value
  140. typedef mpl::bool_<is_convertible<held_type*,T*>::value> use_value_holder;
  141. // Compute the "wrapped type", that is, if held_type is a smart
  142. // pointer, we're talking about the pointee.
  143. typedef typename mpl::eval_if<
  144. use_value_holder
  145. , mpl::identity<held_type>
  146. , pointee<held_type>
  147. >::type wrapped;
  148. // Determine whether to use a "back-reference holder"
  149. typedef mpl::bool_<
  150. mpl::or_<
  151. has_back_reference<T>
  152. , is_same<held_type_arg,T>
  153. , is_base_and_derived<T,wrapped>
  154. >::value
  155. > use_back_reference;
  156. // Select the holder.
  157. typedef typename mpl::eval_if<
  158. use_back_reference
  159. , mpl::if_<
  160. use_value_holder
  161. , value_holder_back_reference<T, wrapped>
  162. , pointer_holder_back_reference<held_type,T>
  163. >
  164. , mpl::if_<
  165. use_value_holder
  166. , value_holder<T>
  167. , pointer_holder<held_type,wrapped>
  168. >
  169. >::type holder;
  170. inline static void register_() // Register the runtime metadata.
  171. {
  172. class_metadata::register_aux((T*)0);
  173. }
  174. private:
  175. template <class T2>
  176. inline static void register_aux(python::wrapper<T2>*)
  177. {
  178. typedef typename mpl::not_<is_same<T2,wrapped> >::type use_callback;
  179. class_metadata::register_aux2((T2*)0, use_callback());
  180. }
  181. inline static void register_aux(void*)
  182. {
  183. typedef typename is_base_and_derived<T,wrapped>::type use_callback;
  184. class_metadata::register_aux2((T*)0, use_callback());
  185. }
  186. template <class T2, class Callback>
  187. inline static void register_aux2(T2*, Callback)
  188. {
  189. objects::register_shared_ptr_from_python_and_casts((T2*)0, bases());
  190. class_metadata::maybe_register_callback_class((T2*)0, Callback());
  191. class_metadata::maybe_register_class_to_python((T2*)0, is_noncopyable());
  192. class_metadata::maybe_register_pointer_to_python(
  193. (T2*)0, (use_value_holder*)0, (use_back_reference*)0);
  194. }
  195. //
  196. // Support for converting smart pointers to python
  197. //
  198. inline static void maybe_register_pointer_to_python(...) {}
  199. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  200. inline static void maybe_register_pointer_to_python(void*,void*,mpl::true_*)
  201. {
  202. objects::copy_class_object(python::type_id<T>(), python::type_id<back_reference<T const &> >());
  203. objects::copy_class_object(python::type_id<T>(), python::type_id<back_reference<T &> >());
  204. }
  205. #endif
  206. template <class T2>
  207. inline static void maybe_register_pointer_to_python(T2*, mpl::false_*, mpl::false_*)
  208. {
  209. python::detail::force_instantiate(
  210. objects::class_value_wrapper<
  211. held_type
  212. , make_ptr_instance<T2, pointer_holder<held_type, T2> >
  213. >()
  214. );
  215. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  216. // explicit qualification of type_id makes msvc6 happy
  217. objects::copy_class_object(python::type_id<T2>(), python::type_id<held_type>());
  218. #endif
  219. }
  220. //
  221. // Support for registering to-python converters
  222. //
  223. inline static void maybe_register_class_to_python(void*, mpl::true_) {}
  224. template <class T2>
  225. inline static void maybe_register_class_to_python(T2*, mpl::false_)
  226. {
  227. python::detail::force_instantiate(class_cref_wrapper<T2, make_instance<T2, holder> >());
  228. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  229. // explicit qualification of type_id makes msvc6 happy
  230. objects::copy_class_object(python::type_id<T2>(), python::type_id<held_type>());
  231. #endif
  232. }
  233. //
  234. // Support for registering callback classes
  235. //
  236. inline static void maybe_register_callback_class(void*, mpl::false_) {}
  237. template <class T2>
  238. inline static void maybe_register_callback_class(T2*, mpl::true_)
  239. {
  240. objects::register_shared_ptr_from_python_and_casts(
  241. (wrapped*)0, mpl::single_view<T2>());
  242. // explicit qualification of type_id makes msvc6 happy
  243. objects::copy_class_object(python::type_id<T2>(), python::type_id<wrapped>());
  244. }
  245. };
  246. }}} // namespace boost::python::object
  247. #endif // CLASS_METADATA_DWA2004719_HPP