]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/python/doc/reference/instance_holder.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / python / doc / reference / instance_holder.qbk
CommitLineData
7c673cae
FG
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``
8namespace boost { namespace python
9{
10 class instance_holder : noncopyable
11 {
12 public:
13 // destructor
14 virtual ~instance_holder();
15
16 // instance_holder modifiers
17 void install(PyObject* inst) throw();
18
19 // instance_holder observers
20 virtual void* holds(type_info) = 0;
21 };
22}}
23``
24[section Class `intance_holder` destructor]
25``virtual ~instance_holder();``
26[variablelist
27[[Effects][destroys the object]]
28]
29[endsect]
30[section Class `intance_holder` modifiers]
31``void install(PyObject* inst) throw();``
32[variablelist
33[[Requires][`inst` is a Python instance of a wrapped C++ class type, or is a type derived from a wrapped C++ class type. ]]
34[[Effects][installs the new instance at the head of the Python object's chain of held instances. ]]
35[[Throws][nothing]]
36]
37[endsect]
38[section Class `intance_holder` observers]
39``virtual void *holds(type_info x) = 0;``
40[variablelist
41[[Returns][A pointer to an object of the type described by `x` if `*this` contains such an object, 0 otherwise. ]]
42]
43[endsect]
44[endsect]
45[section Examples]
46The following is a simplified version of the instance holder template used by Boost.Python to wrap classes held by smart pointers:
47
48``
49template <class SmartPtr, class Value>
50struct pointer_holder : instance_holder
51{
52 // construct from the SmartPtr type
53 pointer_holder(SmartPtr p)
54 :m_p(p)
55
56 // Forwarding constructors for the held type
57 pointer_holder(PyObject*)
58 :m_p(new Value())
59 {
60 }
61
62 template<class A0>
63 pointer_holder(PyObject*,A0 a0)
64 :m_p(new Value(a0))
65 {
66 }
67
68 template<class A0,class A1>
69 pointer_holder(PyObject*,A0 a0,A1 a1)
70 :m_p(new Value(a0,a1))
71 {
72 }
73 ...
74
75 private: // required holder implementation
76 void* holds(type_info dst_t)
77 {
78 // holds an instance of the SmartPtr type...
79 if (dst_t == python::type_id<SmartPtr>())
80 return &this->m_p;
81
82 // ...and an instance of the SmartPtr's element_type, if the
83 // pointer is non-null
84 return python::type_id<Value>() == dst_t ? &*this->m_p : 0;
85 }
86
87 private: // data members
88 SmartPtr m_p;
89};
90 ``
91[endsect]
92[endsect]