Wrap std::vector<pointer*>

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrap std::vector<pointer*>

mrmclovin
>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!

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Wrap std::vector<pointer*>

paulcmnt
You don't really provide that much information: you want help with writing a wrapper? do you want C++ and Python to point to the same instance of `GameObject`? If it's the latter, have you tried doing `vector<boost::shared_ptr<GameObject>>`?
Reply | Threaded
Open this post in threaded view
|

Re: Wrap std::vector<pointer*>

mrmclovin
I wrap my c++ vector like:
class_<GameObject*>("GameObjectList")
.def(vector_index_suite<GameObject*>);

When I run the following in python:

objects = gameobject_manager.getGameObjects() # getGameObjects is returning a std::vector<GameObject*>
for object in objects:
   ...

I get the error
> TypeError: No to_python (by-value) converter found for C++ type:
> GameObject*

I have not tried shared_ptr because I waas hoping on another solution since it would require a lot of changes to make it a shared_ptr.

On Mon, Aug 1, 2011 at 4:58 PM, diego_pmc <[hidden email]> wrote:
You don't really provide that much information: you want help with writing a
wrapper? do you want C++ and Python to point to the same instance of
`GameObject`? If it's the latter, have you tried doing
`vector&lt;boost::shared_ptr&lt;GameObject&gt;>`?

--
View this message in context: http://boost.2283326.n4.nabble.com/Wrap-std-vector-pointer-tp3708421p3709907.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig


_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Wrap std::vector<pointer*>

Matthew Bradbury
In reply to this post by mrmclovin
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.

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig