RustAudio / vst3-sys

Raw Bindings to the VST3 API
Other
284 stars 18 forks source link

Add a thin/transparent wrapper around raw vtable pointers in instances #28

Closed m-hilgendorf closed 4 years ago

m-hilgendorf commented 4 years ago

This creates a VstPtr type which wraps a raw vtable pointer, and replaces the method calls and types that return pointers to interfaces with it. Consumers are required to do a nullptr check by upgrading to ComRc using VstPtr::upgrade(&self), which returns Option<ComRc>. For example, in the again example:

        let param_changes: &VstPtr<IParameterChanges> = &(*data).input_param_changes;
        if let Some(param_changes) = param_changes.upgrade() {
            let num_params_changed: VstPtr<IParamValueQueue> = param_changes.get_parameter_count();
            for i in 0..num_params_changed {
                let param_queue = param_changes.get_parameter_data(i);
                if let Some(param_queue) = param_queue.upgrade() {
                   // ... 

The implementation is a transparent wrapper around a pointer to a vtable, which maintains the ABI.

#[repr(transparent)]
pub struct VstPtr<I:ComInterface + ?Sized> {
   vtable: *mut I::VTable
}

The only difference between this and ComPtr is that we allow VstPtrs to be null, and check when upgrading.