diff --git a/include/boost/python/converter/arg_from_python.hpp b/include/boost/python/converter/arg_from_python.hpp index ccdb961c..fa59f1d7 100755 --- a/include/boost/python/converter/arg_from_python.hpp +++ b/include/boost/python/converter/arg_from_python.hpp @@ -11,13 +11,14 @@ # include # include # include -# include +# include # include # include # include # include # include # include +# include namespace boost { namespace python { @@ -48,7 +49,7 @@ struct pointer_cref_arg_from_python private: // storage for a U* // needed because not all compilers will let us declare U* as the // return type of operator() -- we return U*const& instead - typename detail::referent_storage::type m_result; + typename python::detail::referent_storage::type m_result; }; // Base class for pointer and reference converters diff --git a/include/boost/python/converter/callback_from_python_base.hpp b/include/boost/python/converter/callback_from_python_base.hpp index 117ba145..462857af 100644 --- a/include/boost/python/converter/callback_from_python_base.hpp +++ b/include/boost/python/converter/callback_from_python_base.hpp @@ -12,12 +12,13 @@ namespace detail { // Throw an exception - BOOST_PYTHON_DECL void throw_if_not_registered(rvalue_stage1_data const&); - BOOST_PYTHON_DECL void* convert_rvalue(PyObject*, rvalue_stage1_data& data, void* storage); + BOOST_PYTHON_DECL void throw_if_not_registered(rvalue_from_python_stage1_data const&); + BOOST_PYTHON_DECL void* convert_rvalue(PyObject*, rvalue_from_python_stage1_data&, void* storage); BOOST_PYTHON_DECL void throw_if_not_registered(lvalue_from_python_registration*const&); BOOST_PYTHON_DECL void* callback_convert_reference(PyObject*, lvalue_from_python_registration*const&); BOOST_PYTHON_DECL void* callback_convert_pointer(PyObject*, lvalue_from_python_registration*const&); + BOOST_PYTHON_DECL void absorb_result(PyObject*); } }}} // namespace boost::python::converter diff --git a/include/boost/python/converter/from_python_function.hpp b/include/boost/python/converter/constructor_function.hpp similarity index 53% rename from include/boost/python/converter/from_python_function.hpp rename to include/boost/python/converter/constructor_function.hpp index eea96372..f3c49bd4 100644 --- a/include/boost/python/converter/from_python_function.hpp +++ b/include/boost/python/converter/constructor_function.hpp @@ -3,16 +3,16 @@ // copyright notice appears in all copies. This software is provided // "as is" without express or implied warranty, and with no claim as // to its suitability for any purpose. -#ifndef FROM_PYTHON_FUNCTION_DWA2002128_HPP -# define FROM_PYTHON_FUNCTION_DWA2002128_HPP - -# include +#ifndef CONSTRUCTOR_FUNCTION_DWA200278_HPP +# define CONSTRUCTOR_FUNCTION_DWA200278_HPP namespace boost { namespace python { namespace converter { -struct rvalue_stage1_data; -typedef void (*constructor_function)(PyObject* source, rvalue_stage1_data*); +// Declares the type of functions used to construct C++ objects for +// rvalue from_python conversions. +struct rvalue_from_python_stage1_data; +typedef void (*constructor_function)(PyObject* source, rvalue_from_python_stage1_data*); }}} // namespace boost::python::converter -#endif // FROM_PYTHON_FUNCTION_DWA2002128_HPP +#endif // CONSTRUCTOR_FUNCTION_DWA200278_HPP diff --git a/include/boost/python/converter/find_from_python.hpp b/include/boost/python/converter/find_from_python.hpp index 709c5de0..659848ce 100644 --- a/include/boost/python/converter/find_from_python.hpp +++ b/include/boost/python/converter/find_from_python.hpp @@ -8,7 +8,7 @@ # include # include -# include +# include namespace boost { namespace python { namespace converter { @@ -18,7 +18,7 @@ struct rvalue_from_python_registration; BOOST_PYTHON_DECL void* find( PyObject* source, lvalue_from_python_registration const*); -BOOST_PYTHON_DECL rvalue_stage1_data find( +BOOST_PYTHON_DECL rvalue_from_python_stage1_data find( PyObject* source, rvalue_from_python_registration const*); BOOST_PYTHON_DECL rvalue_from_python_registration const* find_chain( diff --git a/include/boost/python/converter/from_python_data.hpp b/include/boost/python/converter/from_python_data.hpp deleted file mode 100644 index b0d8a49f..00000000 --- a/include/boost/python/converter/from_python_data.hpp +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright David Abrahams 2002. Permission to copy, use, -// modify, sell and distribute this software is granted provided this -// copyright notice appears in all copies. This software is provided -// "as is" without express or implied warranty, and with no claim as -// to its suitability for any purpose. -#ifndef FROM_PYTHON_AUX_DATA_DWA2002128_HPP -# define FROM_PYTHON_AUX_DATA_DWA2002128_HPP - -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include - -namespace boost { namespace python { namespace converter { - -namespace detail -{ - template struct referent_size; - -# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION - - template - struct referent_size - { - BOOST_STATIC_CONSTANT( - std::size_t, value = sizeof(T)); - }; - -# else - - template struct referent_size - { - static T f(); - BOOST_STATIC_CONSTANT(std::size_t, value = sizeof(f())); - }; - -# endif - struct alignment_dummy; - typedef void (*function_ptr)(); - typedef int (alignment_dummy::*member_ptr); - typedef int (alignment_dummy::*member_function_ptr)(); - -# define BOOST_PYTHON_ALIGNMENT_TYPES BOOST_PP_TUPLE_TO_LIST( \ - 11, ( \ - char, short, int, long, float, double, long double \ - , void*, function_ptr, member_ptr, member_function_ptr)) - -# define BOOST_PYTHON_CHOOSE_LOWER_SIZE(R,P,I,T) \ - typename mpl::select_type< \ - sizeof(T) <= target, T, char>::type BOOST_PP_CAT(t,I); - -# define BOOST_PYTHON_CHOOSE_T(R,P,I,T) T BOOST_PP_CAT(t,I); - - template - union lower_size - { - BOOST_PP_LIST_FOR_EACH_I( - BOOST_PYTHON_CHOOSE_LOWER_SIZE - , ignored, BOOST_PYTHON_ALIGNMENT_TYPES) - }; - - template - union aligned_storage - { - Align align; - char bytes[size]; - }; - - template - struct referent_storage - { - typedef lower_size::value> align_t; - typedef aligned_storage::value> type; - }; -} - -template -struct rvalue_base_data -{ - rvalue_stage1_data stage1; - - typename detail::referent_storage< - typename add_reference::type - >::type storage; -}; - -template -struct rvalue_data : rvalue_base_data -{ - rvalue_data(rvalue_stage1_data const&); - rvalue_data(void*); - ~rvalue_data(); - private: - typedef typename add_reference::type>::type ref_type; -}; - -// -// Implementataions -// -template -inline rvalue_data::rvalue_data(rvalue_stage1_data const& stage1) -{ - this->stage1 = stage1; -} - -template -inline rvalue_data::rvalue_data(void* convertible) -{ - this->stage1.convertible = convertible; -} - -template -inline rvalue_data::~rvalue_data() -{ - if (this->stage1.convertible == this->storage.bytes) - python::detail::destroy_reference(this->storage.bytes); -} - -}}} // namespace boost::python::converter - -#endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP diff --git a/include/boost/python/converter/from_python_stage1_data.hpp b/include/boost/python/converter/from_python_stage1_data.hpp deleted file mode 100644 index 50fd74ad..00000000 --- a/include/boost/python/converter/from_python_stage1_data.hpp +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright David Abrahams 2002. Permission to copy, use, -// modify, sell and distribute this software is granted provided this -// copyright notice appears in all copies. This software is provided -// "as is" without express or implied warranty, and with no claim as -// to its suitability for any purpose. -#ifndef FROM_PYTHON_STAGE1_DATA_DWA2002223_HPP -# define FROM_PYTHON_STAGE1_DATA_DWA2002223_HPP - -# include - -namespace boost { namespace python { namespace converter { - -struct rvalue_stage1_data -{ - void* convertible; - constructor_function construct; -}; - -}}} // namespace boost::python::converter - -#endif // FROM_PYTHON_STAGE1_DATA_DWA2002223_HPP diff --git a/include/boost/python/converter/implicit.hpp b/include/boost/python/converter/implicit.hpp index 6a22a3d6..ab78d8a1 100644 --- a/include/boost/python/converter/implicit.hpp +++ b/include/boost/python/converter/implicit.hpp @@ -5,8 +5,7 @@ // to its suitability for any purpose. #ifndef IMPLICIT_DWA2002326_HPP # define IMPLICIT_DWA2002326_HPP -# include -# include +# include # include namespace boost { namespace python { namespace converter { @@ -22,7 +21,7 @@ struct implicit find_chain(obj, rvalue_from_python_chain::value)); } - static void construct(PyObject* obj, rvalue_stage1_data* data) + static void construct(PyObject* obj, rvalue_from_python_stage1_data* data) { // This is the registration we got from the convertible step rvalue_from_python_registration const* registration @@ -36,7 +35,7 @@ struct implicit if (registration->construct != 0) registration->construct(obj, &intermediate_data.stage1); - void* storage = ((rvalue_base_data*)data)->storage.bytes; + void* storage = ((rvalue_from_python_storage*)data)->storage.bytes; # if !defined(BOOST_MSVC) || _MSC_FULL_VER != 13012108 // vc7.01 alpha workaround new (storage) Target(*static_cast(intermediate_data.stage1.convertible)); # else diff --git a/include/boost/python/converter/lvalue_from_python_chain.hpp b/include/boost/python/converter/lvalue_from_python_chain.hpp index 387c1863..0695025c 100644 --- a/include/boost/python/converter/lvalue_from_python_chain.hpp +++ b/include/boost/python/converter/lvalue_from_python_chain.hpp @@ -17,8 +17,8 @@ namespace boost { namespace python { namespace converter { // Given T == U*cv&, T == U*, or T == U&, lvalue_from_python_chain // declares a "templated global reference" to the lvalue from_python // converter chain for U. The optional bool second argument is_return, -// when true, removes special treatment for T == U*cv& so that the -// converter for U* is found. +// when true, removes the usual special treatment which causes the +// converter for U* to bve used when T == U*cv& namespace detail { template diff --git a/include/boost/python/converter/registrations.hpp b/include/boost/python/converter/registrations.hpp index 8a27741c..636e8fa0 100644 --- a/include/boost/python/converter/registrations.hpp +++ b/include/boost/python/converter/registrations.hpp @@ -6,7 +6,7 @@ #ifndef REGISTRATIONS_DWA2002223_HPP # define REGISTRATIONS_DWA2002223_HPP -# include +# include namespace boost { namespace python { namespace converter { diff --git a/include/boost/python/converter/registry.hpp b/include/boost/python/converter/registry.hpp index cf5daa5e..2cf97d98 100644 --- a/include/boost/python/converter/registry.hpp +++ b/include/boost/python/converter/registry.hpp @@ -9,7 +9,8 @@ # include # include # include -# include +# include +# include namespace boost { namespace python { namespace converter { diff --git a/include/boost/python/converter/return_from_python.hpp b/include/boost/python/converter/return_from_python.hpp index cb16c888..2272f94b 100755 --- a/include/boost/python/converter/return_from_python.hpp +++ b/include/boost/python/converter/return_from_python.hpp @@ -7,10 +7,12 @@ # define RETURN_FROM_PYTHON_DWA200265_HPP # include -# include +# include # include # include # include +# include +# include namespace boost { namespace python { namespace converter { @@ -19,6 +21,7 @@ namespace detail template struct return_pointer_from_python { + typedef T result_type; return_pointer_from_python(); T operator()(PyObject*) const; }; @@ -26,6 +29,7 @@ namespace detail template struct return_reference_from_python { + typedef T result_type; return_reference_from_python(); T operator()(PyObject*) const; }; @@ -33,8 +37,9 @@ namespace detail template struct return_rvalue_from_python { + typedef call_traits::param_type result_type; return_rvalue_from_python(); - T const& operator()(PyObject*); + result_type operator()(PyObject*); private: rvalue_data m_data; }; @@ -64,27 +69,20 @@ template struct return_from_python : detail::select_return_from_python::type { - typedef T result_type; -}; - -struct void_result -{ - private: - void_result() {} - void operator=(void_result const&); - - friend struct return_from_python; }; // Specialization as a convenience for call and call_method template <> struct return_from_python { - typedef void_result result_type; + typedef python::detail::returnable::type result_type; + result_type operator()(PyObject* x) const { - Py_DECREF(expect_non_null(x)); + converter::detail::absorb_result(x); +# ifdef BOOST_NO_VOID_RETURNS return result_type(); +# endif } }; @@ -101,7 +99,8 @@ namespace detail } template - inline T const& return_rvalue_from_python::operator()(PyObject* obj) + inline typename return_rvalue_from_python::result_type + return_rvalue_from_python::operator()(PyObject* obj) { return *(T*)convert_rvalue(obj, m_data.stage1, m_data.storage.bytes); } diff --git a/include/boost/python/converter/rvalue_from_python_data.hpp b/include/boost/python/converter/rvalue_from_python_data.hpp new file mode 100644 index 00000000..db4103c1 --- /dev/null +++ b/include/boost/python/converter/rvalue_from_python_data.hpp @@ -0,0 +1,133 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +#ifndef FROM_PYTHON_AUX_DATA_DWA2002128_HPP +# define FROM_PYTHON_AUX_DATA_DWA2002128_HPP + +# include +# include +# include +# include +# include + +// Data management for potential rvalue conversions from Python to C++ +// types. When a client requests a conversion to T* or T&, we +// generally require that an object of type T exists in the source +// Python object, and the code here does not apply**. This implements +// conversions which may create new temporaries of type T. The classic +// example is a conversion which converts a Python tuple to a +// std::vector. Since no std::vector lvalue exists in the Python +// object -- it must be created "on-the-fly" by the converter, and +// which must manage the lifetime of the created object. +// +// Note that the client is not precluded from using a registered +// lvalue conversion to T in this case. In other words, we will +// happily accept a Python object which /does/ contain a std::vector +// lvalue, provided an appropriate converter is registered. So, while +// this is an rvalue conversion from the client's point-of-view, the +// converter registry may serve up lvalue or rvalue conversions for +// the target type. +// +// ** C++ argument from_python conversions to T const& are an +// exception to the rule for references: since in C++, const +// references can bind to temporary rvalues, we allow rvalue +// converters to be chosen when the target type is T const& for some +// T. +namespace boost { namespace python { namespace converter { + +// Conversions begin by filling in and returning a copy of this +// structure. The process looks up a converter in the rvalue converter +// registry for the target type. It calls the convertible() function +// of each registered converter, passing the source PyObject* as an +// argument, until a non-null result is returned. This result goes in +// the convertible field, and the converter's construct() function is +// stored in the construct field. +// +// If no appropriate converter is found, conversion fails and the +// convertible field is null. This step is expected not to throw an +// exception, and when used in argument conversion for wrapped C++ +// functions, it causes overload resolution to reject the current +// function but not to fail completely. If +// +// If an lvalue converter is matched, its convertible() function is +// expected to return a pointer to the stored T object; its +// construct() function will be NULL. The convertible() function of +// rvalue converters may return any non-singular pointer; the actual +// target object will only be available once the converter's +// construct() function is called. +struct rvalue_from_python_stage1_data +{ + void* convertible; + constructor_function construct; +}; + +// Augments rvalue_from_python_stage1_data by adding storage for +// constructing an object of remove_reference::type. The +// construct() function of rvalue converters (stored in m_construct +// above) will cast the rvalue_from_python_stage1_data to an +// appropriate instantiation of this template in order to access that +// storage. +template +struct rvalue_from_python_storage +{ + rvalue_from_python_stage1_data stage1; + + // Storage for the result, in case an rvalue must be constructed + typename python::detail::referent_storage< + typename add_reference::type + >::type storage; +}; + +// Augments rvalue_from_python_storage with a destructor. If +// stage1.convertible == storage.bytes, it indicates that an object of +// remove_reference::type has been constructed in storage and +// should will be destroyed in ~rvalue_data(). It is crucial that +// successful rvalue conversions establish this equality and that +// unsuccessful ones do not. +template +struct rvalue_data : rvalue_from_python_storage +{ + // This must always be a POD struct with m_data its first member. + BOOST_STATIC_ASSERT(offsetof(rvalue_from_python_storage,stage1) == 0); + + // The usual constructor + rvalue_data(rvalue_from_python_stage1_data const&); + + // This constructor just sets m_convertible -- used by + // implicitly_convertible<> to perform the final step of the + // conversion, where the construct() function is already known. + rvalue_data(void* convertible); + + // Destroys any object constructed in the storage. + ~rvalue_data(); + private: + typedef typename add_reference::type>::type ref_type; +}; + +// +// Implementataions +// +template +inline rvalue_data::rvalue_data(rvalue_from_python_stage1_data const& stage1) +{ + this->stage1 = stage1; +} + +template +inline rvalue_data::rvalue_data(void* convertible) +{ + this->stage1.convertible = convertible; +} + +template +inline rvalue_data::~rvalue_data() +{ + if (this->stage1.convertible == this->storage.bytes) + python::detail::destroy_reference(this->storage.bytes); +} + +}}} // namespace boost::python::converter + +#endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP diff --git a/include/boost/python/detail/referent_storage.hpp b/include/boost/python/detail/referent_storage.hpp new file mode 100644 index 00000000..81ab33ea --- /dev/null +++ b/include/boost/python/detail/referent_storage.hpp @@ -0,0 +1,55 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +#ifndef REFERENT_STORAGE_DWA200278_HPP +# define REFERENT_STORAGE_DWA200278_HPP +# include +# include +# include +# include +# include + +namespace boost { namespace python { namespace detail { + +struct alignment_dummy; +typedef void (*function_ptr)(); +typedef int (alignment_dummy::*member_ptr); +typedef int (alignment_dummy::*member_function_ptr)(); + +# define BOOST_PYTHON_ALIGNER(T, n) \ + typename mpl::select_type< \ + sizeof(T) <= size, T, char>::type t##n + +// Storage for size bytes, aligned to all fundamental types no larger than size +template +union aligned_storage +{ + BOOST_PYTHON_ALIGNER(char, 0); + BOOST_PYTHON_ALIGNER(short, 1); + BOOST_PYTHON_ALIGNER(int, 2); + BOOST_PYTHON_ALIGNER(long, 3); + BOOST_PYTHON_ALIGNER(float, 4); + BOOST_PYTHON_ALIGNER(double, 5); + BOOST_PYTHON_ALIGNER(long double, 6); + BOOST_PYTHON_ALIGNER(void*, 7); + BOOST_PYTHON_ALIGNER(function_ptr, 8); + BOOST_PYTHON_ALIGNER(member_ptr, 9); + BOOST_PYTHON_ALIGNER(member_function_ptr, 10); + char bytes[size]; +}; + +# undef BOOST_PYTHON_ALIGNER + +// A metafunction returning a POD type which can store U, where T == +// U&. If T is not a reference type, returns a POD which can store T. +template +struct referent_storage +{ + typedef aligned_storage type; +}; + +}}} // namespace boost::python::detail + +#endif // REFERENT_STORAGE_DWA200278_HPP