mirror of
https://github.com/guilhermewerner/reflection
synced 2025-06-15 21:24:19 +00:00
Create initial reflection system
This commit is contained in:
23
Source/Class.rs
Normal file
23
Source/Class.rs
Normal 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
32
Source/Examples/mod.rs
Normal 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
27
Source/Object.rs
Normal 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
3
Source/Reflect.rs
Normal file
@ -0,0 +1,3 @@
|
||||
use std::any::Any;
|
||||
|
||||
pub trait Reflect: Any + Send + Sync + 'static {}
|
7
Source/TypeRegistry.rs
Normal file
7
Source/TypeRegistry.rs
Normal 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
18
Source/lib.rs
Normal 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::*;
|
Reference in New Issue
Block a user