swigging polymorphic collections...

Stephane Conversy conversy at lri.fr
Mon Aug 30 09:15:07 EDT 1999


very very bad trick (better solution welcome !!) :

what I did:

class SubNode1;

       class Node {
            public:
                int n;
                enum node_type {
                    SubNode1_t
                };
                node_type type;

                SubNode1*
                toSubNode1() {
                    return dynamic_cast<SubNode1*>(this);
                }
            };

        class SubNode1 : public Node {
            public: int sn1;
                SubNode1() : type(SubNode1_t) {}
        };

test the type in python and cast:

    for i in list:
            if(i.type==SubNode1_t):
                j = i.toSubNode1()





Bjorn Pettersen wrote:

> I've got a c++ class hierarchy rooted at Node:
>
>         class Node { public: int n; };
>         class SubNode1 : public Node { public: int sn1; };
>
> and a list:
>
>         class List {
>                 std::vector<Node*> lst;
>         public:
>                 void  append(Node* n);
>                 Node* __getitem__(int key);
>         };
>
> So far everything is fine, however, when my c++ module returns a List, I can
> never access "sn1" even though the actual Node is a SubNode1.
>
> Has anyone found a solution to this?  Should I be using a PyList object
> instead? -- how would I insert the nodes so they would be Python shadow
> classes?
>
> -- bjorn

--
Stéphane Conversy
http://www-ihm.lri.fr/~conversy/
mailto:conversy at lri.fr







More information about the Python-list mailing list