use crate::binds::MonoString;
use crate::domain::Domain;
use crate::gc::{gc_unsafe_enter, gc_unsafe_exit, GCHandle};
use crate::interop::InteropClass;
use crate::jit::esnure_thread_registered;
#[allow(unused_imports)]
use crate::object::Object;
use crate::Class;
use crate::ObjectTrait;
use std::ffi::CString;
#[warn(unused_imports)]
pub struct MString {
#[cfg(not(feature = "referneced_objects"))]
s_ptr: *mut MonoString,
#[cfg(feature = "referneced_objects")]
handle: GCHandle,
}
impl MString {
#[must_use]
pub fn new(domain: &Domain, string: &str) -> Self {
let cstr = CString::new(string).expect(crate::STR2CSTR_ERR);
esnure_thread_registered();
#[cfg(feature = "referneced_objects")]
let marker = gc_unsafe_enter();
let res = unsafe {
Self::from_ptr(crate::binds::mono_string_new(domain.get_ptr(), cstr.as_ptr()).cast())
}
.expect(crate::STR2CSTR_ERR);
#[cfg(feature = "referneced_objects")]
gc_unsafe_exit(marker);
drop(cstr);
res
}
#[must_use]
pub fn is_equal(&self, other: &Self) -> bool {
#[cfg(feature = "referneced_objects")]
let marker = gc_unsafe_enter();
let equ = unsafe {
crate::binds::mono_string_equal(self.get_ptr().cast(), other.get_ptr().cast()) != 0
};
#[cfg(feature = "referneced_objects")]
gc_unsafe_exit(marker);
equ
}
#[must_use]
pub fn hash(&self) -> u32 {
#[cfg(feature = "referneced_objects")]
let marker = gc_unsafe_enter();
let hsh = unsafe { crate::binds::mono_string_hash(self.get_ptr().cast()) };
#[cfg(feature = "referneced_objects")]
gc_unsafe_exit(marker);
hsh
}
}
impl InteropClass for MString {
fn get_mono_class() -> Class {
Class::get_string()
}
}
impl ToString for MString {
fn to_string(&self) -> String {
#[cfg(feature = "referneced_objects")]
let marker = gc_unsafe_enter();
let cstr = unsafe {
CString::from_raw(crate::binds::mono_string_to_utf8(
self.get_ptr().cast::<MonoString>(),
))
};
let res = cstr.to_str().expect("Colud not create String!").to_owned();
unsafe { crate::binds::mono_free(cstr.into_raw().cast::<std::os::raw::c_void>()) };
#[cfg(feature = "referneced_objects")]
gc_unsafe_exit(marker);
res
}
}
use crate::binds::MonoObject;
use crate::Exception;
impl ObjectTrait for MString {
#[must_use]
fn get_ptr(&self) -> *mut MonoObject {
#[cfg(not(feature = "referneced_objects"))]
{
self.s_ptr.cast()
}
#[cfg(feature = "referneced_objects")]
{
self.handle.get_target()
}
}
unsafe fn from_ptr_unchecked(ptr: *mut MonoObject) -> Self {
#[cfg(not(feature = "referneced_objects"))]
{
Self { s_ptr: ptr.cast() }
}
#[cfg(feature = "referneced_objects")]
{
Self {
handle: GCHandle::create_default(ptr.cast::<MonoObject>()),
}
}
}
fn to_mstring(&self) -> Result<Option<MString>, Exception> {
Ok(Some(self.clone()))
}
}
impl Clone for MString {
fn clone(&self) -> Self {
unsafe { Self::from_ptr(self.get_ptr()).unwrap() } }
}
impl<O: ObjectTrait> PartialEq<O> for MString {
fn eq(&self, other: &O) -> bool {
self.get_ptr().cast() == other.get_ptr()
}
}
#[test]
pub fn hash() {
let domain = crate::TEST_DOMAIN.lock().unwrap();
domain.attach_thread();
let hi = MString::new(&domain, "Hi?");
let hello = MString::new(&domain, "Hello?");
assert_eq!(hi.hash(), 72510);
assert_eq!(hello.hash(), 2157899213);
}
#[test]
pub fn to_string() {
let domain = crate::TEST_DOMAIN.lock().unwrap();
domain.attach_thread();
let hi = MString::new(&domain, "Hi?");
let hello = MString::new(&domain, "Hello?");
assert_eq!(hi.to_string(), "Hi?");
assert_eq!(hello.to_string(), "Hello?");
}
#[test]
pub fn to_mstring() {
let domain = crate::TEST_DOMAIN.lock().unwrap();
domain.attach_thread();
let hi = MString::new(&domain, "Hi?");
let hello = MString::new(&domain, "Hello?");
assert_eq!(hi.to_mstring().unwrap().unwrap().to_string(), "Hi?");
assert_eq!(hello.to_mstring().unwrap().unwrap().to_string(), "Hello?");
}
#[test]
pub fn equality() {
let domain = crate::TEST_DOMAIN.lock().unwrap();
domain.attach_thread();
let hi = MString::new(&domain, "Hi?");
let hi2 = MString::new(&domain, "Hi?");
let hello = MString::new(&domain, "Hello?");
assert!(hi.is_equal(&hi));
assert!(hi2.is_equal(&hi2));
assert!(hello.is_equal(&hello));
assert!(hi2.is_equal(&hi));
assert!(hi.is_equal(&hi2));
assert!(!hello.is_equal(&hi));
assert!(!hello.is_equal(&hi2));
assert!(!hi.is_equal(&hello));
assert!(!hi2.is_equal(&hello));
assert!(hi.eq(&hi));
assert!(hi2.eq(&hi2));
assert!(hello.eq(&hello));
assert!(!hi.eq(&hi2));
assert!(!hi2.eq(&hi));
assert!(!hi2.eq(&hello));
}