[C++-sig] Re: Boost.Python v2: beginning of generalized object support

Dave Hawkes daveh at cadlink.com
Mon Jun 17 05:08:11 CEST 2002


The object code seems to work well with the few tests I did with some spare
time this evening. The only thing we're missing now is the class support.
Maybe something fairly minimal as appended at the bottom here would suffice.
Chances are most people would just want to construct it from a regular
object anyway.


Dave Hawkes



namespace boost { namespace python {

namespace detail {
void *find_class_ptr(PyObject *p, type_info const& ti);
}

template<class T>
struct class_object : public object
{
    typedef object base;
    class_object() : m_class_ptr(NULL) {}
    class_object(object const& o) : base(o) { init(); }
    class_object& operator=(object const& o)
    {
        base::operator=(o);
        init();
        return *this;
    }
    T* operator->() const { return m_class_ptr; }
    T& operator*() const { return *m_class_ptr; }
private:
    void init()
    {
        m_class_ptr =
reinterpret_cast<T*>(detail::find_class_ptr(base::operator->(),
type_id<T>()));
    }
    T* m_class_ptr;
};

namespace detail {

// implementation in a .cpp somewhere
void *find_class_ptr(PyObject *p, type_info const& ti)
{
    void *class_ptr = NULL;
    if(p) {
        class_ptr = objects::find_instance_impl(p, ti);
        if(!class_ptr) {
            PyErr_SetString(
                PyExc_ReferenceError
                , const_cast<char*>("attempt to find a class pointer from a
non class object"));
            throw_error_already_set();
        }
    } else
        class_ptr = NULL;
    return class_ptr;
}

}

}}










More information about the Cplusplus-sig mailing list