[C++-sig] Wrap std::vector<pointer*>

Matthew Bradbury matt-bradbury at live.co.uk
Wed Aug 3 23:36:05 CEST 2011


Simon W <simwarg <at> gmail.com> writes:

> 
> 
> >From my research it seems not very trivial to wrap std::vector that holds
pointer types. For example:    std::vector<GameObject*>I've looked at boost
python vector_index_suite but it just gives me the runtime error:> TypeError: No
to_python (by-value) converter found for C++ type:> GameObject*I have already
exposed GameObject:    class_<GameObject>("
> GameObject") ...
> So
>  it have come to my understanding it's not possible with an out of the 
> box solution. I have to do some kind of wrapper? Could someone just help
>  me out where I should start?Thank you!
> 
> 


I've come across this before as well and use the following to provide support
for vectors of pointers.
I only need to iterate over them so have only modified the __iter__ method.

template <class Container>
class vector_ptr_indexing_suite
    : public vector_indexing_suite<Container, true,
vector_ptr_indexing_suite<Container>>
{
public:
    typedef iterator<Container, return_value_policy<reference_existing_object>>
def_iterator;

    template <class Class>
    static void
    extension_def(Class & cl)
    {
        vector_indexing_suite<Container, true,
vector_ptr_indexing_suite<Container>>::extension_def(cl);

        cl.def("__iter__", def_iterator());
    }
};


class_<std::vector<Object*>>("ObjectContainer")
    .def(vector_ptr_indexing_suite<std::vector<Object*>>())
;


You may want to override functions such as get_item, whose implementation might
look like:

static object get_item(Container& container, index_type i)
{ 
    return object(ptr(container[i]));
}


Anyway have a good look in vector_indexing_suite.hpp to see what is there and
what you might need to hack at.



More information about the Cplusplus-sig mailing list