Commit 77420aa2 authored by Stefan Behnel's avatar Stefan Behnel

Merge branch 'master' of git+ssh://github.com/cython/cython

parents c012ad6f 12ca46bf
cdef extern from *:
ctypedef bint bool
ctypedef void* nullptr_t
nullptr_t nullptr
from libcpp cimport bool, nullptr_t, nullptr
cdef extern from "<memory>" namespace "std" nogil:
cdef cppclass unique_ptr[T]:
unique_ptr()
unique_ptr(nullptr_t)
unique_ptr(T*)
unique_ptr(unique_ptr[T]&)
# Modifiers
T* release()
void reset()
void reset(nullptr_t)
void reset(T*)
void swap(unique_ptr&)
# Observers
T* get()
T& operator*()
#T* operator->() # Not Supported
bool operator bool() # Not Supported
bool operator==(const unique_ptr&)
bool operator!=(const unique_ptr&)
bool operator<(const unique_ptr&)
bool operator>(const unique_ptr&)
bool operator<=(const unique_ptr&)
bool operator>=(const unique_ptr&)
bool operator==(nullptr_t)
bool operator!=(nullptr_t)
# Forward Declaration not working ("Compiler crash in AnalyseDeclarationsTransform")
#cdef cppclass weak_ptr[T]
cdef cppclass shared_ptr[T]:
shared_ptr()
shared_ptr(nullptr_t)
shared_ptr(T*)
shared_ptr(shared_ptr[T]&)
shared_ptr(shared_ptr[T]&, T*)
shared_ptr(unique_ptr[T]&)
#shared_ptr(weak_ptr[T]&) # Not Supported
# Modifiers
void reset()
void reset(T*)
void swap(shared_ptr&)
# Observers
T* get()
T& operator*()
#T* operator->() # Not Supported
long use_count()
bool unique()
bool operator bool() # Not Supported
#bool owner_before[Y](const weak_ptr[Y]&) # Not Supported
bool owner_before[Y](const shared_ptr[Y]&)
bool operator==(const shared_ptr&)
bool operator!=(const shared_ptr&)
bool operator<(const shared_ptr&)
bool operator>(const shared_ptr&)
bool operator<=(const shared_ptr&)
bool operator>=(const shared_ptr&)
bool operator==(nullptr_t)
bool operator!=(nullptr_t)
cdef cppclass weak_ptr[T]:
weak_ptr()
weak_ptr(weak_ptr[T]&)
weak_ptr(shared_ptr[T]&)
# Modifiers
void reset()
void swap(weak_ptr&)
# Observers
long use_count()
bool expired()
shared_ptr[T] lock()
bool owner_before[Y](const weak_ptr[Y]&)
bool owner_before[Y](const shared_ptr[Y]&)
......@@ -37,7 +37,7 @@ We could implement custom ``__getitem__``, ``__setitem__``, etc. for this,
but instead we'll use the buffer protocol to expose the matrix's data to Python
so we can use NumPy to do useful work.
Implementing the buffer protocol requires adding two members,
Implementing the buffer protocol requires adding two methods,
``__getbuffer__`` and ``__releasebuffer__``,
which Cython handles specially.
......@@ -62,7 +62,7 @@ which Cython handles specially.
def __getbuffer__(self, Py_buffer *buffer, int flags):
cdef Py_ssize_t itemsize = sizeof(self.v[0])
self.shape[0] = self.v.size()
self.shape[0] = self.v.size() / self.ncols
self.shape[1] = self.ncols
# Stride 1 is the distance, in bytes, between two items in a row;
......@@ -165,7 +165,7 @@ The ``flags`` argument to ``__getbuffer__`` comes from ``np.asarray``
that describe the kind of array that is requested.
Strictly speaking, if the flags contain ``PyBUF_ND``, ``PyBUF_SIMPLE``,
or ``PyBUF_F_CONTIGUOUS``, ``__getbuffer__`` must raise a ``BufferError``.
These macros can be ``cimport``'d from the pseudo-package ``cython``.
These macros can be ``cimport``'d from ``cpython.buffer``.
(The matrix-in-vector structure actually conforms to ``PyBUF_ND``,
but that would prohibit ``__getbuffer__`` from filling in the strides.
......
# mode: run
# tag: cpp
from libcpp.memory cimport unique_ptr, shared_ptr
cdef extern from "cpp_smart_ptr_helper.h":
cdef cppclass CountAllocDealloc:
CountAllocDealloc(int*, int*)
def test_unique_ptr():
"""
>>> test_unique_ptr()
"""
cdef int alloc_count = 0, dealloc_count = 0
cdef unique_ptr[CountAllocDealloc] x_ptr
x_ptr.reset(new CountAllocDealloc(&alloc_count, &dealloc_count))
assert alloc_count == 1
x_ptr.reset()
assert alloc_count == 1
assert dealloc_count == 1
class CountAllocDealloc {
public:
CountAllocDealloc(int* alloc_count, int* dealloc_count)
: _alloc_count(alloc_count), _dealloc_count(dealloc_count) {
(*_alloc_count)++;
}
~CountAllocDealloc() {
(*_dealloc_count)++;
}
private:
int* _alloc_count;
int* _dealloc_count;
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment