cypclass_multiple_inheritance.pyx 1.3 KB
Newer Older
gsamain's avatar
gsamain committed
1
# mode: run
2
# tag: cpp, cpp11, pthread
gsamain's avatar
gsamain committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# cython: experimental_cpp_class_def=True, language_level=2

cdef cypclass Base:
    int base
    __init__(self, int arg):
        self.base = arg

cdef cypclass InplaceAddition(Base):
    __init__(self, int arg):
        Base.__init__(self, arg)

    InplaceAddition __iadd__(self, InplaceAddition other):
        self.base += other.base

    void print_IA_base(self) with gil:
        print self.base

cdef cypclass InplaceSubstraction(Base):
    __init__(self, int arg):
        Base.__init__(self, arg)

    InplaceSubstraction __isub__(self, InplaceSubstraction other):
        self.base -= other.base

    void print_IS_base(self) with gil:
        print self.base

cdef cypclass Diamond(InplaceAddition, InplaceSubstraction):
    __init__(self, int a, int b):
        InplaceAddition.__init__(self, a)
        InplaceSubstraction.__init__(self, b)

def test_non_virtual_inheritance():
    """
    >>> test_non_virtual_inheritance()
    1
    2
    3
    0
    """
    cdef Diamond diamond = Diamond(1, 2)

    diamond.print_IA_base()
    diamond.print_IS_base()

    cdef InplaceAddition iadd_obj = InplaceAddition(2)
    cdef InplaceSubstraction isub_obj = InplaceSubstraction(2)

    diamond += iadd_obj
    diamond -= isub_obj

    diamond.print_IA_base()
    diamond.print_IS_base()