Skip to content

Class

All Class automatically have the following API

Note

  • The zoop object separates data cleaning and memory release into two function calls. deinit() is used to clean up data, and destroy() is used to release memory. Because destroy() will automatically call deinit(), users do not need to call deinit() manually.
  • deinit() function can be omitted.

new

zig
pub fn new(ally: std.mem.Allocator) !*Self

Function: Create an object on the heap

parameter:

  • ally: memory allocator

Returns: If successful, return the created object, otherwise std.heap.Allocator.Error

make

zig
pub fn make() Self

Function: Create an object on the stack

Returns: the created object

Note

Objects created with make() must call initMixin() before they can be used normally

initMixin

zig
pub fn initMixin(self: *Self) void

Function: Initialize self.mixin

parameter:

  • self: target object

destroy

zig
pub fn destroy(self: *Self) void

Function: Destroy object

parameter:

  • self: target object

Note

Regardless of whether the object is allocated on the heap or on the stack, you need to call destroy() (unless it is allocated on the heap and it is confirmed that you do not need to call deinit()), because destroy() is responsible for calling all deinit() of the object and its parent class

as

zig
pub fn as(self: *const Self, comptime T: type) t: {
break :t if (isInterface(T)) ?T else ?*T;
}

Function: Dynamically convert object types

parameter:

  • self: the object to be converted
  • T: the type you want to convert to, which can be Class or Interface

Return: If the conversion is possible, return the converted object or interface, otherwise return null

Note

This conversion requires an array search operation, which is expensive.

cast

zig
pub fn cast(self: *const Self, comptime T: type) t: {
break :t if (isInterface(T)) T else *T;
}

Function: static conversion of object type

parameter:

  • self: the object to be converted
  • T: the type you want to convert to, which can be Class or Interface

Return: If the conversion is possible, the converted object or interface is returned, otherwise a compilation error occurs

Note

This conversion has almost no overhead, at most one pointer addition.

asptr

zig
pub fn asptr(self: *const Self) *anyopaque

Function: Get rootptr

parameter:

  • self: target object

Returns: rootptr of self

Released under the MIT License