Cython Object API
This commit is contained in:
@ -6,7 +6,7 @@ cdef dict __c_cython_mapping__
|
||||
|
||||
cdef class OBIObject:
|
||||
|
||||
cdef dict _dependent_object
|
||||
cdef dict _dependent_objects
|
||||
|
||||
cdef register(self, OBIObject object)
|
||||
cdef unregister(self, OBIObject object)
|
||||
@ -16,12 +16,12 @@ cdef class OBIObject:
|
||||
cdef class OBIWrapper(OBIObject):
|
||||
|
||||
cdef void* _pointer
|
||||
cdef size_t cid(self)
|
||||
|
||||
cpdef bint active(self)
|
||||
|
||||
cdef inline size_t cid(self)
|
||||
cdef inline bint active(self)
|
||||
|
||||
@staticmethod
|
||||
cdef object new_wrapper(type constructor, void* pointer)
|
||||
cdef object new(type constructor, void* pointer)
|
||||
|
||||
|
||||
cdef class OBIObjectClosedInstance(Exception):
|
||||
|
@ -1,83 +1,85 @@
|
||||
#cython: language_level=3
|
||||
|
||||
|
||||
__c_cython_mapping__={}
|
||||
__c_cython_mapping__ = {}
|
||||
|
||||
|
||||
cdef class OBIObject:
|
||||
|
||||
def __init__(self, __internalCall__) :
|
||||
|
||||
if __internalCall__ != 987654 or \
|
||||
type(self) == OBIObject or \
|
||||
type(self) == OBIWrapper or \
|
||||
not isinstance(self, OBIObject) :
|
||||
|
||||
raise RuntimeError('OBIObject constructor can not be called directly')
|
||||
|
||||
self._dependent_objects = {}
|
||||
|
||||
|
||||
cdef register(self, OBIObject object):
|
||||
self._dependent_object[id(object)]=object
|
||||
self._dependent_objects[id(object)] = object
|
||||
|
||||
|
||||
cdef unregister(self, OBIObject object):
|
||||
del self._dependent_object[id(object)]
|
||||
del self._dependent_objects[id(object)]
|
||||
|
||||
|
||||
cpdef close(self):
|
||||
|
||||
cdef OBIObject object
|
||||
cdef list toclose = list(self._dependent_object.values())
|
||||
cdef list to_close = list((self._dependent_objects).values())
|
||||
|
||||
for object in toclose:
|
||||
for object in to_close:
|
||||
object.close()
|
||||
|
||||
assert len(self._dependent_object.values)==0
|
||||
|
||||
|
||||
def __init__(self, __internalCall__):
|
||||
|
||||
if __internalCall__ != 987654 :
|
||||
raise RuntimeError('OBIObject constructor can not be called directly')
|
||||
|
||||
if type(self) == OBIObject or type(self) == OBIWrapper or not isinstance(self, OBIObject) :
|
||||
raise RuntimeError('OBIObject constructor can not be called directly')
|
||||
|
||||
self._dependent_object={}
|
||||
assert len(self._dependent_objects.values()) == 0
|
||||
|
||||
|
||||
cdef class OBIWrapper(OBIObject):
|
||||
"""
|
||||
The OBIWrapper class enables to wrap a C object representing a DMS or an element from a DMS
|
||||
"""
|
||||
cdef class OBIWrapper(OBIObject) :
|
||||
'''
|
||||
The OBIWrapper class enables to wrap a C object representing a DMS or an element from a DMS.
|
||||
'''
|
||||
|
||||
cdef size_t cid(self) :
|
||||
cdef inline size_t cid(self) :
|
||||
return <size_t>(self._pointer)
|
||||
|
||||
|
||||
cdef inline bint active(self) :
|
||||
return self._pointer != NULL
|
||||
|
||||
|
||||
cpdef close(self):
|
||||
|
||||
if (self._pointer != NULL):
|
||||
OBIObject.close(self)
|
||||
del __c_cython_mapping__[<size_t>self._pointer]
|
||||
self._pointer = NULL
|
||||
|
||||
assert len(self._dependent_object.values) == 0
|
||||
|
||||
|
||||
cpdef bint active(self):
|
||||
return self._pointer != NULL
|
||||
|
||||
assert len(self._dependent_objects.values()) == 0
|
||||
|
||||
|
||||
def __dealloc__(self):
|
||||
"""
|
||||
'''
|
||||
Destructor of any OBI instance.
|
||||
|
||||
The destructor automatically calls the `close` method and
|
||||
therefore frees all the associated memory.
|
||||
"""
|
||||
therefore closes and frees all associated objects and memory.
|
||||
'''
|
||||
self.close()
|
||||
|
||||
|
||||
@staticmethod
|
||||
cdef object new_wrapper(type constructor, void* pointer):
|
||||
cdef object new(type constructor, void* pointer) :
|
||||
|
||||
cdef object o
|
||||
cdef OBIWrapper o
|
||||
|
||||
if (<size_t>pointer in __c_cython_mapping__):
|
||||
print("Pointer already in cython dict")
|
||||
return __c_cython_mapping__[<size_t>pointer]
|
||||
else:
|
||||
o = constructor(<size_t>pointer, 987654)
|
||||
o._pointer = <size_t>pointer
|
||||
o = constructor(987654)
|
||||
o._pointer = pointer
|
||||
__c_cython_mapping__[<size_t>pointer] = o
|
||||
return o
|
||||
|
||||
|
Reference in New Issue
Block a user