jsam
8/13/2013 - 9:35 PM

pyobjs proposal

pyobjs proposal

cdef class CArray:
    """Class for representing c-array. Due to lack of void ptr arithmetic support there is no void casting magic, therefore per type casting is needed."""

    cdef public list PyList
    cdef public unsigned int PyListSize
        
    def __init__(self, arr=None):
        dprint("Initialize CArray in __init__")
        if arr is not None:
            self.PyList = self.fix_args(arr)
            self.PyListSize = <unsigned int> len(self.PyList)
            dprint("CArray values initialized: {0}".format(self.PyList, self.PyListSize))
        else:
            dprint("CArray(arr=None)")


    def fix_args(self, arr):
        if type(arr) == list:
            return arr
        else:
            return list(arr)

    def get_from_ptr(self, unsigned long long ptr, char *type, unsigned long long arr_size):
        dprint("CArray().get_from_ptr({0}, {1}, {2})".format(ptr, type, arr_size))
        ret = []
        if str(type) == "i":
            arr_cast = ctypes.cast(ptr, ctypes.POINTER(ctypes.c_int))
            for i in xrange(arr_size):
                ret.append(arr_cast[i])
                
        # TODO: remaining types 
        return ret
        
        
    cdef int* as_int(self):
        dprint("...converting to int array")
        cdef int *int_t = <int*> malloc(cython.sizeof(int) * self.PyListSize)
        if int_t is NULL:
            raise MemoryError()
        for i in xrange(self.PyListSize):
            int_t[i] = self.PyList[i]
        return int_t