2023-12-30 23:55:05 +00:00
|
|
|
#![feature(trait_upcasting)]
|
|
|
|
|
2024-04-01 15:17:19 +00:00
|
|
|
use std::{any::TypeId, any::Any};
|
2023-12-30 23:55:05 +00:00
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
use lyra_ecs::{AtomicRef, AtomicRefMut, World};
|
2023-12-30 23:55:05 +00:00
|
|
|
|
|
|
|
extern crate self as lyra_reflect;
|
|
|
|
|
2024-01-12 19:08:46 +00:00
|
|
|
pub use lyra_reflect_derive::*;
|
|
|
|
|
2023-12-30 23:55:05 +00:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
pub(crate) mod lyra_engine {
|
|
|
|
pub(crate) mod reflect {
|
|
|
|
pub use super::super::*;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod reflected_struct;
|
|
|
|
pub use reflected_struct::*;
|
|
|
|
|
|
|
|
pub mod reflected_enum;
|
|
|
|
pub use reflected_enum::*;
|
|
|
|
|
|
|
|
pub mod reflected_tuple;
|
|
|
|
pub use reflected_tuple::*;
|
|
|
|
|
|
|
|
pub mod reflected_array;
|
|
|
|
pub use reflected_array::*;
|
|
|
|
|
|
|
|
pub mod reflected_list;
|
|
|
|
pub use reflected_list::*;
|
|
|
|
|
|
|
|
pub mod dynamic_tuple;
|
|
|
|
pub use dynamic_tuple::*;
|
|
|
|
|
2024-03-10 05:17:09 +00:00
|
|
|
pub mod reflected_hashmap;
|
|
|
|
pub use reflected_hashmap::*;
|
|
|
|
|
2024-01-20 05:54:36 +00:00
|
|
|
pub mod component;
|
|
|
|
pub use component::*;
|
|
|
|
|
|
|
|
pub mod resource;
|
|
|
|
pub use resource::*;
|
|
|
|
|
2023-12-30 23:55:05 +00:00
|
|
|
pub mod util;
|
|
|
|
pub mod field;
|
|
|
|
pub use field::*;
|
|
|
|
pub mod method;
|
|
|
|
pub use method::*;
|
|
|
|
pub mod registry;
|
|
|
|
pub use registry::*;
|
|
|
|
|
2024-01-12 19:08:46 +00:00
|
|
|
pub mod impls;
|
2023-12-30 23:55:05 +00:00
|
|
|
|
|
|
|
pub trait Reflect: Any {
|
|
|
|
fn name(&self) -> String;
|
|
|
|
fn type_id(&self) -> TypeId;
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
2024-03-10 05:17:09 +00:00
|
|
|
fn as_boxed_any(self: Box<Self>) -> Box<dyn Any>;
|
2023-12-30 23:55:05 +00:00
|
|
|
|
|
|
|
/// Apply a value to this object.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// The method panics if
|
|
|
|
/// * the type of `val` is not the same type as `self`
|
2024-03-10 05:17:09 +00:00
|
|
|
// TODO: instead of taking as reference, it would make more sense to take a Box<dyn Reflect>.
|
2023-12-30 23:55:05 +00:00
|
|
|
fn apply(&mut self, val: &dyn Reflect);
|
|
|
|
|
|
|
|
/// Clone self into a [`Box<dyn Reflect>`]
|
|
|
|
fn clone_inner(&self) -> Box<dyn Reflect>;
|
|
|
|
|
|
|
|
/// Returns a reflected reference of the value of `self`
|
|
|
|
fn reflect_ref(&self) -> ReflectRef;
|
|
|
|
|
|
|
|
/// Returns a reflected mutable reference of the value of `self`
|
|
|
|
fn reflect_mut(&mut self) -> ReflectMut;
|
|
|
|
|
|
|
|
fn reflect_val(&self) -> &dyn Reflect;
|
|
|
|
|
|
|
|
fn reflect_val_mut(&mut self) -> &mut dyn Reflect;
|
|
|
|
|
|
|
|
/// Returns a boolean indicating if this reflected object is `other_typeid`
|
|
|
|
///
|
|
|
|
/// I can't use a generic here because this trait needs to be object safe
|
|
|
|
fn is(&self, other_typeid: TypeId) -> bool {
|
|
|
|
TypeId::of::<Self>() == other_typeid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ReflectRef<'a> {
|
|
|
|
Struct(&'a dyn Struct),
|
|
|
|
Enum(&'a dyn Enum),
|
|
|
|
Tuple(&'a dyn Tuple),
|
|
|
|
Array(&'a dyn Array),
|
|
|
|
List(&'a dyn List),
|
|
|
|
Value(&'a dyn Reflect),
|
2024-03-10 05:17:09 +00:00
|
|
|
Map(&'a dyn ReflectedMap),
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ReflectRef<'a> {
|
|
|
|
/// Gets the reflected reference as a struct.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// * If `self` is not a `Struct` variant, this will panic
|
|
|
|
pub fn as_struct(&self) -> &dyn Struct {
|
|
|
|
match self {
|
|
|
|
ReflectRef::Struct(s) => *s,
|
|
|
|
_ => panic!("`self` is not a struct variant!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ReflectMut<'a> {
|
|
|
|
Struct(&'a mut dyn Struct),
|
|
|
|
Enum(&'a mut dyn Enum),
|
|
|
|
Tuple(&'a mut dyn Tuple),
|
|
|
|
Array(&'a mut dyn Array),
|
|
|
|
List(&'a mut dyn List),
|
|
|
|
Value(&'a mut dyn Reflect),
|
2024-03-10 05:17:09 +00:00
|
|
|
Map(&'a dyn ReflectedMap),
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ReflectMut<'a> {
|
|
|
|
/// Retrieves self as a [`ReflectRef`].
|
|
|
|
pub fn as_ref(&self) -> ReflectRef {
|
|
|
|
match self {
|
|
|
|
ReflectMut::Struct(v) => ReflectRef::Struct(*v),
|
|
|
|
ReflectMut::Enum(v) => ReflectRef::Enum(*v),
|
|
|
|
ReflectMut::Tuple(v) => ReflectRef::Tuple(*v),
|
|
|
|
ReflectMut::Array(v) => ReflectRef::Array(*v),
|
|
|
|
ReflectMut::List(v) => ReflectRef::List(*v),
|
|
|
|
ReflectMut::Value(v) => ReflectRef::Value(*v),
|
2024-03-10 05:17:09 +00:00
|
|
|
ReflectMut::Map(v) => ReflectRef::Map(*v),
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the reflected reference as a struct.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// * If `self` is not a `Struct` variant, this will panic
|
|
|
|
pub fn as_struct(&self) -> &dyn Struct {
|
|
|
|
match self {
|
|
|
|
ReflectMut::Struct(s) => *s,
|
|
|
|
_ => panic!("`self` is not a struct variant!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the reflected reference mutable as a struct.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// * If `self` is not a `Struct` variant, this will panic
|
|
|
|
pub fn as_struct_mut(&mut self) -> &mut dyn Struct {
|
|
|
|
match self {
|
|
|
|
ReflectMut::Struct(s) => *s,
|
|
|
|
_ => panic!("`self` is not a struct variant!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ReflectEither<'a> {
|
|
|
|
Ref(ReflectRef<'a>),
|
|
|
|
Mut(ReflectMut<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ReflectEither<'a> {
|
|
|
|
/// Converts `self` into a [`ReflectRef`]. Both variants can be retrieved as a reference.
|
|
|
|
/* pub fn into_ref(self) -> ReflectRef<'a> {
|
|
|
|
match self {
|
|
|
|
Self::Ref(v) => v,
|
|
|
|
Self::Mut(v) => v.as_ref()
|
|
|
|
}
|
|
|
|
} */
|
|
|
|
|
|
|
|
/// Attempts to get `self` as a [`ReflectMut`].
|
|
|
|
///
|
|
|
|
/// If `self` is a `Ref`, `None` will be returned
|
|
|
|
pub fn try_into_mut(self) -> Option<ReflectMut<'a>> {
|
|
|
|
match self {
|
|
|
|
Self::Mut(v) => Some(v),
|
|
|
|
Self::Ref(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pub fn as_ref(&self) -> &ReflectRef<'a> {
|
|
|
|
match self {
|
|
|
|
Self::Ref(v) => v,
|
|
|
|
Self::Mut(v) => &v.as_ref()
|
|
|
|
}
|
|
|
|
} */
|
|
|
|
|
|
|
|
pub fn is_mut(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
ReflectEither::Ref(_) => false,
|
|
|
|
ReflectEither::Mut(_) => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 20:52:12 +00:00
|
|
|
#[allow(dead_code)]
|
2023-12-30 23:55:05 +00:00
|
|
|
pub struct Value {
|
|
|
|
inner: Box<dyn Reflect>,
|
|
|
|
type_id: TypeId,
|
|
|
|
ref_type_id: TypeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn new<T: Reflect>(val: T) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Box::new(val),
|
|
|
|
type_id: TypeId::of::<T>(),
|
|
|
|
ref_type_id: TypeId::of::<&T>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the stored object from Value
|
|
|
|
pub fn take<T: Reflect>(self) -> Option<T> {
|
|
|
|
(self.inner as Box<dyn Any>).downcast::<T>().ok().map(|v| *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<T: Reflect>(&self) -> Option<&T> {
|
|
|
|
self.inner.as_any().downcast_ref::<T>()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut<T: Reflect>(&mut self) -> Option<&mut T> {
|
|
|
|
self.inner.as_any_mut().downcast_mut::<T>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoValue {
|
|
|
|
fn into_value(self) -> Value;
|
|
|
|
fn type_id() -> TypeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Reflect> IntoValue for T {
|
|
|
|
fn into_value(self) -> Value {
|
|
|
|
Value::new(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_id() -> TypeId {
|
|
|
|
TypeId::of::<T>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FromType<T> {
|
|
|
|
fn from_type() -> Self;
|
|
|
|
}
|
|
|
|
|
2024-01-20 05:54:36 +00:00
|
|
|
pub trait ReflectWorldExt {
|
|
|
|
/// Retrieves the type registry from the world.
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_registry(&self) -> AtomicRef<TypeRegistry>;
|
2024-01-20 05:54:36 +00:00
|
|
|
/// Retrieves the type registry mutably from the world.
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_registry_mut(&self) -> AtomicRefMut<TypeRegistry>;
|
2024-01-20 05:54:36 +00:00
|
|
|
/// Get a registered type from the type registry. Returns `None` if the type is not registered
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type<T>(&self, type_id: TypeId) -> Option<AtomicRef<RegisteredType>>;
|
2024-01-20 05:54:36 +00:00
|
|
|
/// Get a mutable registered type from the type registry in the world. returns `None` if the type is not registered.
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_mut<T>(&self, type_id: TypeId) -> Option<AtomicRefMut<RegisteredType>>;
|
2024-01-20 05:54:36 +00:00
|
|
|
/// Get a registered type, or register a new type and return it.
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_or_default<T>(&self, type_id: TypeId) -> AtomicRefMut<RegisteredType>;
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 05:54:36 +00:00
|
|
|
impl ReflectWorldExt for World {
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_registry(&self) -> AtomicRef<TypeRegistry> {
|
2024-01-20 05:54:36 +00:00
|
|
|
self.get_resource::<TypeRegistry>()
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
2024-01-20 05:54:36 +00:00
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_registry_mut(&self) -> AtomicRefMut<TypeRegistry> {
|
2024-01-20 05:54:36 +00:00
|
|
|
self.get_resource_mut::<TypeRegistry>()
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
2024-01-20 05:54:36 +00:00
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type<T>(&self, type_id: TypeId) -> Option<AtomicRef<RegisteredType>> {
|
2024-01-20 05:54:36 +00:00
|
|
|
let r = self.get_resource::<TypeRegistry>();
|
|
|
|
if r.has_type(type_id) {
|
2024-03-31 02:42:41 +00:00
|
|
|
Some(AtomicRef::map(r, |tr| tr.get_type(type_id).unwrap()))
|
2024-01-20 05:54:36 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_mut<T>(&self, type_id: TypeId) -> Option<AtomicRefMut<RegisteredType>> {
|
2024-01-20 05:54:36 +00:00
|
|
|
let r = self.get_resource_mut::<TypeRegistry>();
|
|
|
|
if r.has_type(type_id) {
|
2024-03-31 02:42:41 +00:00
|
|
|
Some(AtomicRefMut::map(r, |tr| tr.get_type_mut(type_id).unwrap()))
|
2024-01-20 05:54:36 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
fn get_type_or_default<T>(&self, type_id: TypeId) -> AtomicRefMut<RegisteredType> {
|
2024-01-20 05:54:36 +00:00
|
|
|
let r = self.get_resource_mut::<TypeRegistry>();
|
2024-03-31 02:42:41 +00:00
|
|
|
AtomicRefMut::map(r, |tr| tr.get_type_or_default(type_id))
|
2023-12-30 23:55:05 +00:00
|
|
|
}
|
|
|
|
}
|