Create initial reflection system

This commit is contained in:
Werner
2021-12-23 08:46:30 -03:00
parent aa549e37ef
commit 19eebb4085
10 changed files with 174 additions and 0 deletions

23
Source/Class.rs Normal file
View File

@ -0,0 +1,23 @@
use std::any::{type_name, TypeId};
pub struct Class {
id: TypeId,
name: &'static str,
}
impl Class {
pub fn New<T: 'static>() -> Self {
Self {
id: TypeId::of::<T>(),
name: type_name::<T>().into(),
}
}
pub fn GetId(&self) -> TypeId {
self.id
}
pub fn GetName(&self) -> &'static str {
self.name
}
}

32
Source/Examples/mod.rs Normal file
View File

@ -0,0 +1,32 @@
use crate::*;
#[derive(Reflect)]
pub struct Foo {
#[property(visible, editable, category = "Default")]
pub a: u32,
#[property(visible, editable, category = "Default")]
pub b: Bar,
#[property(visible, editable, category = "Default")]
pub c: Vec<u128>,
#[property(visible, editable, category = "Default")]
pub d: Vec<Bar>,
}
impl Foo {
#[function(callable, category = "Default")]
pub fn Func(&mut self) {}
}
#[derive(Reflect)]
pub struct Bar {
#[property(visible, editable, category = "Default")]
pub value: f32,
}
impl Bar {
#[function(callable, category = "Default")]
pub fn Func(&mut self) {}
}

27
Source/Object.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::{Class, Reflect};
use anyhow::Result;
use std::sync::Arc;
pub struct Object {
inner: Arc<dyn Reflect>,
}
impl Object {
fn New(obj: impl Reflect) -> Self {
Self {
inner: Arc::new(obj),
}
}
fn InstanceOf(&self, class: &Class) -> bool {
self.inner.as_ref().type_id() == class.GetId()
}
fn GetClass(&self) -> Result<()> {
Ok(())
}
pub fn GetProperty(&self) -> Result<()> {
Ok(())
}
}

3
Source/Reflect.rs Normal file
View File

@ -0,0 +1,3 @@
use std::any::Any;
pub trait Reflect: Any + Send + Sync + 'static {}

7
Source/TypeRegistry.rs Normal file
View File

@ -0,0 +1,7 @@
use std::any::TypeId;
use std::collections::HashMap;
#[derive(Default)]
pub struct TypeRegistry {
types: HashMap<TypeId, ()>,
}

18
Source/lib.rs Normal file
View File

@ -0,0 +1,18 @@
#![allow(dead_code)]
#![allow(non_snake_case)]
pub use Reflection_Macros::*;
pub mod Examples;
#[path = "Class.rs"]
mod _Class;
pub use self::_Class::*;
#[path = "Object.rs"]
mod _Object;
pub use self::_Object::*;
#[path = "Reflect.rs"]
mod _Reflect;
pub use self::_Reflect::*;