def _get_raw_address(self, space, w_obj, offset): rawobject = get_rawobject_nonnull(space, w_obj) assert lltype.typeOf(rawobject) == capi.C_OBJECT if rawobject: fieldptr = capi.direct_ptradd(rawobject, offset) else: fieldptr = rffi.cast(capi.C_OBJECT, offset) return fieldptr
def to_memory(self, space, w_obj, w_value, offset): # copy only the pointer value rawobject = get_rawobject_nonnull(space, w_obj) byteptr = rffi.cast(rffi.VOIDPP, capi.direct_ptradd(rawobject, offset)) buf = space.getarg_w('s*', w_value) try: byteptr[0] = rffi.cast(rffi.VOIDP, buf.get_raw_address()) except ValueError: raise oefmt(space.w_TypeError, "raw buffer interface not supported")
def _unwrap_object(self, space, w_obj): from pypy.module._cppyy.interp_cppyy import W_CPPInstance if isinstance(w_obj, W_CPPInstance): if capi.c_is_subtype(space, w_obj.cppclass, self.cppclass): rawobject = w_obj.get_rawobject() offset = capi.c_base_offset(space, w_obj.cppclass, self.cppclass, rawobject, 1) obj_address = capi.direct_ptradd(rawobject, offset) return rffi.cast(capi.C_OBJECT, obj_address) raise oefmt(space.w_TypeError, "cannot pass %T as %s", w_obj, self.cppclass.name)
def _unwrap_object(self, space, w_obj): from pypy.module._cppyy.interp_cppyy import W_CPPInstance if isinstance(w_obj, W_CPPInstance): from pypy.module._cppyy.interp_cppyy import INSTANCE_FLAGS_IS_RVALUE if w_obj.rt_flags & INSTANCE_FLAGS_IS_RVALUE: # reject moves as all are explicit raise ValueError("lvalue expected") if capi.c_is_subtype(space, w_obj.clsdecl, self.clsdecl): rawobject = w_obj.get_rawobject() offset = capi.c_base_offset(space, w_obj.clsdecl, self.clsdecl, rawobject, 1) obj_address = capi.direct_ptradd(rawobject, offset) return rffi.cast(capi.C_OBJECT, obj_address) raise oefmt(space.w_TypeError, "cannot pass %T instance as %s", w_obj, self.clsdecl.name)
def to_memory(self, space, w_obj, w_value, offset): # the actual data member is of object* type, but we receive a pointer to that # data member in order to modify its value, so by convention, the internal type # used is object** address = rffi.cast(rffi.VOIDPP, self._get_raw_address(space, w_obj, offset)) from pypy.module._cppyy.interp_cppyy import W_CPPInstance cppinstance = space.interp_w(W_CPPInstance, w_value, can_be_None=True) if cppinstance: rawobject = cppinstance.get_rawobject() offset = capi.c_base_offset(space, cppinstance.clsdecl, self.clsdecl, rawobject, 1) obj_address = capi.direct_ptradd(rawobject, offset) address[0] = rffi.cast(rffi.VOIDP, obj_address); # register the value for potential recycling from pypy.module._cppyy.interp_cppyy import memory_regulator memory_regulator.register(cppinstance) else: raise oefmt(space.w_TypeError, "cannot pass %T instance as %s", w_value, self.clsdecl.name)
def to_memory(self, space, w_obj, w_value, offset): from pypy.module._cppyy.interp_cppyy import W_CPPInstance cppinstance = space.interp_w(W_CPPInstance, w_value, can_be_None=True) if cppinstance: # get the object address from value, correct for hierarchy offset rawobject = cppinstance.get_rawobject() base_offset = capi.c_base_offset(space, cppinstance.clsdecl, self.clsdecl, rawobject, 1) rawptr = capi.direct_ptradd(rawobject, base_offset) # get the data member address and write the pointer in address = rffi.cast(rffi.VOIDPP, self._get_raw_address(space, w_obj, offset)) address[0] = rffi.cast(rffi.VOIDP, rawptr) # register the value object for potential recycling from pypy.module._cppyy.interp_cppyy import memory_regulator memory_regulator.register(cppinstance) else: raise oefmt(space.w_TypeError, "cannot pass %T instance as %s", w_value, self.clsdecl.name)
def descr_next(self, space): if self.w_seq is None: raise OperationError(space.w_StopIteration, space.w_None) if self.len <= self.index: self.w_seq = None raise OperationError(space.w_StopIteration, space.w_None) try: from pypy.module._cppyy import capi # TODO: refector offset = capi.direct_ptradd(rffi.cast(C_OBJECT, self.data), self.index * self.stride) w_item = self.converter.from_memory(space, space.w_None, space.w_None, offset) except OperationError as e: self.w_seq = None if not e.match(space, space.w_IndexError): raise raise OperationError(space.w_StopIteration, space.w_None) self.index += 1 return w_item
def _unwrap_object(self, space, w_obj): from pypy.module._cppyy.interp_cppyy import W_CPPInstance if isinstance(w_obj, W_CPPInstance): # w_obj could carry a 'hidden' smart ptr or be one, cover both cases have_match = False if w_obj.smartdecl and capi.c_is_subtype(space, w_obj.smartdecl, self.smartdecl): # hidden case, do not derefence when getting obj address have_match = True rawobject = w_obj._rawobject # TODO: this direct access if fugly offset = capi.c_base_offset(space, w_obj.smartdecl, self.smartdecl, rawobject, 1) elif capi.c_is_subtype(space, w_obj.clsdecl, self.smartdecl): # exposed smart pointer have_match = True rawobject = w_obj.get_rawobject() offset = capi.c_base_offset(space, w_obj.clsdecl, self.smartdecl, rawobject, 1) if have_match: obj_address = capi.direct_ptradd(rawobject, offset) return rffi.cast(capi.C_OBJECT, obj_address) raise oefmt(space.w_TypeError, "cannot pass %T instance as %s", w_obj, self.rawdecl.name)