Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef FROM_PYTHON_AUX_DATA_DWA2002128_HPP
|
Chris@16
|
6 # define FROM_PYTHON_AUX_DATA_DWA2002128_HPP
|
Chris@16
|
7
|
Chris@16
|
8 # include <boost/python/converter/constructor_function.hpp>
|
Chris@16
|
9 # include <boost/python/detail/referent_storage.hpp>
|
Chris@16
|
10 # include <boost/python/detail/destroy.hpp>
|
Chris@16
|
11 # include <boost/static_assert.hpp>
|
Chris@16
|
12 # include <boost/type_traits/add_reference.hpp>
|
Chris@16
|
13 # include <boost/type_traits/add_cv.hpp>
|
Chris@16
|
14 # include <cstddef>
|
Chris@16
|
15
|
Chris@16
|
16 // Data management for potential rvalue conversions from Python to C++
|
Chris@16
|
17 // types. When a client requests a conversion to T* or T&, we
|
Chris@16
|
18 // generally require that an object of type T exists in the source
|
Chris@16
|
19 // Python object, and the code here does not apply**. This implements
|
Chris@16
|
20 // conversions which may create new temporaries of type T. The classic
|
Chris@16
|
21 // example is a conversion which converts a Python tuple to a
|
Chris@16
|
22 // std::vector. Since no std::vector lvalue exists in the Python
|
Chris@16
|
23 // object -- it must be created "on-the-fly" by the converter, and
|
Chris@16
|
24 // which must manage the lifetime of the created object.
|
Chris@16
|
25 //
|
Chris@16
|
26 // Note that the client is not precluded from using a registered
|
Chris@16
|
27 // lvalue conversion to T in this case. In other words, we will
|
Chris@16
|
28 // happily accept a Python object which /does/ contain a std::vector
|
Chris@16
|
29 // lvalue, provided an appropriate converter is registered. So, while
|
Chris@16
|
30 // this is an rvalue conversion from the client's point-of-view, the
|
Chris@16
|
31 // converter registry may serve up lvalue or rvalue conversions for
|
Chris@16
|
32 // the target type.
|
Chris@16
|
33 //
|
Chris@16
|
34 // ** C++ argument from_python conversions to T const& are an
|
Chris@16
|
35 // exception to the rule for references: since in C++, const
|
Chris@16
|
36 // references can bind to temporary rvalues, we allow rvalue
|
Chris@16
|
37 // converters to be chosen when the target type is T const& for some
|
Chris@16
|
38 // T.
|
Chris@16
|
39 namespace boost { namespace python { namespace converter {
|
Chris@16
|
40
|
Chris@16
|
41 // Conversions begin by filling in and returning a copy of this
|
Chris@16
|
42 // structure. The process looks up a converter in the rvalue converter
|
Chris@16
|
43 // registry for the target type. It calls the convertible() function
|
Chris@16
|
44 // of each registered converter, passing the source PyObject* as an
|
Chris@16
|
45 // argument, until a non-null result is returned. This result goes in
|
Chris@16
|
46 // the convertible field, and the converter's construct() function is
|
Chris@16
|
47 // stored in the construct field.
|
Chris@16
|
48 //
|
Chris@16
|
49 // If no appropriate converter is found, conversion fails and the
|
Chris@16
|
50 // convertible field is null. When used in argument conversion for
|
Chris@16
|
51 // wrapped C++ functions, it causes overload resolution to reject the
|
Chris@16
|
52 // current function but not to fail completely. If an exception is
|
Chris@16
|
53 // thrown, overload resolution stops and the exception propagates back
|
Chris@16
|
54 // through the caller.
|
Chris@16
|
55 //
|
Chris@16
|
56 // If an lvalue converter is matched, its convertible() function is
|
Chris@16
|
57 // expected to return a pointer to the stored T object; its
|
Chris@16
|
58 // construct() function will be NULL. The convertible() function of
|
Chris@16
|
59 // rvalue converters may return any non-singular pointer; the actual
|
Chris@16
|
60 // target object will only be available once the converter's
|
Chris@16
|
61 // construct() function is called.
|
Chris@16
|
62 struct rvalue_from_python_stage1_data
|
Chris@16
|
63 {
|
Chris@16
|
64 void* convertible;
|
Chris@16
|
65 constructor_function construct;
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@16
|
68 // Augments rvalue_from_python_stage1_data by adding storage for
|
Chris@16
|
69 // constructing an object of remove_reference<T>::type. The
|
Chris@16
|
70 // construct() function of rvalue converters (stored in m_construct
|
Chris@16
|
71 // above) will cast the rvalue_from_python_stage1_data to an
|
Chris@16
|
72 // appropriate instantiation of this template in order to access that
|
Chris@16
|
73 // storage.
|
Chris@16
|
74 template <class T>
|
Chris@16
|
75 struct rvalue_from_python_storage
|
Chris@16
|
76 {
|
Chris@16
|
77 rvalue_from_python_stage1_data stage1;
|
Chris@16
|
78
|
Chris@16
|
79 // Storage for the result, in case an rvalue must be constructed
|
Chris@16
|
80 typename python::detail::referent_storage<
|
Chris@16
|
81 typename add_reference<T>::type
|
Chris@16
|
82 >::type storage;
|
Chris@16
|
83 };
|
Chris@16
|
84
|
Chris@16
|
85 // Augments rvalue_from_python_storage<T> with a destructor. If
|
Chris@16
|
86 // stage1.convertible == storage.bytes, it indicates that an object of
|
Chris@16
|
87 // remove_reference<T>::type has been constructed in storage and
|
Chris@16
|
88 // should will be destroyed in ~rvalue_from_python_data(). It is
|
Chris@16
|
89 // crucial that successful rvalue conversions establish this equality
|
Chris@16
|
90 // and that unsuccessful ones do not.
|
Chris@16
|
91 template <class T>
|
Chris@16
|
92 struct rvalue_from_python_data : rvalue_from_python_storage<T>
|
Chris@16
|
93 {
|
Chris@16
|
94 # if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \
|
Chris@16
|
95 && (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \
|
Chris@16
|
96 && (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \
|
Chris@16
|
97 && !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */
|
Chris@16
|
98 // This must always be a POD struct with m_data its first member.
|
Chris@16
|
99 BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage<T>,stage1) == 0);
|
Chris@16
|
100 # endif
|
Chris@16
|
101
|
Chris@16
|
102 // The usual constructor
|
Chris@16
|
103 rvalue_from_python_data(rvalue_from_python_stage1_data const&);
|
Chris@16
|
104
|
Chris@16
|
105 // This constructor just sets m_convertible -- used by
|
Chris@16
|
106 // implicitly_convertible<> to perform the final step of the
|
Chris@16
|
107 // conversion, where the construct() function is already known.
|
Chris@16
|
108 rvalue_from_python_data(void* convertible);
|
Chris@16
|
109
|
Chris@16
|
110 // Destroys any object constructed in the storage.
|
Chris@16
|
111 ~rvalue_from_python_data();
|
Chris@16
|
112 private:
|
Chris@16
|
113 typedef typename add_reference<typename add_cv<T>::type>::type ref_type;
|
Chris@16
|
114 };
|
Chris@16
|
115
|
Chris@16
|
116 //
|
Chris@16
|
117 // Implementataions
|
Chris@16
|
118 //
|
Chris@16
|
119 template <class T>
|
Chris@16
|
120 inline rvalue_from_python_data<T>::rvalue_from_python_data(rvalue_from_python_stage1_data const& _stage1)
|
Chris@16
|
121 {
|
Chris@16
|
122 this->stage1 = _stage1;
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@16
|
125 template <class T>
|
Chris@16
|
126 inline rvalue_from_python_data<T>::rvalue_from_python_data(void* convertible)
|
Chris@16
|
127 {
|
Chris@16
|
128 this->stage1.convertible = convertible;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 template <class T>
|
Chris@16
|
132 inline rvalue_from_python_data<T>::~rvalue_from_python_data()
|
Chris@16
|
133 {
|
Chris@16
|
134 if (this->stage1.convertible == this->storage.bytes)
|
Chris@16
|
135 python::detail::destroy_referent<ref_type>(this->storage.bytes);
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 }}} // namespace boost::python::converter
|
Chris@16
|
139
|
Chris@16
|
140 #endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP
|