use std::ptr;
use godot_ffi::{ExtVariantType, GodotFfi, PtrcallType};
use crate::builtin::Variant;
use crate::meta::error::ConvertError;
use crate::meta::traits::GodotFfiVariant;
use crate::obj::{Gd, GodotClass};
use crate::{obj, sys};
#[derive(Debug, PartialEq)]
#[doc(hidden)]
pub struct ObjectArg<'gd> {
object_ptr: sys::GDExtensionObjectPtr,
_lifetime: std::marker::PhantomData<&'gd ()>,
}
impl<'gd> ObjectArg<'gd> {
pub fn from_gd<T: GodotClass>(obj: &'gd Gd<T>) -> Self {
Self {
object_ptr: obj.obj_sys(),
_lifetime: std::marker::PhantomData,
}
}
pub fn from_option_gd<T: GodotClass>(obj: Option<&'gd Gd<T>>) -> Self {
match obj {
Some(gd) => Self::from_gd(gd),
None => Self::null(),
}
}
pub fn null() -> Self {
Self {
object_ptr: ptr::null_mut(),
_lifetime: std::marker::PhantomData,
}
}
pub fn is_null(&self) -> bool {
self.object_ptr.is_null()
}
pub fn obj_sys(&self) -> sys::GDExtensionObjectPtr {
self.object_ptr
}
}
impl<'gd> Clone for ObjectArg<'gd> {
fn clone(&self) -> Self {
Self {
object_ptr: self.object_ptr,
_lifetime: std::marker::PhantomData,
}
}
}
unsafe impl<'gd> GodotFfi for ObjectArg<'gd> {
const VARIANT_TYPE: ExtVariantType = ExtVariantType::Concrete(sys::VariantType::OBJECT);
unsafe fn new_from_sys(_ptr: sys::GDExtensionConstTypePtr) -> Self {
unreachable!("ObjectArg should only be passed *to* Godot, not *from*.")
}
unsafe fn new_with_uninit(_init: impl FnOnce(sys::GDExtensionUninitializedTypePtr)) -> Self {
unreachable!("ObjectArg should only be passed *to* Godot, not *from*.")
}
unsafe fn new_with_init(_init: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
unreachable!("ObjectArg should only be passed *to* Godot, not *from*.")
}
fn sys(&self) -> sys::GDExtensionConstTypePtr {
self.object_ptr.cast()
}
fn sys_mut(&mut self) -> sys::GDExtensionTypePtr {
self.object_ptr.cast()
}
fn as_arg_ptr(&self) -> sys::GDExtensionConstTypePtr {
obj::object_as_arg_ptr(&self.object_ptr)
}
unsafe fn from_arg_ptr(_ptr: sys::GDExtensionTypePtr, _call_type: PtrcallType) -> Self {
unreachable!("ObjectArg should only be passed *to* Godot, not *from*.")
}
unsafe fn move_return_ptr(self, _ptr: sys::GDExtensionTypePtr, _call_type: PtrcallType) {
unreachable!("ObjectArg should only be passed *to* Godot, not *from*.")
}
}
impl<'gd> GodotFfiVariant for ObjectArg<'gd> {
fn ffi_to_variant(&self) -> Variant {
obj::object_ffi_to_variant(self)
}
fn ffi_from_variant(_variant: &Variant) -> Result<Self, ConvertError> {
unreachable!("ObjectArg should only be passed *to* Godot, not *from*.")
}
}