instance_holder.qbk 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. [section boost/python/instance_holder.hpp]
  2. [section Introduction]
  3. <boost/python/instance_holder.hpp> provides class `instance_holder`, the base class for types which hold C++ instances of wrapped classes.
  4. [endsect]
  5. [section Class template `instance_holder`]
  6. `instance_holder` is an abstract base class whose concrete derived classes hold C++ class instances within their Python object wrappers. To allow multiple inheritance in Python from C++ class wrappers, each such Python object contains a chain of instance_holders. When an `__init__` function for a wrapped C++ class is invoked, a new `instance_holder` instance is created and installed in the Python object using its `install()` function. Each concrete class derived from `instance_holder` must provide a `holds()` implementation which allows Boost.Python to query it for the type(s) it is holding. In order to support the held type's wrapped constructor(s), the class must also provide constructors that can accept an initial `PyObject*` argument referring to the owning Python object, and which forward the rest of their arguments to the constructor of the held type. The initial argument is needed to enable virtual function overriding in Python, and may be ignored, depending on the specific `instance_holder` subclass.
  7. ``
  8. namespace boost { namespace python
  9. {
  10. class instance_holder : noncopyable
  11. {
  12. public:
  13. // destructor
  14. virtual ~instance_holder();
  15. // instance_holder modifiers
  16. void install(PyObject* inst) throw();
  17. // instance_holder observers
  18. virtual void* holds(type_info) = 0;
  19. };
  20. }}
  21. ``
  22. [section Class `intance_holder` destructor]
  23. ``virtual ~instance_holder();``
  24. [variablelist
  25. [[Effects][destroys the object]]
  26. ]
  27. [endsect]
  28. [section Class `intance_holder` modifiers]
  29. ``void install(PyObject* inst) throw();``
  30. [variablelist
  31. [[Requires][`inst` is a Python instance of a wrapped C++ class type, or is a type derived from a wrapped C++ class type. ]]
  32. [[Effects][installs the new instance at the head of the Python object's chain of held instances. ]]
  33. [[Throws][nothing]]
  34. ]
  35. [endsect]
  36. [section Class `intance_holder` observers]
  37. ``virtual void *holds(type_info x) = 0;``
  38. [variablelist
  39. [[Returns][A pointer to an object of the type described by `x` if `*this` contains such an object, 0 otherwise. ]]
  40. ]
  41. [endsect]
  42. [endsect]
  43. [section Examples]
  44. The following is a simplified version of the instance holder template used by Boost.Python to wrap classes held by smart pointers:
  45. ``
  46. template <class SmartPtr, class Value>
  47. struct pointer_holder : instance_holder
  48. {
  49. // construct from the SmartPtr type
  50. pointer_holder(SmartPtr p)
  51. :m_p(p)
  52. // Forwarding constructors for the held type
  53. pointer_holder(PyObject*)
  54. :m_p(new Value())
  55. {
  56. }
  57. template<class A0>
  58. pointer_holder(PyObject*,A0 a0)
  59. :m_p(new Value(a0))
  60. {
  61. }
  62. template<class A0,class A1>
  63. pointer_holder(PyObject*,A0 a0,A1 a1)
  64. :m_p(new Value(a0,a1))
  65. {
  66. }
  67. ...
  68. private: // required holder implementation
  69. void* holds(type_info dst_t)
  70. {
  71. // holds an instance of the SmartPtr type...
  72. if (dst_t == python::type_id<SmartPtr>())
  73. return &this->m_p;
  74. // ...and an instance of the SmartPtr's element_type, if the
  75. // pointer is non-null
  76. return python::type_id<Value>() == dst_t ? &*this->m_p : 0;
  77. }
  78. private: // data members
  79. SmartPtr m_p;
  80. };
  81. ``
  82. [endsect]
  83. [endsect]