Skip to content

Instantly share code, notes, and snippets.

@koljakube
Created August 17, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koljakube/f242b1c4c131c247cff0c8df2120f44b to your computer and use it in GitHub Desktop.
Save koljakube/f242b1c4c131c247cff0c8df2120f44b to your computer and use it in GitHub Desktop.
fn Caller(comptime T: anytype) type {
const ti = @typeInfo(T);
const m1 = @field(T, "method1");
const m2a = @field(T, "method2a");
const m2b = @field(T, "method2b");
const m1r = @field(T, "method1r");
const getters = struct {
fn getI32() i32 {
return 3;
}
fn getStr() []const u8 {
return "Zig";
}
};
return struct {
fn callProxy1(function: []const u8) void {
if (std.mem.eql(u8, "method1", function)) {
const params = .{getters.getI32()};
m1(params[0]);
}
if (std.mem.eql(u8, "method1r", function)) {
const params = .{getters.getI32()};
}
}
fn callProxy2(function: []const u8, comptime arg_types: anytype) void {
var args = .{};
inline for (arg_types) |AT| {
print("AT = {s}\n", .{@typeName(AT)});
args = args ++ switch (T) {
i32 => getters.getI32(),
[]const u8 => getters.getStr(),
else => unreachable,
};
}
if (std.mem.eql(u8, "method2a", function)) {
@call(.{}, m2a, args);
}
if (std.mem.eql(u8, "method2b", function)) {
@call(.{}, m2b, args);
}
}
pub fn call(function: []const u8) void {
if (std.mem.eql(u8, "method1", function)) {
callProxy1(function);
}
if (std.mem.eql(u8, "method2a", function)) {
callProxy2(function, .{ []const u8, i32 });
}
if (std.mem.eql(u8, "method2b", function)) {
callProxy2(function, .{ i32, []const u8 });
}
if (std.mem.eql(u8, "method1r", function)) {
callProxy1(function);
}
}
};
}
pub fn main() !void {
var fun = "method2a";
Caller(Test).call(fun);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment