use giterated_abi::{ callback::{ operation::IntoPluginOperationHandler, setting::{IntoPluginSettingGetter, IntoPluginSettingSetter}, value::IntoPluginValueGetter, Callback, CallbackPtr, }, result::FfiError, state::{State, StateUUID}, value_ex::FfiValueUntyped, vtable::{ operation::{IntoOperationVTable, OperationVTable}, plugin_initialization::InitializationVTable, IntoObjectVTable, IntoSettingVTable, IntoValueVTable, Object, ObjectVTable, SettingVTable, VTable, ValueVTable, }, FfiValueMut, FfiValueRef, }; use giterated_models::{ object::GiteratedObject, operation::GiteratedOperation, settings::Setting, value::GiteratedObjectValue, }; use tracing::trace_span; pub struct PluginStackBuilder { init_state: FfiValueUntyped, vtable: &'static VTable, } impl PluginStackBuilder { pub fn new(// state: FfiValueUntyped, // vtable: &'static VTable, ) -> PluginStackBuilder { todo!() // PluginStackBuilder { // init_state: state, // vtable // } } pub fn insert_state(&mut self, state: S) -> &mut Self { todo!() } pub fn object(&mut self) -> &mut Self { let _guard = trace_span!("register object").entered(); let func = self.vtable.register_object; let mut init_state = self.init_state.pin(); unsafe { func( unsafe { init_state.grant_mut() }, O::object_name(), O::VTABLE, ) }; self } pub fn register_operation(&mut self) -> &mut Self where D: IntoOperationVTable + GiteratedOperation, O: GiteratedObject, { let _guard = trace_span!("register operation").entered(); let mut init_state = self.init_state.pin(); unsafe { (self.vtable.register_operation)( unsafe { init_state.grant_mut() }, O::object_name(), D::operation_name(), >::VTABLE, ) } self } pub fn object_setting(&mut self, _get: HG, _set: HS) -> &mut Self where O: GiteratedObject, OS: IntoSettingVTable + Setting, HG: IntoPluginSettingGetter, HS: IntoPluginSettingSetter, { let _guard = trace_span!("register setting").entered(); let mut init_state = self.init_state.pin(); unsafe { (self.vtable.register_setting)( unsafe { init_state.grant_mut() }, O::object_name(), OS::name(), OS::VTABLE, ) } self } pub fn object_user_setting(&mut self) -> &mut Self where O: GiteratedObject, OS: IntoSettingVTable + Setting, { let _guard = trace_span!("register setting").entered(); let mut init_state = self.init_state.pin(); unsafe { (self.vtable.register_setting)( unsafe { init_state.grant_mut() }, O::object_name(), OS::name(), OS::VTABLE, ) } self } pub fn value(&mut self, handler: T) -> &mut Self where O: GiteratedObject, V: IntoValueVTable + GiteratedObjectValue, T: IntoPluginValueGetter, { let _guard = trace_span!("register value").entered(); let mut init_state = self.init_state.pin(); unsafe { (self.vtable.register_value)( unsafe { init_state.grant_mut() }, O::object_name(), V::value_name(), V::VTABLE, ) } // unsafe { // (self.vtable.value_getter)( // self.init_state, // O::object_name(), // V::value_name(), // ValueGetterCallback::new::(handler), // ) // } self } pub fn operation< A, O: GiteratedObject + IntoObjectVTable, D: IntoOperationVTable + GiteratedOperation, T: IntoPluginOperationHandler, >( &mut self, handler: T, ) -> &mut Self { let _guard = trace_span!("register operation handler").entered(); // unsafe { // (self.vtable.operation_handler)( // self.init_state, // O::object_name(), // D::operation_name(), // OperationHandlerCallback::new::(handler), // ) // } // TODO: Yikes? self.object::(); self.register_operation::(); self } // pub fn value_getter(&mut self, handler: T) -> &mut Self // where // O: GiteratedObject + IntoObjectVTable, // V: GiteratedObjectValue + IntoValueVTable, // T: IntoPluginValueGetter, // { // let _guard = trace_span!("register value_getter handler").entered(); // unsafe { // (self.vtable.value_getter)( // self.init_state, // O::object_name(), // V::value_name(), // ValueGetterCallback::new::(handler), // ) // } // // TODO: Yikes? // self.object::(); // self.value::(); // self // } pub fn setting_getter(&mut self, handler: T) -> &mut Self where O: GiteratedObject + IntoObjectVTable, OS: Setting + IntoSettingVTable, T: IntoPluginSettingGetter, { let _guard = trace_span!("register setting_getter handler").entered(); // unsafe { // (self.vtable.setting_getter)( // self.init_state, // O::object_name(), // OS::name(), // SettingGetterCallback::new::(handler), // ) // } self.object::(); let mut init_state = self.init_state.pin(); unsafe { (self.vtable.register_setting)( unsafe { init_state.grant_mut() }, O::object_name(), OS::name(), OS::VTABLE, ) }; self } } pub trait ValueSettingExt { fn value_setting(&mut self, get: HG, set: HS) -> &mut Self where O: GiteratedObject + IntoObjectVTable + 'static, VS: GiteratedObjectValue + IntoValueVTable + Setting + IntoSettingVTable, HG: IntoPluginSettingGetter, HS: IntoPluginSettingSetter; } impl ValueSettingExt for PluginStackBuilder { fn value_setting(&mut self, _get: HG, _set: HS) -> &mut Self where O: GiteratedObject + IntoObjectVTable + 'static, VS: GiteratedObjectValue + IntoValueVTable + Setting + IntoSettingVTable, HG: IntoPluginSettingGetter, HS: IntoPluginSettingSetter, { self } } #[derive(Clone, Copy)] pub struct ValueSettingGetterCallback; impl Callback for ValueSettingGetterCallback { type CallbackFunc = unsafe extern "C" fn( callback: CallbackPtr, state: FfiValueMut, object: FfiValueRef, ) -> Result; } pub trait ValueSettingGetter { unsafe extern "C" fn get_value( callback: CallbackPtr, state: FfiValueMut, object: FfiValueRef, ) -> Result; fn callback_ptr(&self) -> CallbackPtr; } impl ValueSettingGetter for HG where O: GiteratedObject, VS: GiteratedObjectValue, HG: IntoPluginSettingGetter, { unsafe extern "C" fn get_value( _callback: CallbackPtr, _state: FfiValueMut, _object: FfiValueRef, ) -> Result { // let result = HG::get_setting(callback, state, object)?; // let setting = *result.transmute_owned::(); todo!(); // Ok(NewAnyValue::new(setting)) } fn callback_ptr(&self) -> CallbackPtr { todo!() } }