Boost logo

Boost :

From: Anton Gluck (gluc_at_[hidden])
Date: 2000-11-21 01:07:30


Dave,

My work with py_cpp has been going very well lately, and it proved to be a
very useful tool for me. I have now a a quite good Python interface to
many of the classes in a dll which I am trying to expose. However, I am
having a problem exposing an iterator that is a typedef for const_iterator
of vector. This is meant to be an iterator through a vector of variables
(Vars).

My first problem is how to expose the iterator. This is how it is defined
in VarList.h:

typedef std::vector<Var*>::const_iterator VarListConstIter;

This is the code from VarList.cpp:

VarList::VarListConstIter VarList::Begin() const {
    return fMe.begin();
}

In my wrapper code I tried to expose VarList.Begin() like this:

VarList_class.def(VarList::Begin, "Begin");

And these are the errors I got:
d:\py_cpp/extclass.h(280) : error C2665: 'py_extension_class_converters'
: none of the 14 overloads can convert parameter 1 from type 'struct
py::Type<class Var * const *>'
        d:\py_cpp/caller.h(113) : see reference to function template
instantiation 'struct _object *__cdecl py::to_python(class Var *const
*const & )' being compiled
d:\py_cpp/extclass.h(280) : error C2228: left of '.to_python' must have
class/struct/union type
        d:\py_cpp/caller.h(113) : see reference to function template
instantiation 'struct _object *__cdecl py::to_python(class Var *const
*const & )' being compiled

I tried to write my own to_python function by including this (the idea
was to write a to_python for the return of a pointer to a Var):

template class py::ExtensionClass<Var>;
PyObject* to_python(const Var* v)
{
        return to_python(*v);
}

But the error messages stayede the same.

I have also tried to expose VarListConstIter itself:

py::ClassWrapper<VarList::VarListConstIter>
VarListConstIter_class(ClusterForPy, "VarListConstIter");

But this didn't go down well at all:
d:\py_cpp/class_wrapper.h(47) : error C2143: syntax error : missing ','
before '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(47) : error C2059: syntax error : '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(53) : error C2143: syntax error : missing ','
before '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(53) : error C2059: syntax error : '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(58) : error C2143: syntax error : missing ','
before '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(58) : error C2059: syntax error : '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(63) : error C2143: syntax error : missing ','
before '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled
d:\py_cpp/class_wrapper.h(63) : error C2059: syntax error : '<tag>::*'
        D:\cluster\pyCPP\clusterglue\ClusterForPy\glue.cpp(155) : see
reference to class template instantiation 'py::ClassWrapper<class Var *
const *,class py::HeldInstance<class Var * const *> >' being compiled

I think the problem here is with the return of pointers, which I tried to
solve by adding my own to_python. But I've probably not written it
correctly. Does this require one of the three "dangerous" to_python
approaches described in your documentation? Could you help me out once
again?

The second question I have here is more conceptual: In C++ const_iterator
can be used in a for loop, going from address a to address z. In my case
it would be something like this:

for (VarList::VarListConstIter iter = varlist.Begin(); iter !=
varlist.End(); iter++) {
    Var v = *iter;
    // do something with each variable
}

Is it possible to do this with the exposed iterator in Python? If not, how
can I loop through the list of Vars, which is of unknown length? Do I need
to add a new function to VarList that will allow me to create a normal for
loop in Python (one that goes from 0 to the length of the list)? Should I
write a function that allows me access to the vector of Vars directly, so
that I could determine its length for example?

Thanks once again,

Toni


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk