registrations.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef REGISTRATIONS_DWA2002223_HPP
  6. # define REGISTRATIONS_DWA2002223_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/type_id.hpp>
  9. # include <boost/python/converter/convertible_function.hpp>
  10. # include <boost/python/converter/constructor_function.hpp>
  11. # include <boost/python/converter/to_python_function_type.hpp>
  12. # include <boost/detail/workaround.hpp>
  13. namespace boost { namespace python { namespace converter {
  14. struct lvalue_from_python_chain
  15. {
  16. convertible_function convert;
  17. lvalue_from_python_chain* next;
  18. };
  19. struct rvalue_from_python_chain
  20. {
  21. convertible_function convertible;
  22. constructor_function construct;
  23. PyTypeObject const* (*expected_pytype)();
  24. rvalue_from_python_chain* next;
  25. };
  26. struct BOOST_PYTHON_DECL registration
  27. {
  28. public: // member functions
  29. explicit registration(type_info target, bool is_shared_ptr = false);
  30. ~registration();
  31. // Convert the appropriately-typed data to Python
  32. PyObject* to_python(void const volatile*) const;
  33. // Return the class object, or raise an appropriate Python
  34. // exception if no class has been registered.
  35. PyTypeObject* get_class_object() const;
  36. // Return common denominator of the python class objects,
  37. // convertable to target. Inspects the m_class_object and the value_chains.
  38. PyTypeObject const* expected_from_python_type() const;
  39. PyTypeObject const* to_python_target_type() const;
  40. public: // data members. So sue me.
  41. const python::type_info target_type;
  42. // The chain of eligible from_python converters when an lvalue is required
  43. lvalue_from_python_chain* lvalue_chain;
  44. // The chain of eligible from_python converters when an rvalue is acceptable
  45. rvalue_from_python_chain* rvalue_chain;
  46. // The class object associated with this type
  47. PyTypeObject* m_class_object;
  48. // The unique to_python converter for the associated C++ type.
  49. to_python_function_t m_to_python;
  50. PyTypeObject const* (*m_to_python_target_type)();
  51. // True iff this type is a shared_ptr. Needed for special rvalue
  52. // from_python handling.
  53. const bool is_shared_ptr;
  54. # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  55. private:
  56. void operator=(registration); // This is not defined, and just keeps MWCW happy.
  57. # endif
  58. };
  59. //
  60. // implementations
  61. //
  62. inline registration::registration(type_info target_type, bool is_shared_ptr)
  63. : target_type(target_type)
  64. , lvalue_chain(0)
  65. , rvalue_chain(0)
  66. , m_class_object(0)
  67. , m_to_python(0)
  68. , m_to_python_target_type(0)
  69. , is_shared_ptr(is_shared_ptr)
  70. {}
  71. inline bool operator<(registration const& lhs, registration const& rhs)
  72. {
  73. return lhs.target_type < rhs.target_type;
  74. }
  75. }}} // namespace boost::python::converter
  76. #endif // REGISTRATIONS_DWA2002223_HPP