1
0
Fork 0

Create a factory stub from the entry point macro

From this point on we should no longer need any of these ugly macros.
This commit is contained in:
Robbert van der Helm 2022-02-28 16:31:49 +01:00
parent 199deb887c
commit 0609f89df6
2 changed files with 56 additions and 8 deletions

View file

@ -1,8 +1,11 @@
use crate::ClapPlugin;
mod factory;
/// Re-export for the wrapper.
pub use ::clap_sys::entry::clap_plugin_entry;
pub use ::clap_sys::version::CLAP_VERSION;
pub use self::factory::Factory;
pub use clap_sys::entry::clap_plugin_entry;
pub use clap_sys::plugin_factory::CLAP_PLUGIN_FACTORY_ID;
pub use clap_sys::version::CLAP_VERSION;
pub use lazy_static::lazy_static;
/// Export a CLAP plugin from this library using the provided plugin type.
#[macro_export]
@ -12,6 +15,15 @@ macro_rules! nih_export_clap {
// `$plugin_ty`, so we need to generate the function inside of this macro
#[doc(hidden)]
mod clap {
// Because `$plugin_ty` is likely defined in the enclosing scope
use super::*;
// We don't use generics inside of statics, so this lazy_static is used as kind of an
// escape hatch
::nih_plug::wrapper::clap::lazy_static! {
static ref factory: ::nih_plug::wrapper::clap::Factory<$plugin_ty> = ::nih_plug::wrapper::clap::Factory::default();
}
// We don't need any special initialization or deinitialization handling
pub fn init(_plugin_path: *const ::std::os::raw::c_char) -> bool {
nih_log!("clap::init()");
@ -26,11 +38,16 @@ macro_rules! nih_export_clap {
pub fn get_factory(
factory_id: *const ::std::os::raw::c_char,
) -> *const ::std::ffi::c_void {
nih_log!("clap::get_factory({:?})", unsafe {
::std::ffi::CStr::from_ptr(factory_id)
});
std::ptr::null()
if !factory_id.is_null()
&& unsafe { ::std::ffi::CStr::from_ptr(factory_id) }
== unsafe { ::std::ffi::CStr::from_ptr(
::nih_plug::wrapper::clap::CLAP_PLUGIN_FACTORY_ID,
) }
{
&(*factory).clap_plugin_factory() as *const _ as *const ::std::ffi::c_void
} else {
std::ptr::null()
}
}
}

View file

@ -0,0 +1,31 @@
use clap_sys::plugin_factory::clap_plugin_factory;
use std::marker::PhantomData;
use std::mem;
use crate::ClapPlugin;
/// The plugin's factory. Initialized using a lazy_static from the entry poiunt's `get_factory()`
/// function. From this point onwards we don't need to generate code with macros anymore.
#[doc(hidden)]
pub struct Factory<P: ClapPlugin> {
/// The type will be used for constructing plugin instances later.
_phantom: PhantomData<P>,
}
impl<P: ClapPlugin> Default for Factory<P> {
fn default() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<P: ClapPlugin> Factory<P> {
pub fn clap_plugin_factory(&self) -> clap_plugin_factory {
clap_plugin_factory {
get_plugin_count: todo!(),
get_plugin_descriptor: todo!(),
create_plugin: todo!(),
}
}
}