pub struct FactoryWrapper<Raw, F> { /* private fields */ }Expand description
A wrapper around a clack factory of a given F type, as well as its Raw CLAP representation.
This wrapper allows to safely create a CLAP-compatible factory pointer, as well as soundly
casting that pointer back to this wrapper’s type, allowing to access the F type using the
handle function.
Implementations§
Source§impl<Raw, F> FactoryWrapper<Raw, F>
impl<Raw, F> FactoryWrapper<Raw, F>
Sourcepub const fn new(raw: Raw, inner: F) -> Self
pub const fn new(raw: Raw, inner: F) -> Self
Creates a new factory wrapper from a Raw CLAP type instance and an associated instance of F.
Sourcepub fn as_raw(&self) -> RawFactoryPointer<'_, Raw>
pub fn as_raw(&self) -> RawFactoryPointer<'_, Raw>
Returns a raw factory pointer compatible with this wrapper’s Raw CLAP representation
Sourcepub fn as_raw_ptr(&self) -> *const Raw
pub fn as_raw_ptr(&self) -> *const Raw
Returns a raw pointer to the Raw CLAP representation contained in this wrapper.
Sourcepub unsafe fn handle<T>(
raw: *const Raw,
handler: impl FnOnce(&F) -> Result<T, FactoryWrapperError>,
) -> Option<T>
pub unsafe fn handle<T>( raw: *const Raw, handler: impl FnOnce(&F) -> Result<T, FactoryWrapperError>, ) -> Option<T>
Provides a shared reference to the F factory instance to the given handler closure.
Besides providing a reference, this function does a few extra safety checks:
- The given
Rawfactory pointer is null-checked; - The handler is wrapped in
std::panic::catch_unwind; - Any
FactoryWrapperErrorreturned by the handler is caught.
If any of the above safety check fails, an error message is logged through stderr (as CLAP logging facilities are not available at the factory stage).
Note that some safety checks (e.g. the Raw pointer null-checks) may result in the
closure never being called, and an error being returned only. Users of this function must
not rely on the completion of this closure for safety, and must handle this function
returning None gracefully.
If all goes well, the return value of the handler closure is forwarded and returned by this function.
§Errors
If any safety check failed, or any error or panic occurred inside the handler closure, this
function returns None, and the error message is logged.
§Safety
The raw pointer must be created by the as_raw method, from an instance that
is still valid for the duration of the handler call.
While this function does a couple of simple safety checks, only a few common cases are actually covered (i.e. null checks), and those must not be relied upon: those checks only exist to help debugging faulty hosts.
§Example
This is the implementation of the plugin_count
callback’s C wrapper.
use clap_sys::factory::plugin_factory::clap_plugin_factory;
use clack_plugin::plugin::{Plugin, PluginMainThread};
use clack_plugin::factory::{FactoryWrapper, plugin::PluginFactoryImpl};
unsafe extern "C" fn get_plugin_count<F: PluginFactoryImpl>(factory: *const clap_plugin_factory) -> u32 {
FactoryWrapper::<clap_plugin_factory, F>::handle(factory, |factory| {
Ok(factory.plugin_count())
})
.unwrap_or(0)
}