DikobrAz
DikobrAz

Reputation: 3737

c++ class in fused type

I wish to implement python wrapper for a bunch of c++ classes. Somewhere in pxd I have:

cdef cppclass FooImpl1:
    FooImpl1()
    int foo()

cdef cppclass FooImpl2
    FooImpl2()
    int foo()

I wonder if I can write something like this in pyx python wrapper:

ctypedef fused FooImpl:
    FooImpl1*
    FooImpl2*

cdef class Foo:
    cdef FooImpl impl
    def __cinit__(self, int selector):
        if selector == 1:
            self.impl = new FooImpl1()
        else:
            self.impl = new FooImpl2()

    def func(self):
        # depending on the object stored in impl FooImpl2::foo or FooImpl1::foo
        # will be called
        return self.impl.foo()

Is there a way to accomplish expected behavior? FooImpl1 and FooImpl2 don't share abstract interface, they are template specializations of a class.

Upvotes: 3

Views: 648

Answers (1)

ratiotile
ratiotile

Reputation: 963

As of this version (0.20), Cython doesn't support fused types in classes, only in function parameters and variables. Here are the docs.

Upvotes: 4

Related Questions