//! This file has been automatically generated by `objc2`'s `header-translator`.
//! DO NOT EDIT
use core::cell::UnsafeCell;
use core::ffi::*;
use core::marker::{PhantomData, PhantomPinned};
use core::ptr::NonNull;
#[cfg(feature = "objc2")]
use objc2::__framework_prelude::*;

use crate::*;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cffiledescriptornativedescriptor?language=objc)
pub type CFFileDescriptorNativeDescriptor = c_int;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cffiledescriptor?language=objc)
#[repr(C)]
pub struct CFFileDescriptor {
    inner: [u8; 0],
    _p: UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>,
}

cf_type!(
    #[encoding_name = "__CFFileDescriptor"]
    unsafe impl CFFileDescriptor {}
);

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcffiledescriptorreadcallback?language=objc)
#[cfg(feature = "CFBase")]
pub const kCFFileDescriptorReadCallBack: CFOptionFlags = 1 << 0;
/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcffiledescriptorwritecallback?language=objc)
#[cfg(feature = "CFBase")]
pub const kCFFileDescriptorWriteCallBack: CFOptionFlags = 1 << 1;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cffiledescriptorcallback?language=objc)
#[cfg(feature = "CFBase")]
pub type CFFileDescriptorCallBack =
    Option<unsafe extern "C-unwind" fn(*mut CFFileDescriptor, CFOptionFlags, *mut c_void)>;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cffiledescriptorcontext?language=objc)
#[cfg(feature = "CFBase")]
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CFFileDescriptorContext {
    pub version: CFIndex,
    pub info: *mut c_void,
    pub retain: Option<unsafe extern "C-unwind" fn(*mut c_void) -> *mut c_void>,
    pub release: Option<unsafe extern "C-unwind" fn(*mut c_void)>,
    pub copyDescription: Option<unsafe extern "C-unwind" fn(*mut c_void) -> *const CFString>,
}

#[cfg(all(feature = "CFBase", feature = "objc2"))]
unsafe impl Encode for CFFileDescriptorContext {
    const ENCODING: Encoding = Encoding::Struct(
        "?",
        &[
            <CFIndex>::ENCODING,
            <*mut c_void>::ENCODING,
            <Option<unsafe extern "C-unwind" fn(*mut c_void) -> *mut c_void>>::ENCODING,
            <Option<unsafe extern "C-unwind" fn(*mut c_void)>>::ENCODING,
            <Option<unsafe extern "C-unwind" fn(*mut c_void) -> *const CFString>>::ENCODING,
        ],
    );
}

#[cfg(all(feature = "CFBase", feature = "objc2"))]
unsafe impl RefEncode for CFFileDescriptorContext {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

#[cfg(feature = "CFBase")]
unsafe impl ConcreteType for CFFileDescriptor {
    #[doc(alias = "CFFileDescriptorGetTypeID")]
    #[inline]
    fn type_id() -> CFTypeID {
        extern "C-unwind" {
            fn CFFileDescriptorGetTypeID() -> CFTypeID;
        }
        unsafe { CFFileDescriptorGetTypeID() }
    }
}

#[cfg(feature = "CFBase")]
#[inline]
pub unsafe extern "C-unwind" fn CFFileDescriptorCreate(
    allocator: Option<&CFAllocator>,
    fd: CFFileDescriptorNativeDescriptor,
    close_on_invalidate: bool,
    callout: CFFileDescriptorCallBack,
    context: *const CFFileDescriptorContext,
) -> Option<CFRetained<CFFileDescriptor>> {
    extern "C-unwind" {
        fn CFFileDescriptorCreate(
            allocator: Option<&CFAllocator>,
            fd: CFFileDescriptorNativeDescriptor,
            close_on_invalidate: Boolean,
            callout: CFFileDescriptorCallBack,
            context: *const CFFileDescriptorContext,
        ) -> Option<NonNull<CFFileDescriptor>>;
    }
    let ret = unsafe {
        CFFileDescriptorCreate(allocator, fd, close_on_invalidate as _, callout, context)
    };
    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
}

extern "C-unwind" {
    pub fn CFFileDescriptorGetNativeDescriptor(
        f: &CFFileDescriptor,
    ) -> CFFileDescriptorNativeDescriptor;
}

extern "C-unwind" {
    #[cfg(feature = "CFBase")]
    pub fn CFFileDescriptorGetContext(f: &CFFileDescriptor, context: *mut CFFileDescriptorContext);
}

extern "C-unwind" {
    #[cfg(feature = "CFBase")]
    pub fn CFFileDescriptorEnableCallBacks(f: &CFFileDescriptor, call_back_types: CFOptionFlags);
}

extern "C-unwind" {
    #[cfg(feature = "CFBase")]
    pub fn CFFileDescriptorDisableCallBacks(f: &CFFileDescriptor, call_back_types: CFOptionFlags);
}

extern "C-unwind" {
    pub fn CFFileDescriptorInvalidate(f: &CFFileDescriptor);
}

#[inline]
pub unsafe extern "C-unwind" fn CFFileDescriptorIsValid(f: &CFFileDescriptor) -> bool {
    extern "C-unwind" {
        fn CFFileDescriptorIsValid(f: &CFFileDescriptor) -> Boolean;
    }
    let ret = unsafe { CFFileDescriptorIsValid(f) };
    ret != 0
}

#[cfg(all(feature = "CFBase", feature = "CFRunLoop"))]
#[inline]
pub unsafe extern "C-unwind" fn CFFileDescriptorCreateRunLoopSource(
    allocator: Option<&CFAllocator>,
    f: Option<&CFFileDescriptor>,
    order: CFIndex,
) -> Option<CFRetained<CFRunLoopSource>> {
    extern "C-unwind" {
        fn CFFileDescriptorCreateRunLoopSource(
            allocator: Option<&CFAllocator>,
            f: Option<&CFFileDescriptor>,
            order: CFIndex,
        ) -> Option<NonNull<CFRunLoopSource>>;
    }
    let ret = unsafe { CFFileDescriptorCreateRunLoopSource(allocator, f, order) };
    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
}
