Skip to content

Instantly share code, notes, and snippets.

@lidizheng
Last active January 30, 2019 19:56
Show Gist options
  • Save lidizheng/b41e6dd47ea2c3bb40d962eb6305ca23 to your computer and use it in GitHub Desktop.
Save lidizheng/b41e6dd47ea2c3bb40d962eb6305ca23 to your computer and use it in GitHub Desktop.
### Current Pattern ###
cdef class CClass:
cdef c_struct *c_ptr
cdef c(self, c_struct *c_ptr):
self.c_ptr = c_ptr
# Do stuff
cdef un_c(self):
# Free resource
def main():
cdef c_struct the_struct
c_class = CClass()
c_class.c(&the_struct)
# -----------------------
### Static Method ###
cdef class CClass:
cdef c_struct *c_ptr
def __cinit__(self):
self.c_ptr = NULL
# Initialize other resources
def __dealloc__(self):
# Free resources
@staticmethod
cdef from_ptr(c_struct *c_ptr):
cdef CClass c_class = CClass()
c_class.c_ptr = c_ptr
return c_class
cdef close(self):
# Free reosurces
# -----------------------
### C-like Pattern ###
cdef class CClass:
cdef c_struct *c_ptr
def __cinit__(self):
self.c_ptr = NULL
def __dealloc__(self):
# Free resources
cdef c_class_create(c_struct *c_ptr):
cdef CClass c_class = CClass()
c_class.c_ptr = c_ptr
return c_class
cdef c_class_destroy(CClass c_class):
# Free resources
# -----------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment