|
||||
File indexing completed on 2025-01-18 09:50:37
0001 // Copyright David Abrahams 2002. 0002 // Distributed under the Boost Software License, Version 1.0. (See 0003 // accompanying file LICENSE_1_0.txt or copy at 0004 // http://www.boost.org/LICENSE_1_0.txt) 0005 #ifndef FROM_PYTHON_AUX_DATA_DWA2002128_HPP 0006 # define FROM_PYTHON_AUX_DATA_DWA2002128_HPP 0007 0008 # include <boost/python/converter/constructor_function.hpp> 0009 # include <boost/python/detail/referent_storage.hpp> 0010 # include <boost/python/detail/destroy.hpp> 0011 # include <boost/python/detail/type_traits.hpp> 0012 # include <boost/align/align.hpp> 0013 # include <boost/static_assert.hpp> 0014 # include <cstddef> 0015 0016 // Data management for potential rvalue conversions from Python to C++ 0017 // types. When a client requests a conversion to T* or T&, we 0018 // generally require that an object of type T exists in the source 0019 // Python object, and the code here does not apply**. This implements 0020 // conversions which may create new temporaries of type T. The classic 0021 // example is a conversion which converts a Python tuple to a 0022 // std::vector. Since no std::vector lvalue exists in the Python 0023 // object -- it must be created "on-the-fly" by the converter, and 0024 // which must manage the lifetime of the created object. 0025 // 0026 // Note that the client is not precluded from using a registered 0027 // lvalue conversion to T in this case. In other words, we will 0028 // happily accept a Python object which /does/ contain a std::vector 0029 // lvalue, provided an appropriate converter is registered. So, while 0030 // this is an rvalue conversion from the client's point-of-view, the 0031 // converter registry may serve up lvalue or rvalue conversions for 0032 // the target type. 0033 // 0034 // ** C++ argument from_python conversions to T const& are an 0035 // exception to the rule for references: since in C++, const 0036 // references can bind to temporary rvalues, we allow rvalue 0037 // converters to be chosen when the target type is T const& for some 0038 // T. 0039 namespace boost { namespace python { namespace converter { 0040 0041 // Conversions begin by filling in and returning a copy of this 0042 // structure. The process looks up a converter in the rvalue converter 0043 // registry for the target type. It calls the convertible() function 0044 // of each registered converter, passing the source PyObject* as an 0045 // argument, until a non-null result is returned. This result goes in 0046 // the convertible field, and the converter's construct() function is 0047 // stored in the construct field. 0048 // 0049 // If no appropriate converter is found, conversion fails and the 0050 // convertible field is null. When used in argument conversion for 0051 // wrapped C++ functions, it causes overload resolution to reject the 0052 // current function but not to fail completely. If an exception is 0053 // thrown, overload resolution stops and the exception propagates back 0054 // through the caller. 0055 // 0056 // If an lvalue converter is matched, its convertible() function is 0057 // expected to return a pointer to the stored T object; its 0058 // construct() function will be NULL. The convertible() function of 0059 // rvalue converters may return any non-singular pointer; the actual 0060 // target object will only be available once the converter's 0061 // construct() function is called. 0062 struct rvalue_from_python_stage1_data 0063 { 0064 void* convertible; 0065 constructor_function construct; 0066 }; 0067 0068 // Augments rvalue_from_python_stage1_data by adding storage for 0069 // constructing an object of remove_reference<T>::type. The 0070 // construct() function of rvalue converters (stored in m_construct 0071 // above) will cast the rvalue_from_python_stage1_data to an 0072 // appropriate instantiation of this template in order to access that 0073 // storage. 0074 template <class T> 0075 struct rvalue_from_python_storage 0076 { 0077 rvalue_from_python_stage1_data stage1; 0078 0079 // Storage for the result, in case an rvalue must be constructed 0080 typename python::detail::referent_storage< 0081 typename boost::python::detail::add_lvalue_reference<T>::type 0082 >::type storage; 0083 }; 0084 0085 // Augments rvalue_from_python_storage<T> with a destructor. If 0086 // stage1.convertible == storage.bytes, it indicates that an object of 0087 // remove_reference<T>::type has been constructed in storage and 0088 // should will be destroyed in ~rvalue_from_python_data(). It is 0089 // crucial that successful rvalue conversions establish this equality 0090 // and that unsuccessful ones do not. 0091 template <class T> 0092 struct rvalue_from_python_data : rvalue_from_python_storage<T> 0093 { 0094 # if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \ 0095 && (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \ 0096 && (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \ 0097 && !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */ 0098 // This must always be a POD struct with m_data its first member. 0099 BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage<T>,stage1) == 0); 0100 # endif 0101 0102 // The usual constructor 0103 rvalue_from_python_data(rvalue_from_python_stage1_data const&); 0104 0105 // This constructor just sets m_convertible -- used by 0106 // implicitly_convertible<> to perform the final step of the 0107 // conversion, where the construct() function is already known. 0108 rvalue_from_python_data(void* convertible); 0109 0110 // Destroys any object constructed in the storage. 0111 ~rvalue_from_python_data(); 0112 private: 0113 typedef typename boost::python::detail::add_lvalue_reference< 0114 typename boost::python::detail::add_cv<T>::type>::type ref_type; 0115 }; 0116 0117 // 0118 // Implementataions 0119 // 0120 template <class T> 0121 inline rvalue_from_python_data<T>::rvalue_from_python_data(rvalue_from_python_stage1_data const& _stage1) 0122 { 0123 this->stage1 = _stage1; 0124 } 0125 0126 template <class T> 0127 inline rvalue_from_python_data<T>::rvalue_from_python_data(void* convertible) 0128 { 0129 this->stage1.convertible = convertible; 0130 } 0131 0132 template <class T> 0133 inline rvalue_from_python_data<T>::~rvalue_from_python_data() 0134 { 0135 if (this->stage1.convertible == this->storage.bytes) 0136 { 0137 size_t allocated = sizeof(this->storage); 0138 void *ptr = this->storage.bytes; 0139 void *aligned_storage = 0140 ::boost::alignment::align(boost::python::detail::alignment_of<T>::value, 0, ptr, allocated); 0141 python::detail::destroy_referent<ref_type>(aligned_storage); 0142 } 0143 } 0144 0145 }}} // namespace boost::python::converter 0146 0147 #endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |