Skip to content

Commit f902622

Browse files
committed
move parameter array initialization to zigplug.Plugin method
1 parent 60797fc commit f902622

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/clap/root.zig

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,35 +168,12 @@ fn ClapPlugin(comptime Plugin: type) type {
168168
fn init(clap_plugin: [*c]const c.clap_plugin) callconv(.c) bool {
169169
log.debug("init()", .{});
170170

171-
// TODO: move parameter initialization to a zigplug function
172171
if (@hasDecl(Plugin, "Parameters")) {
173-
const Parameters = Plugin.Parameters;
174172
const data = Data.fromClap(clap_plugin);
175-
const allocator = data.plugin_data.plugin.allocator;
176-
177-
const parameters = allocator.create(Parameters) catch {
178-
log.err("Failed to allocate parameters", .{});
179-
return false;
180-
};
181-
parameters.* = .{};
182-
const fields = @typeInfo(Parameters).@"struct".fields;
183-
184-
var parameters_array = allocator.alloc(*zigplug.Parameter, fields.len) catch {
185-
log.err("Failed to allocate parameters", .{});
173+
data.parameters = data.plugin_data.plugin.makeParametersSlice(Plugin) catch {
174+
log.err("failed to allocate parameters", .{});
186175
return false;
187176
};
188-
189-
inline for (fields, 0..) |field, i| {
190-
if (field.type != zigplug.Parameter)
191-
@compileError("Parameter '" ++ field.name ++ "' is not of type 'zigplug.Parameter'");
192-
if (field.default_value_ptr == null)
193-
@compileError("Parameter '" ++ field.name ++ "' has no default value");
194-
195-
parameters_array[i] = &@field(parameters, field.name);
196-
}
197-
198-
data.parameters = parameters_array;
199-
data.plugin_data.plugin.parameters = parameters;
200177
}
201178

202179
return true;

src/zigplug/zigplug.zig

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub const Plugin = struct {
8787
context: *anyopaque,
8888

8989
vtable: struct {
90-
// TODO: verify types
9190
deinit: *const fn (*anyopaque) void,
9291
process: *const fn (*anyopaque, ProcessBlock, ?*const anyopaque) anyerror!void,
9392
},
@@ -197,6 +196,30 @@ pub const Plugin = struct {
197196
pub inline fn process(self: *Plugin, block: ProcessBlock, params: ?*const anyopaque) !void {
198197
try self.vtable.process(self.context, block, params);
199198
}
199+
200+
pub fn makeParametersSlice(self: *Plugin, comptime P: type) ![]*Parameter {
201+
if (!@hasDecl(P, "Parameters"))
202+
@compileError("'Plugin.makeParametersSlice' was called but plugin has no parameters");
203+
const Parameters = P.Parameters;
204+
205+
const params = try self.allocator.create(Parameters);
206+
params.* = .{};
207+
const fields = @typeInfo(Parameters).@"struct".fields;
208+
209+
var parameters_array = try self.allocator.alloc(*Parameter, fields.len);
210+
211+
inline for (fields, 0..) |field, i| {
212+
if (field.type != Parameter)
213+
@compileError("Parameter '" ++ field.name ++ "' is not of type 'zigplug.Parameter'");
214+
if (field.default_value_ptr == null)
215+
@compileError("Parameter '" ++ field.name ++ "' has no default value");
216+
217+
parameters_array[i] = &@field(params, field.name);
218+
}
219+
220+
self.parameters = params;
221+
return parameters_array;
222+
}
200223
};
201224

202225
comptime {

0 commit comments

Comments
 (0)