summaryrefslogtreecommitdiff
path: root/ecs/src/system/stateful.rs
blob: e74ef3188bcb34680f68da471c897c1533ae883b (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use std::mem::transmute;
use std::panic::{RefUnwindSafe, UnwindSafe};

use seq_macro::seq;

use crate::component::local::SystemWithLocalComponents;
use crate::component::Parts as ComponentParts;
use crate::event::Emitted as EmittedEvent;
use crate::system::initializable::{Initializable, MaybeInitializableParamTuple};
use crate::system::observer::{
    Observe,
    Observed,
    Observer,
    WrapperComponent as ObserverWrapperComponent,
};
use crate::system::{Into as IntoSystem, Metadata, Param, System, TypeErased};
use crate::World;

/// A stateful system.
pub struct Stateful<Func>
{
    func: Func,
    local_components: Vec<ComponentParts>,
}

macro_rules! impl_system {
    ($c: tt) => {
        seq!(I in 0..$c {
            impl<'world, Func, #(TParam~I,)*>
                System<'world, fn(&'world (), #(TParam~I,)*)> for Stateful<Func>
            where
                Func: Fn(#(TParam~I,)*) + Copy + RefUnwindSafe + UnwindSafe + 'static,
                #(TParam~I: Param<'world, Input: 'static>,)*
            {
                type Callbacks = Callbacks;

                fn finish(self) -> (TypeErased, Self::Callbacks)
                {
                    let Self { func, local_components } = self;

                    let callbacks = Callbacks { local_components };

                    let type_erased = TypeErased {
                        run: Box::new(move |world, metadata| {
                            // SAFETY: The caller of TypeErased::run ensures the lifetime
                            // is correct
                            let world = unsafe { &*std::ptr::from_ref(world) };

                            func(#({
                                TParam~I::new(&world, &metadata)
                            },)*);
                        }),
                    };


                    (type_erased, callbacks)
                }
            }

            impl<'world, Func, #(TParam~I,)*>
                Initializable<'world, fn(&'world (), #(TParam~I,)*)> for Stateful<Func>
            where
                Func: Fn(#(TParam~I,)*) + Copy + RefUnwindSafe + UnwindSafe + 'static,
                #(TParam~I: Param<'world, Input: 'static>,)*
                (#(TParam~I,)*): MaybeInitializableParamTuple<'world, Self>
            {
                type Inputs = <
                    (#(TParam~I,)*) as MaybeInitializableParamTuple<'world, Self>
                >::Inputs;

                fn initialize(mut self, inputs: Self::Inputs) -> Self
                {
                    init_initializable_params::<_, (#(TParam~I,)*)>(&mut self, inputs);

                    self
                }
            }

            impl<'world, Func, #(TParam~I,)*> IntoSystem<'world, fn(#(TParam~I,)*)>
                for Func
            where
                #(TParam~I: Param<'world>,)*
                Func: Fn(#(TParam~I,)*) + Copy + 'static,
            {
                type System = Stateful<Func>;

                fn into_system(self) -> Self::System
                {
                    Self::System {
                        func: self,
                        local_components: Vec::new(), // TODO: Use Vec::with_capacity
                    }
                }
            }
        });
    };
}

seq!(C in 1..16 {
    impl_system!(C);
});

impl<Func> SystemWithLocalComponents for Stateful<Func>
{
    fn add_local_component(&mut self, component_parts: ComponentParts)
    {
        self.local_components.push(component_parts);
    }
}

#[derive(Debug)]
pub struct Callbacks
{
    local_components: Vec<ComponentParts>,
}

impl crate::system::Callbacks for Callbacks
{
    fn on_created(&mut self, world: &mut World, metadata: Metadata)
    {
        for local_comp_parts in self.local_components.drain(..) {
            world.add_component(metadata.ent_id, local_comp_parts);
        }
    }
}

fn init_initializable_params<'world, SystemT, Params>(
    system: &mut SystemT,
    inputs: Params::Inputs,
) where
    Params: MaybeInitializableParamTuple<'world, SystemT>,
{
    Params::init_initializable(system, inputs);
}

macro_rules! impl_observer {
    ($c: tt) => {
        seq!(I in 0..$c {
            impl<'world, ObservedT, Func, #(TParam~I,)*> System<
                'world,
                fn(Observe<'world, ObservedT>, #(TParam~I,)*)
            > for Stateful<Func>
            where
                ObservedT: Observed,
                Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static,
                #(TParam~I: Param<'world>,)*
            {
                type Callbacks = Callbacks;

                fn finish(self) -> (TypeErased, Callbacks)
                {
                    unimplemented!();
                }
            }

            impl<'world, ObservedT, Func, #(TParam~I,)*> Initializable<
                'world,
                fn(Observe<'world, ObservedT>, #(TParam~I,)*)
            > for Stateful<Func>
            where
                ObservedT: Observed,
                Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static,
                #(TParam~I: Param<'world>,)*
                (#(TParam~I,)*): MaybeInitializableParamTuple<'world, Self>
            {
                type Inputs = <
                    (#(TParam~I,)*) as MaybeInitializableParamTuple<'world, Self>
                >::Inputs;

                fn initialize(mut self, inputs: Self::Inputs) -> Self
                {
                    init_initializable_params::<_, (#(TParam~I,)*)>(&mut self, inputs);

                    self
                }
            }

            impl<'world, ObservedT, Func, #(TParam~I,)*> Observer<
                'world,
                fn(Observe<'world, ObservedT>, #(TParam~I,)*)
            > for Stateful<Func>
            where
                ObservedT: Observed,
                Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static,
                #(TParam~I: Param<'world>,)*
            {
                type ObservedEvents = ObservedT::Events;

                fn observed_events() -> Self::ObservedEvents
                {
                    ObservedT::events()
                }

                fn finish_observer(self) -> (ObserverWrapperComponent, Callbacks)
                {
                    let Self { func, local_components } = self;

                    let callbacks = Callbacks { local_components };

                    let wrapper_comp = ObserverWrapperComponent::new(
                        move |world, metadata, emitted_event| {
                            // SAFETY: The caller of TypeErased::run ensures the lifetime
                            // is correct
                            let world = unsafe { &*std::ptr::from_ref(world) };

                            // SAFETY: The caller of TypeErased::run ensures the lifetime
                            // is correct
                            let emitted_event = unsafe {
                                transmute::<EmittedEvent<'_>, EmittedEvent<'_>>(
                                    emitted_event
                                )
                            };

                            func(Observe::new(world, emitted_event), #({
                                TParam~I::new(world, &metadata)
                            },)*);
                        },
                    );

                    (wrapper_comp, callbacks)
                }
            }

            impl<'world, Func, ObservedT, #(TParam~I,)*> IntoSystem<
                'world,
                fn(Observe<'world, ObservedT>,
                #(TParam~I,)*)
            > for Func
            where
                ObservedT: Observed,
                #(TParam~I: Param<'world>,)*
                Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static,
            {
                type System = Stateful<Func>;

                fn into_system(self) -> Stateful<Func>
                {
                    Stateful {
                        func: self,
                        local_components: Vec::new(), // TODO: Use Vec::with_capacity
                    }
                }
            }
        });
    };
}

seq!(C in 1..16 {
    impl_observer!(C);
});