2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-22 17:32:55 +00:00

added class wrapping

[SVN r12384]
This commit is contained in:
Dave Abrahams
2002-01-20 23:52:36 +00:00
parent 3d03ca3d10
commit dabb22bb6a
5 changed files with 150 additions and 30 deletions

View File

@@ -6,16 +6,44 @@
#ifndef CLASS_DWA20011214_HPP
# define CLASS_DWA20011214_HPP
# include <boost/python/module.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/utility.hpp>
# include <boost/python/converter/type_id.hpp>
# include <boost/python/reference.hpp>
# include <boost/iterator_adaptors.hpp>
# include <cstddef>
namespace boost { namespace python { namespace objects {
namespace boost { namespace python {
class module;
namespace objects {
template <class T> struct holder;
// To identify a class, we don't need cv/reference decorations
typedef converter::undecorated_type_id_t class_id;
struct BOOST_PYTHON_DECL class_base : noncopyable
{
// constructor
class_base(
module& name_space // Which name space the class will live in
, char const* name // The name of the class
, std::size_t num_types // A list of class_ids. The first is the type
, class_id const*const types // this is wrapping. The rest are the types of
// any bases.
);
// Retrieve a pointer to the underlying object
PyObject* object() const { return m_object.get(); }
private:
ref m_object;
};
// Base class for all holders
struct BOOST_PYTHON_DECL instance_holder : noncopyable
{
@@ -28,7 +56,7 @@ struct BOOST_PYTHON_DECL instance_holder : noncopyable
virtual void* holds(converter::type_id_t) = 0;
void install(PyObject* inst);
void install(PyObject* inst) throw();
struct iterator_policies : default_iterator_policies
{
@@ -58,6 +86,8 @@ struct instance
instance_holder* objects;
};
// Given a type_id, find the instance data which corresponds to it, or
// return 0 in case no such type is held.
BOOST_PYTHON_DECL void* find_instance_impl(PyObject*, converter::type_id_t);
template <class T>
@@ -66,8 +96,8 @@ T* find_instance(PyObject* p, T* = 0)
return static_cast<T*>(find_instance_impl(p, converter::type_id<T>()));
}
BOOST_PYTHON_DECL PyTypeObject* class_metatype();
BOOST_PYTHON_DECL PyTypeObject* class_type();
BOOST_PYTHON_DECL ref class_metatype();
BOOST_PYTHON_DECL ref class_type();
//
// implementation

View File

@@ -18,14 +18,14 @@ struct class_unwrapper
template <class Target>
struct reference_unwrapper : converter::unwrapper<Target>
{
bool convertible(PyObject* p) const
void* can_convert(PyObject* p) const
{
return find_holder<T>(p) != 0;
return find_instance<T>(p);
}
Target convert(PyObject* p, void*&) const
Target convert(PyObject* p, void* data, ) const
{
return *find_holder<T>(p)->target();
return *find_instance<T>(p)->target();
}
};

View File

@@ -22,10 +22,16 @@ struct BOOST_PYTHON_DECL function : PyObject
~function();
PyObject* call(PyObject*, PyObject*) const;
void add_overload(function* overload);
// Add an attributeto the name_space with the given name. If it is
// a function object (this class), and an existing function is
// already there, add it as an overload.
static void add_to_namespace(
PyObject* name_space, char const* name, PyObject* attribute);
private: // helper functions
void argument_error(PyObject* args, PyObject* keywords) const;
void add_overload(function* overload);
private: // data members
py_function m_fn;

View File

@@ -8,6 +8,8 @@
# include <boost/python/object/class.hpp>
# include <boost/python/converter/type_id.hpp>
# include <boost/python/object/inheritance.hpp>
# include <boost/ref.hpp>
namespace boost { namespace python { namespace objects {
@@ -76,9 +78,11 @@ struct value_holder_generator
};
template <class Held>
void* value_holder<Held>::holds(converter::type_id_t x)
void* value_holder<Held>::holds(converter::type_id_t dst_t)
{
return x == converter::type_id<Held>() ? &m_held : 0;
converter::type_id_t src_t = converter::type_id<Held>();
return src_t == dst_t ? &m_held
: find_static_type(&m_held, src_t, dst_t);
}
}}} // namespace boost::python::objects