Skip to content

Commit

Permalink
add IObject.formatAny()
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuyadong committed Aug 14, 2024
1 parent 53272f0 commit 211ced8
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/zoop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub const IObject = struct {
pub const Vtable = struct {
__meta__: *const fn (*anyopaque) *MetaInfo,
destroy: *const fn (*anyopaque) void,
formatAny: *const fn (*anyopaque, writer: std.io.AnyWriter) anyerror!void,
};
pub usingnamespace CoreApi(@This());

Expand Down Expand Up @@ -213,9 +214,7 @@ pub fn Mixin(comptime T: type) type {
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
var buf: [512]u8 = undefined;
const msg = try std.fmt.bufPrint(&buf, "{s}@{x}{{{any}}} ", .{ @typeName(Self), @intFromPtr(self), self.meta });
try writer.writeAll(msg);
try writer.print("{s}@{x}{{{any}}} ", .{ @typeName(Self), @intFromPtr(self), self.meta });
}
};
}
Expand Down Expand Up @@ -477,16 +476,9 @@ fn CoreFn(comptime Class: type) type {
}
},
struct {
pub fn format(
self: *const Class,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
pub fn formatAny(self: *const Class, writer: std.io.AnyWriter) anyerror!void {
if (self.mixin.meta == null) @panic("obj create by make() must call obj.initMixin() before use it.");
var buf: [512]u8 = undefined;
const msg = try std.fmt.bufPrint(&buf, "{X}:{s}", .{ @intFromPtr(self), @typeName(Class) });
try writer.writeAll(msg);
try writer.print("{X}:{s}", .{ @intFromPtr(self), @typeName(Class) });
}
},
});
Expand Down Expand Up @@ -521,15 +513,23 @@ fn CoreApi(comptime I: type) type {
self.vptr.destroy(self.ptr);
}

pub fn formatAny(self: I, writer: std.io.AnyWriter) anyerror!void {
try self.vptr.formatAny(self.ptr, writer);
}

pub fn format(
self: I,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
var buf: [512]u8 = undefined;
const msg = try std.fmt.bufPrint(&buf, "{X}:{s}{{{s} vptr@{X}}}", .{ @intFromPtr(self.ptr), @typeName(I), metaInfo(self).typename(), @intFromPtr(self.vptr) });
try writer.writeAll(msg);
try writer.print("{X}:{s}{{vptr@{X} ", .{ @intFromPtr(self.ptr), @typeName(I), @intFromPtr(self.vptr) });
if (@TypeOf(writer) == std.io.AnyWriter) {
try self.formatAny(writer);
} else {
try self.formatAny(writer.any());
}
try writer.writeAll("}");
}
};
}
Expand Down Expand Up @@ -638,7 +638,7 @@ fn ApiInfoList(comptime len: usize) type {
writer: anytype,
) !void {
for (self.items[0..self.idx]) |item| {
try writer.writeAll(compfmt("> {s}.{s} : {}\n", .{ @typeName(item.iface), item.name, @TypeOf(@field(item.iface, item.name)) }));
try writer.print("> {s}.{s} : {}\n", .{ @typeName(item.iface), item.name, @TypeOf(@field(item.iface, item.name)) });
}
}
};
Expand Down

0 comments on commit 211ced8

Please sign in to comment.