88 lines
2.1 KiB
Cython
88 lines
2.1 KiB
Cython
#cython: language_level=3
|
|
|
|
|
|
__c_cython_mapping__={}
|
|
|
|
|
|
cdef class OBIObject:
|
|
|
|
cdef register(self, OBIObject object):
|
|
self._dependent_object[id(object)]=object
|
|
|
|
|
|
cdef unregister(self, OBIObject object):
|
|
del self._dependent_object[id(object)]
|
|
|
|
|
|
cpdef close(self):
|
|
|
|
cdef OBIObject object
|
|
cdef list toclose = list(self._dependent_object.values())
|
|
|
|
for object in toclose:
|
|
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={}
|
|
|
|
|
|
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) :
|
|
return <size_t>(self._pointer)
|
|
|
|
|
|
cpdef close(self):
|
|
|
|
if (self._pointer != NULL):
|
|
OBIObject.close(self)
|
|
self._pointer = NULL
|
|
|
|
assert len(self._dependent_object.values) == 0
|
|
|
|
|
|
cpdef bint active(self):
|
|
return self._pointer != NULL
|
|
|
|
|
|
def __dealloc__(self):
|
|
"""
|
|
Destructor of any OBI instance.
|
|
|
|
The destructor automatically calls the `close` method and
|
|
therefore frees all the associated memory.
|
|
"""
|
|
self.close()
|
|
|
|
|
|
@staticmethod
|
|
cdef object new_wrapper(type constructor, void* pointer):
|
|
|
|
cdef object o
|
|
|
|
if (<size_t>pointer in __c_cython_mapping__):
|
|
return __c_cython_mapping__[<size_t>pointer]
|
|
else:
|
|
o = constructor(<size_t>pointer, 987654)
|
|
o._pointer = <size_t>pointer
|
|
__c_cython_mapping__[<size_t>pointer] = o
|
|
return o
|
|
|
|
|
|
cdef class OBIDeactivatedInstanceError(Exception):
|
|
pass
|
|
|