Subclassing stl containers

Asked by Sebastian Luther

I'm wrapping an API which defines a class like this:

class X : public std::vector<Y>
{
//...
};

I tried:
Yvec = mod.add_container('std::vector<Y>', 'Y', 'vector')
mod.add_class('X',parent=Yvec)

But this doesn't work as add_class expects a CppClass, which Yvec isn't. Is there a way to do this?

Question information

Language:
English Edit question
Status:
Solved
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Solved by:
Sebastian Luther
Solved:
Last query:
Last reply:
Revision history for this message
Gustavo Carneiro (gjc) said :
#1

I think this might work:

  X=mod.add_class('X',parent=Yvec)
  X.add_container_traits(retval("Y"))

Revision history for this message
Sebastian Luther (sebastianluther) said :
#2

Calling

Yvec = mod.add_container('std::vector<Y>', 'Y', 'vector')
X=mod.add_class('X',parent=Yvec)

already results in a traceback:
TypeError: 'parent' must be None, CppClass instance, or a list of CppClass instances

Revision history for this message
Gustavo Carneiro (gjc) said :
#3

The point of add_container_traits is that this way you don't need to inherit from the std::vector class. Just remove the parent=Yvec parameter.

Revision history for this message
Sebastian Luther (sebastianluther) said :
#4

Ah, I think it works. At least it's now possible to iterate over the elements of an X object. What does not work is for example len(X) or indexing (__getitem__). Is this a limitation of the container?

Revision history for this message
Gustavo Carneiro (gjc) said :
#5

It's just a limitation in pybindgen, functionality not implemented...
As a workaround, you could create a list from your object, and then you can use the normal list len/getitem.