1#![no_std]
2
3extern crate alloc;
4
5use alloc::sync::Arc;
6use dynamic_cast::{SupportsInterfaces, impl_supports_interfaces};
7use macro_magic::export_tokens_no_emit;
8
9#[doc(hidden)]
10pub use macro_magic;
11
12pub use basic_oop_macro::class_unsafe;
13
14#[doc(hidden)]
15pub use alloc::sync::Arc as alloc_sync_Arc;
16#[doc(hidden)]
17pub use core::mem::transmute as core_mem_transmute;
18#[doc(hidden)]
19pub use dynamic_cast::SupportsInterfaces as dynamic_cast_SupportsInterfaces;
20#[doc(hidden)]
21pub use dynamic_cast::impl_supports_interfaces as dynamic_cast_impl_supports_interfaces;
22#[doc(hidden)]
23pub use dynamic_cast::dyn_cast_arc as dynamic_cast_dyn_cast_arc;
24
25#[export_tokens_no_emit]
26struct inherited_from_Obj { __class__: ::basic_oop::Obj }
27
28pub type Vtable = *const *const ();
29
30pub struct Obj {
31 vtable: Vtable,
32}
33
34impl Obj {
35 pub fn new() -> Arc<dyn TObj> {
36 Arc::new(unsafe { Self::new_raw(OBJ_VTABLE.as_ptr()) })
37 }
38
39 pub unsafe fn new_raw(vtable: Vtable) -> Self {
40 Obj { vtable }
41 }
42
43 pub fn vtable(&self) -> Vtable { self.vtable }
44}
45
46pub trait TObj: SupportsInterfaces {
47 fn obj(&self) -> &Obj;
48}
49
50impl_supports_interfaces!(Obj: TObj);
51
52impl TObj for Obj {
53 fn obj(&self) -> &Obj { self }
54}
55
56#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
57#[repr(usize)]
58pub enum ObjVirtMethods {
59 VirtMethodsCount = 0usize,
60}
61
62pub struct ObjVtable(pub [*const (); ObjVirtMethods::VirtMethodsCount as usize]);
63
64impl ObjVtable {
65 pub const fn new() -> Self {
66 ObjVtable([])
67 }
68}
69
70const OBJ_VTABLE: [*const (); ObjVirtMethods::VirtMethodsCount as usize] = ObjVtable::new().0;
71
72#[repr(C)]
73pub struct VtableJoin<const A: usize, const B: usize> {
74 pub a: [*const (); A],
75 pub b: [*const (); B],
76}