2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-20 16:52:15 +00:00

Various kinds of cleanup and code massage; preparing for rvalue from_python protocol change

[SVN r14353]
This commit is contained in:
Dave Abrahams
2002-07-08 17:17:31 +00:00
parent 8b611322e5
commit 0945f79ced
13 changed files with 225 additions and 186 deletions

View File

@@ -11,13 +11,14 @@
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/type_traits/transform_traits.hpp>
# include <boost/type_traits/cv_traits.hpp>
# include <boost/python/converter/from_python_data.hpp>
# include <boost/python/converter/rvalue_from_python_data.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/converter/registry.hpp>
# include <boost/python/converter/lvalue_from_python_chain.hpp>
# include <boost/python/converter/rvalue_from_python_chain.hpp>
# include <boost/python/detail/void_ptr.hpp>
# include <boost/python/back_reference.hpp>
# include <boost/python/detail/referent_storage.hpp>
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<T>::type m_result;
typename python::detail::referent_storage<T>::type m_result;
};
// Base class for pointer and reference converters

View File

@@ -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

View File

@@ -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 <boost/python/detail/wrap_python.hpp>
#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

View File

@@ -8,7 +8,7 @@
# include <boost/python/detail/config.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/converter/from_python_stage1_data.hpp>
# include <boost/python/converter/rvalue_from_python_data.hpp>
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(

View File

@@ -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 <boost/python/detail/char_array.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/type_traits/same_traits.hpp>
# include <boost/type_traits/transform_traits.hpp>
# include <boost/static_assert.hpp>
# include <boost/python/converter/from_python_stage1_data.hpp>
# include <boost/type_traits/composite_traits.hpp>
# include <boost/type_traits/cv_traits.hpp>
# include <boost/python/detail/destroy.hpp>
# include <boost/preprocessor/list/for_each_i.hpp>
# include <boost/preprocessor/tuple/to_list.hpp>
# include <boost/preprocessor/cat.hpp>
namespace boost { namespace python { namespace converter {
namespace detail
{
template <class T> struct referent_size;
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T>
struct referent_size<T&>
{
BOOST_STATIC_CONSTANT(
std::size_t, value = sizeof(T));
};
# else
template <class T> 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 <std::size_t target>
union lower_size
{
BOOST_PP_LIST_FOR_EACH_I(
BOOST_PYTHON_CHOOSE_LOWER_SIZE
, ignored, BOOST_PYTHON_ALIGNMENT_TYPES)
};
template <class Align, std::size_t size>
union aligned_storage
{
Align align;
char bytes[size];
};
template <class Reference>
struct referent_storage
{
typedef lower_size<referent_size<Reference>::value> align_t;
typedef aligned_storage<align_t,referent_size<Reference>::value> type;
};
}
template <class T>
struct rvalue_base_data
{
rvalue_stage1_data stage1;
typename detail::referent_storage<
typename add_reference<T>::type
>::type storage;
};
template <class T>
struct rvalue_data : rvalue_base_data<T>
{
rvalue_data(rvalue_stage1_data const&);
rvalue_data(void*);
~rvalue_data();
private:
typedef typename add_reference<typename add_cv<T>::type>::type ref_type;
};
//
// Implementataions
//
template <class T>
inline rvalue_data<T>::rvalue_data(rvalue_stage1_data const& stage1)
{
this->stage1 = stage1;
}
template <class T>
inline rvalue_data<T>::rvalue_data(void* convertible)
{
this->stage1.convertible = convertible;
}
template <class T>
inline rvalue_data<T>::~rvalue_data()
{
if (this->stage1.convertible == this->storage.bytes)
python::detail::destroy_reference<ref_type>(this->storage.bytes);
}
}}} // namespace boost::python::converter
#endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP

View File

@@ -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 <boost/python/converter/from_python_function.hpp>
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

View File

@@ -5,8 +5,7 @@
// to its suitability for any purpose.
#ifndef IMPLICIT_DWA2002326_HPP
# define IMPLICIT_DWA2002326_HPP
# include <boost/python/converter/from_python_data.hpp>
# include <boost/python/converter/from_python_stage1_data.hpp>
# include <boost/python/converter/rvalue_from_python_data.hpp>
# include <boost/python/converter/registrations.hpp>
namespace boost { namespace python { namespace converter {
@@ -22,7 +21,7 @@ struct implicit
find_chain(obj, rvalue_from_python_chain<Source>::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<Target>*)data)->storage.bytes;
void* storage = ((rvalue_from_python_storage<Target>*)data)->storage.bytes;
# if !defined(BOOST_MSVC) || _MSC_FULL_VER != 13012108 // vc7.01 alpha workaround
new (storage) Target(*static_cast<Source*>(intermediate_data.stage1.convertible));
# else

View File

@@ -17,8 +17,8 @@ namespace boost { namespace python { namespace converter {
// Given T == U*cv&, T == U*, or T == U&, lvalue_from_python_chain<T>
// 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 <class T>

View File

@@ -6,7 +6,7 @@
#ifndef REGISTRATIONS_DWA2002223_HPP
# define REGISTRATIONS_DWA2002223_HPP
# include <boost/python/converter/from_python_function.hpp>
# include <boost/python/converter/constructor_function.hpp>
namespace boost { namespace python { namespace converter {

View File

@@ -9,7 +9,8 @@
# include <boost/python/detail/config.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/converter/to_python_function_type.hpp>
# include <boost/python/converter/from_python_function.hpp>
# include <boost/python/converter/rvalue_from_python_data.hpp>
# include <boost/python/converter/constructor_function.hpp>
namespace boost { namespace python { namespace converter {

View File

@@ -7,10 +7,12 @@
# define RETURN_FROM_PYTHON_DWA200265_HPP
# include <boost/python/converter/callback_from_python_base.hpp>
# include <boost/python/converter/from_python_data.hpp>
# include <boost/python/converter/rvalue_from_python_data.hpp>
# include <boost/python/converter/rvalue_from_python_chain.hpp>
# include <boost/python/converter/lvalue_from_python_chain.hpp>
# include <boost/python/detail/void_ptr.hpp>
# include <boost/call_traits.hpp>
# include <boost/python/detail/void_return.hpp>
namespace boost { namespace python { namespace converter {
@@ -19,6 +21,7 @@ namespace detail
template <class T>
struct return_pointer_from_python
{
typedef T result_type;
return_pointer_from_python();
T operator()(PyObject*) const;
};
@@ -26,6 +29,7 @@ namespace detail
template <class T>
struct return_reference_from_python
{
typedef T result_type;
return_reference_from_python();
T operator()(PyObject*) const;
};
@@ -33,8 +37,9 @@ namespace detail
template <class T>
struct return_rvalue_from_python
{
typedef call_traits<T>::param_type result_type;
return_rvalue_from_python();
T const& operator()(PyObject*);
result_type operator()(PyObject*);
private:
rvalue_data<T> m_data;
};
@@ -64,27 +69,20 @@ template <class T>
struct return_from_python
: detail::select_return_from_python<T>::type
{
typedef T result_type;
};
struct void_result
{
private:
void_result() {}
void operator=(void_result const&);
friend struct return_from_python<void>;
};
// Specialization as a convenience for call and call_method
template <>
struct return_from_python<void>
{
typedef void_result result_type;
typedef python::detail::returnable<void>::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 <class T>
inline T const& return_rvalue_from_python<T>::operator()(PyObject* obj)
inline typename return_rvalue_from_python<T>::result_type
return_rvalue_from_python<T>::operator()(PyObject* obj)
{
return *(T*)convert_rvalue(obj, m_data.stage1, m_data.storage.bytes);
}

View File

@@ -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 <boost/python/converter/constructor_function.hpp>
# include <boost/python/detail/referent_storage.hpp>
# include <boost/python/detail/destroy.hpp>
# include <boost/static_assert.hpp>
# include <cstddef>
// 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<T>::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 <class T>
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<T>::type
>::type storage;
};
// Augments rvalue_from_python_storage<T> with a destructor. If
// stage1.convertible == storage.bytes, it indicates that an object of
// remove_reference<T>::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 <class T>
struct rvalue_data : rvalue_from_python_storage<T>
{
// This must always be a POD struct with m_data its first member.
BOOST_STATIC_ASSERT(offsetof(rvalue_from_python_storage<T>,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<typename add_cv<T>::type>::type ref_type;
};
//
// Implementataions
//
template <class T>
inline rvalue_data<T>::rvalue_data(rvalue_from_python_stage1_data const& stage1)
{
this->stage1 = stage1;
}
template <class T>
inline rvalue_data<T>::rvalue_data(void* convertible)
{
this->stage1.convertible = convertible;
}
template <class T>
inline rvalue_data<T>::~rvalue_data()
{
if (this->stage1.convertible == this->storage.bytes)
python::detail::destroy_reference<ref_type>(this->storage.bytes);
}
}}} // namespace boost::python::converter
#endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP

View File

@@ -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 <boost/mpl/select_type.hpp>
# include <boost/preprocessor/list/for_each_i.hpp>
# include <boost/preprocessor/tuple/to_list.hpp>
# include <boost/preprocessor/cat.hpp>
# include <cstddef>
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 <std::size_t size>
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 <class T>
struct referent_storage
{
typedef aligned_storage<sizeof(T)> type;
};
}}} // namespace boost::python::detail
#endif // REFERENT_STORAGE_DWA200278_HPP