1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::any::TypeId;
use super::{ Component, ComponentMeta };

/// A macro that helps setting up Component Registry
///
/// ```
/// # use sai::{ComponentMeta, ComponentRepository, Component, ComponentLifecycle, component_registry, Injected};
/// # use std::any::TypeId;
/// # struct A { }
/// # impl Component for A {
/// #     fn build(_: &ComponentRepository) -> A { A{} }
/// #     #[inline]
/// #     fn meta() -> ComponentMeta<Box<A>> {
/// #         ComponentMeta {
/// #             type_id: TypeId::of::<Injected<A>>(),
/// #             build: Box::new(|_| Box::new(A{})),
/// #             depends_on: vec![ ]
/// #         }
/// #     }
/// # }
/// # impl ComponentLifecycle for A {}
/// component_registry!(DummyRegistry, [A]);
/// ```
#[macro_export]
macro_rules! component_registry {
    ($name:ident, [$($x:ty),*]) => {

        pub struct $name {}

        impl $crate::ComponentRegistry for $name {
            fn get (tid: std::any::TypeId) -> Option<$crate::ComponentMeta<Box<dyn $crate::Component>>> {
                $(
                    let meta = <$x>::meta();
                    if tid == meta.type_id {
                        return Some(meta.into())
                    }
                )*

                None

            }

            fn all () -> Vec<std::any::TypeId> {
                vec![
                    $(
                        std::any::TypeId::of::<$crate::Injected<$x>>(),
                    )*
                ]
            }

            fn new () -> Self {
                $name{}
            }
        }

    }
}

/// A macro that combines any number of Component Registry
///
/// ```
/// # use sai::{ComponentMeta, ComponentRepository, Component, ComponentLifecycle, component_registry, Injected, combine_component_registry};
/// # use std::any::TypeId;
/// # struct A { }
/// # impl Component for A {
/// #     fn build(_: &ComponentRepository) -> A { A{} }
/// #     #[inline]
/// #     fn meta() -> ComponentMeta<Box<A>> {
/// #         ComponentMeta {
/// #             type_id: TypeId::of::<Injected<A>>(),
/// #             build: Box::new(|_| Box::new(A{})),
/// #             depends_on: vec![ ]
/// #         }
/// #     }
/// # }
/// # impl ComponentLifecycle for A {}
/// component_registry!(DummyRegistry, [A]);
/// component_registry!(DummyRegistry2, [A]);
///
/// // Combine DummyRegistry and DummyRegistry2 into SuperRegistry
/// combine_component_registry!(SuperRegistry, [ DummyRegistry, DummyRegistry2 ]);
/// ```
#[macro_export]
macro_rules! combine_component_registry {
    ($name:ident, [$($x:ty),*]) => {

        pub struct $name {}

        impl $crate::ComponentRegistry for $name {
            fn get (tid: std::any::TypeId) -> Option<$crate::ComponentMeta<Box<dyn $crate::Component>>> {
                $(
                    let meta = <$x>::get(tid);
                    if meta.is_some() {
                        return meta;
                    }
                )*

                None

            }

            fn all () -> Vec<std::any::TypeId> {
                let mut result = Vec::new();
                $(
                    let mut all = <$x>::all();
                    result.append(&mut all);
                )*
                return result;
            }

            fn new () -> Self {
                $name{}
            }
        }

    }
}


/// ComponentRegistry is a **data structure** for system to find a meta information for component.
/// It's required for a system to have a ComponentRegistry.
///
/// Normaly, you don't need to manually implement this trait.
///
/// To define a component registry, you just need to specify the name and a list of Component
/// identifiers.
/// ```
/// use sai::{Component};
/// # use sai::{component_registry};
///
/// #[derive(Component)]
/// struct A {};
/// #[derive(Component)]
/// struct B {};
///
/// component_registry!(ExampleRegistry, [
///     A, B
/// ]);
/// ```
/// Note that A, B above are not values, they are the identifiers.
///
/// In big project, uou can also composite multiple component registires into one.
/// Check out [here](macro.combine_component_registry.html).
pub trait ComponentRegistry {
    /// Getting a
    fn get (type_id: TypeId) -> Option<ComponentMeta<Box<dyn Component>>>;

    /// All the TypeIds that's in this registry
    fn all () -> Vec<TypeId>;

    fn new () -> Self;
}


#[cfg(test)]
mod tests {

    use super::*;
    use super::super::*;
    use std::any::TypeId;

    // A manually implemented component
    struct A { }
    impl Component for A {
        fn build(_: &ComponentRepository) -> A { A{} }
        #[inline]
        fn meta() -> ComponentMeta<Box<A>> {
            ComponentMeta {
                type_id: TypeId::of::<Injected<A>>(),
                build: Box::new(|_| Box::new(A{})),
                depends_on: vec![ ]
            }
        }
    }
    impl ComponentLifecycle for A {}

    component_registry!(DummyRegistry, [A]);
    component_registry!(DummyRegistry2, [A]);
    combine_component_registry!(CombinedRegistry, [DummyRegistry, DummyRegistry2]);

    #[test]
    fn component_registry_new_macro() {
        assert!(matches!(DummyRegistry::get(TypeId::of::<i32>()), None));
        assert!(matches!(DummyRegistry::get(TypeId::of::<Injected<A>>()), Some(_)));

        assert_eq!(DummyRegistry::all(), vec![TypeId::of::<Injected<A>>()]);
    }

    #[test]
    fn combine_registries_new_macro() {
        assert!(matches!(CombinedRegistry::get(TypeId::of::<i32>()), None));
        assert!(matches!(CombinedRegistry::get(TypeId::of::<Injected<A>>()), Some(_)));
    }
}