cpp_call_stack_allocated.srctree 1.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# tag: cpp

"""
PYTHON setup.py build_ext --inplace
PYTHON -c "from call_stack_allocated import test; test()"
"""

######## setup.py ########

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('*.pyx', language='c++'))

######## call.cpp ########

class wint {
public:
  long long val;
  wint() { val = 0; }
  wint(long long val) { this->val = val; }
  long long &operator()() { return this->val; }
22 23
  long long operator()(long long i) { return this->val + i; }
  long long operator()(long long i, long long j) { return this->val + i + j; }
24 25 26 27 28 29 30 31 32 33
};

######## call.pxd ########

cdef extern from "call.cpp" nogil:
    cppclass wint:
        long long val
        wint()
        wint(long long val)
        long long& operator()()
34 35
        long long operator()(long long i)
        long long operator()(long long i, long long j)
36 37 38 39 40 41 42 43 44 45


######## call_stack_allocated.pyx ########

from call cimport wint
def test():
    cdef wint a = wint(4)
    cdef long long b = 3
    b = a()
    assert b == 4
46 47 48 49
    b = a(1ll)
    assert b == 5
    b = a(1ll, 1ll)
    assert b == 6
50