Skip to content

Commit

Permalink
support for non-rootptr objects to call destroy()
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuyadong committed Aug 17, 2024
1 parent 08be525 commit 1371c2f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/zoop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub const MetaInfo = packed struct {
pub const IObject = struct {
pub const Vtable = struct {
__meta__: *const fn (*anyopaque) *MetaInfo,
__destroy__: *const fn (*anyopaque) void,
destroy: *const fn (*anyopaque) void,
formatAny: *const fn (*anyopaque, writer: std.io.AnyWriter) anyerror!void,
};
Expand Down Expand Up @@ -469,10 +470,19 @@ fn CoreFn(comptime Class: type) type {
return &pself.mixin.meta.?;
}
},
struct {
pub fn __destroy__(self: *Class) void {
destroyObj(self);
}
},
struct {
pub fn destroy(self: *Class) void {
if (self.mixin.meta == null) @panic("obj create by make() must call obj.initMixin() before use it.");
destroyObj(self);
if (isRootPtr(self)) {
self.__destroy__();
} else {
self.as(IObject).?.destroy();
}
}
},
struct {
Expand Down Expand Up @@ -505,6 +515,10 @@ fn CoreApi(comptime I: type) type {
return self.vptr.__meta__(self.ptr);
}

pub fn __destroy__(self: I) void {
self.vptr.__destroy__(self.ptr);
}

pub fn as(self: I, comptime T: type) t: {
break :t if (isInterface(T)) ?T else ?*T;
} {
Expand Down

0 comments on commit 1371c2f

Please sign in to comment.