sead
Loading...
Searching...
No Matches
seadDelegate.h
Go to the documentation of this file.
1#ifndef SEAD_DELEGATE_H_
2#define SEAD_DELEGATE_H_
3
4#include <basis/seadNew.h>
5
6namespace sead {
7
8template <typename T, typename METHODPTR, typename IDELEGATE>
9class DelegateBase : public IDELEGATE
10{
11private:
12 typedef METHODPTR MethodPtr;
13
14protected:
16 METHODPTR mMethod;
17
18public:
20 : mObject(nullptr)
21 , mMethod(nullptr)
22 {
23 }
24
25 DelegateBase(T* o, METHODPTR m)
26 : mObject(o)
27 , mMethod(m)
28 {
29 }
30
31 void bind(T* o, METHODPTR m)
32 {
33 mObject = o;
34 mMethod = m;
35 }
36
37 void bind(T* o)
38 {
39 mObject = o;
40 }
41
42 void bind(METHODPTR m)
43 {
44 mMethod = m;
45 }
46
47 void unbind()
48 {
49 mObject = nullptr;
50 mMethod = nullptr;
51 }
52
53 T* getObject() const
54 {
55 return mObject;
56 }
57};
58
59template <typename T, typename R>
61{
62public:
63 typedef R (T::*MethodPtr)();
64};
65
66template <typename T, typename R>
68{
69public:
70 typedef R (T::*MethodPtr)() const;
71};
72
73template <typename R>
74class DelegateTraits<void, R>
75{
76public:
77 typedef R (*MethodPtr)();
78};
79
81{
82public:
83 virtual void invoke() = 0;
84
85 void operator() ()
86 {
87 return invoke();
88 }
89
90 virtual IDelegate* clone(Heap*) const = 0;
91};
92
93template <typename T>
94class Delegate : public DelegateBase< T, typename DelegateTraits<T, void>::MethodPtr, IDelegate >
95{
96public:
97 typedef Delegate<T> self;
98 typedef typename DelegateTraits<T, void>::MethodPtr MethodPtr;
99
100public:
103 {
104 }
105
107 : DelegateBase< T, MethodPtr, IDelegate >(o, m)
108 {
109 }
110
111 virtual void invoke()
112 {
113 if (this->mObject && this->mMethod)
114 (this->mObject->*this->mMethod)();
115 }
116
117 void invoke() const
118 {
119 if (this->mObject && this->mMethod)
120 (this->mObject->*this->mMethod)();
121 }
122
123 void operator() () const
124 {
125 invoke();
126 }
127
128 virtual IDelegate* clone(Heap* heap) const
129 {
130 return new (heap) self(*this);
131 }
132};
133
134template <>
135class Delegate<void> : public DelegateBase< void, typename DelegateTraits<void, void>::MethodPtr, IDelegate >
136{
137public:
138 typedef Delegate<void> self;
139 typedef typename DelegateTraits<void, void>::MethodPtr MethodPtr;
140
141public:
142 Delegate()
143 : DelegateBase< void, MethodPtr, IDelegate >()
144 {
145 }
146
147 Delegate(MethodPtr m)
148 : DelegateBase< void, MethodPtr, IDelegate >(nullptr, m)
149 {
150 }
151
152 void invoke() override
153 {
154 if (mMethod)
155 (mMethod)();
156 }
157
158 void invoke() const
159 {
160 if (mMethod)
161 (mMethod)();
162 }
163
164 void operator() () const
165 {
166 invoke();
167 }
168
169 IDelegate* clone(Heap* heap) const override
170 {
171 return new (heap) self(*this);
172 }
173};
174
175template <typename T>
176class DelegateConst : public DelegateBase< const T, typename DelegateTraitsConst<T, void>::MethodPtr, IDelegate >
177{
178public:
179 typedef DelegateConst<T> self;
180 typedef typename DelegateTraitsConst<T, void>::MethodPtr MethodPtr;
181
182public:
184 : DelegateBase< const T, MethodPtr, IDelegate >()
185 {
186 }
187
189 : DelegateBase< const T, MethodPtr, IDelegate >(o, m)
190 {
191 }
192
193 virtual void invoke()
194 {
195 if (this->mObject && this->mMethod)
196 (this->mObject->*this->mMethod)();
197 }
198
199 void invoke() const
200 {
201 if (this->mObject && this->mMethod)
202 (this->mObject->*this->mMethod)();
203 }
204
205 void operator() () const
206 {
207 invoke();
208 }
209
210 virtual IDelegate* clone(Heap* heap) const
211 {
212 return new (heap) self(*this);
213 }
214};
215
216class StaticDelegate : public Delegate<void>
217{
218public:
220 : Delegate<void>()
221 {
222 }
223
224 StaticDelegate(typename Delegate<void>::MethodPtr m)
225 : Delegate<void>(m)
226 {
227 }
228};
229
230template <typename T>
231inline Delegate<T>
232DelegateCreator(T* obj, void (T::*m)())
233{
234 return Delegate<T>(obj, m);
235}
236
237inline Delegate<void>
239{
240 return Delegate<void>(m);
241}
242
243template <typename T, typename A, typename R>
245{
246public:
247 typedef R (T::*MethodPtr)(A);
248};
249
250template <typename T, typename A, typename R>
252{
253public:
254 typedef R (T::*MethodPtr)(A) const;
255};
256
257template <typename A, typename R>
258class DelegateTraits1<void, A, R>
259{
260public:
261 typedef R (*MethodPtr)(A);
262};
263
264template <typename A>
266{
267public:
268 virtual void invoke(A a) = 0;
269
270 void operator() (A a)
271 {
272 return invoke(a);
273 }
274};
275
276template <typename T, typename A>
277class Delegate1 : public DelegateBase< T, typename DelegateTraits1<T, A, void>::MethodPtr, IDelegate1<A> >
278{
279public:
280 typedef typename DelegateTraits1<T, A, void>::MethodPtr MethodPtr;
281
282public:
284 : DelegateBase< T, MethodPtr, IDelegate1<A> >()
285 {
286 }
287
289 : DelegateBase< T, MethodPtr, IDelegate1<A> >(o, m)
290 {
291 }
292
293 virtual void invoke(A a)
294 {
295 if (this->mObject && this->mMethod)
296 (this->mObject->*this->mMethod)(a);
297 }
298
299 void invoke(A a) const
300 {
301 if (this->mObject && this->mMethod)
302 (this->mObject->*this->mMethod)(a);
303 }
304
305 void operator() (A a) const
306 {
307 invoke(a);
308 }
309};
310
311template <typename A>
312class Delegate1<void, A> : public DelegateBase< void, typename DelegateTraits1<void, A, void>::MethodPtr, IDelegate1<A> >
313{
314public:
315 typedef typename DelegateTraits1<void, A, void>::MethodPtr MethodPtr;
316
317public:
319 : DelegateBase< void, MethodPtr, IDelegate1<A> >()
320 {
321 }
322
324 : DelegateBase< void, MethodPtr, IDelegate1<A> >(nullptr, m)
325 {
326 }
327
328 virtual void invoke(A a)
329 {
330 if (this->mMethod)
331 (this->mMethod)(a);
332 }
333
334 void invoke(A a) const
335 {
336 if (this->mMethod)
337 (this->mMethod)(a);
338 }
339
340 void operator() (A a) const
341 {
342 invoke(a);
343 }
344};
345
346template <typename T, typename A>
347class Delegate1Const : public DelegateBase< const T, typename DelegateTraits1Const<T, A, void>::MethodPtr, IDelegate1<A> >
348{
349public:
350 typedef typename DelegateTraits1Const<T, A, void>::MethodPtr MethodPtr;
351
352public:
354 : DelegateBase< const T, MethodPtr, IDelegate1<A> >()
355 {
356 }
357
359 : DelegateBase< const T, MethodPtr, IDelegate1<A> >(o, m)
360 {
361 }
362
363 virtual void invoke(A a)
364 {
365 if (this->mObject && this->mMethod)
366 (this->mObject->*this->mMethod)(a);
367 }
368
369 void invoke(A a) const
370 {
371 if (this->mObject && this->mMethod)
372 (this->mObject->*this->mMethod)(a);
373 }
374
375 void operator() (A a) const
376 {
377 invoke(a);
378 }
379};
380
381template<typename A>
382class StaticDelegate1 : public Delegate1<void, A>
383{
384public:
386 : Delegate1<void, A>()
387 {
388 }
389
390 StaticDelegate1(typename Delegate1<void, A>::MethodPtr method)
391 : Delegate1<void, A>(method)
392 {
393 }
394};
395
396template <typename T, typename A>
397inline Delegate1<T, A>
398DelegateCreator(T* obj, void (T::*m)(A))
399{
400 return Delegate1<T, A>(obj, m);
401}
402
403template <typename A>
404inline Delegate1<void, A>
406{
407 return Delegate1<void, A>(m);
408}
409
410template <typename T, typename A1, typename A2, typename R>
412{
413public:
414 typedef R (T::*MethodPtr)(A1, A2);
415};
416
417template <typename T, typename A1, typename A2, typename R>
419{
420public:
421 typedef R (T::*MethodPtr)(A1, A2) const;
422};
423
424template <typename A1, typename A2, typename R>
425class DelegateTraits2<void, A1, A2, R>
426{
427public:
428 typedef R (*MethodPtr)(A1, A2);
429};
430
431template <typename A1, typename A2>
433{
434public:
435 virtual void invoke(A1 a1, A2 a2) = 0;
436
437 void operator() (A1 a1, A2 a2)
438 {
439 return invoke(a1, a2);
440 }
441};
442
443template <typename T, typename A1, typename A2>
444class Delegate2 : public DelegateBase< T, typename DelegateTraits2<T, A1, A2, void>::MethodPtr, IDelegate2<A1, A2> >
445{
446public:
447 typedef typename DelegateTraits2<T, A1, A2, void>::MethodPtr MethodPtr;
448
449public:
451 : DelegateBase< T, MethodPtr, IDelegate2<A1, A2> >()
452 {
453 }
454
456 : DelegateBase< T, MethodPtr, IDelegate2<A1, A2> >(o, m)
457 {
458 }
459
460 virtual void invoke(A1 a1, A2 a2)
461 {
462 if (this->mObject && this->mMethod)
463 (this->mObject->*this->mMethod)(a1, a2);
464 }
465
466 void invoke(A1 a1, A2 a2) const
467 {
468 if (this->mObject && this->mMethod)
469 (this->mObject->*this->mMethod)(a1, a2);
470 }
471
472 void operator() (A1 a1, A2 a2) const
473 {
474 invoke(a1, a2);
475 }
476};
477
478template <typename A1, typename A2>
479class Delegate2<void, A1, A2> : public DelegateBase< void, typename DelegateTraits2<void, A1, A2, void>::MethodPtr, IDelegate2<A1, A2> >
480{
481public:
482 typedef typename DelegateTraits2<void, A1, A2, void>::MethodPtr MethodPtr;
483
484public:
486 : DelegateBase< void, MethodPtr, IDelegate2<A1, A2> >()
487 {
488 }
489
491 : DelegateBase< void, MethodPtr, IDelegate2<A1, A2> >(nullptr, m)
492 {
493 }
494
495 virtual void invoke(A1 a1, A2 a2)
496 {
497 if (this->mMethod)
498 (this->mMethod)(a1, a2);
499 }
500
501 void invoke(A1 a1, A2 a2) const
502 {
503 if (this->mMethod)
504 (this->mMethod)(a1, a2);
505 }
506
507 void operator() (A1 a1, A2 a2) const
508 {
509 invoke(a1, a2);
510 }
511};
512
513template <typename T, typename A1, typename A2>
514class Delegate2Const : public DelegateBase< const T, typename DelegateTraits2Const<T, A1, A2, void>::MethodPtr, IDelegate2<A1, A2> >
515{
516public:
517 typedef typename DelegateTraits2Const<T, A1, A2, void>::MethodPtr MethodPtr;
518
519public:
521 : DelegateBase< const T, MethodPtr, IDelegate2<A1, A2> >()
522 {
523 }
524
526 : DelegateBase< const T, MethodPtr, IDelegate2<A1, A2> >(o, m)
527 {
528 }
529
530 virtual void invoke(A1 a1, A2 a2)
531 {
532 if (this->mObject && this->mMethod)
533 (this->mObject->*this->mMethod)(a1, a2);
534 }
535
536 void invoke(A1 a1, A2 a2) const
537 {
538 if (this->mObject && this->mMethod)
539 (this->mObject->*this->mMethod)(a1, a2);
540 }
541
542 void operator() (A1 a1, A2 a2) const
543 {
544 invoke(a1, a2);
545 }
546};
547
548template<typename A1, typename A2>
549class StaticDelegate2 : public Delegate2<void, A1, A2>
550{
551public:
553 : Delegate2<void, A1, A2>()
554 {
555 }
556
557 StaticDelegate2(typename Delegate2<void, A1, A2>::MethodPtr method)
558 : Delegate2<void, A1, A2>(method)
559 {
560 }
561};
562
563template <typename T, typename A1, typename A2>
564inline Delegate2<T, A1, A2>
565DelegateCreator(T* obj, void (T::*m)(A1, A2))
566{
567 return Delegate2<T, A1, A2>(obj, m);
568}
569
570template <typename A1, typename A2>
571inline Delegate2<void, A1, A2>
572FunctionDelegateCreator(void (*m)(A1, A2))
573{
574 return Delegate2<void, A1, A2>(m);
575}
576
577} // namespace sead
578
579#endif // SEAD_DELEGATE_H_
Definition seadDelegate.h:348
Delegate1Const(const T *o, MethodPtr m)
Definition seadDelegate.h:358
Delegate1Const()
Definition seadDelegate.h:353
void operator()(A a) const
Definition seadDelegate.h:375
void invoke(A a) const
Definition seadDelegate.h:369
virtual void invoke(A a)
Definition seadDelegate.h:363
DelegateTraits1Const< T, A, void >::MethodPtr MethodPtr
Definition seadDelegate.h:350
Definition seadDelegate.h:313
Delegate1(MethodPtr m)
Definition seadDelegate.h:323
virtual void invoke(A a)
Definition seadDelegate.h:328
void operator()(A a) const
Definition seadDelegate.h:340
Delegate1()
Definition seadDelegate.h:318
void invoke(A a) const
Definition seadDelegate.h:334
DelegateTraits1< void, A, void >::MethodPtr MethodPtr
Definition seadDelegate.h:315
Definition seadDelegate.h:278
void operator()(A a) const
Definition seadDelegate.h:305
virtual void invoke(A a)
Definition seadDelegate.h:293
void invoke(A a) const
Definition seadDelegate.h:299
Delegate1()
Definition seadDelegate.h:283
Delegate1(T *o, MethodPtr m)
Definition seadDelegate.h:288
DelegateTraits1< T, A, void >::MethodPtr MethodPtr
Definition seadDelegate.h:280
Definition seadDelegate.h:515
DelegateTraits2Const< T, A1, A2, void >::MethodPtr MethodPtr
Definition seadDelegate.h:517
Delegate2Const(const T *o, MethodPtr m)
Definition seadDelegate.h:525
virtual void invoke(A1 a1, A2 a2)
Definition seadDelegate.h:530
Delegate2Const()
Definition seadDelegate.h:520
void invoke(A1 a1, A2 a2) const
Definition seadDelegate.h:536
void operator()(A1 a1, A2 a2) const
Definition seadDelegate.h:542
Definition seadDelegate.h:480
Delegate2()
Definition seadDelegate.h:485
Delegate2(MethodPtr m)
Definition seadDelegate.h:490
virtual void invoke(A1 a1, A2 a2)
Definition seadDelegate.h:495
void operator()(A1 a1, A2 a2) const
Definition seadDelegate.h:507
DelegateTraits2< void, A1, A2, void >::MethodPtr MethodPtr
Definition seadDelegate.h:482
void invoke(A1 a1, A2 a2) const
Definition seadDelegate.h:501
Definition seadDelegate.h:445
Delegate2()
Definition seadDelegate.h:450
Delegate2(T *o, MethodPtr m)
Definition seadDelegate.h:455
DelegateTraits2< T, A1, A2, void >::MethodPtr MethodPtr
Definition seadDelegate.h:447
virtual void invoke(A1 a1, A2 a2)
Definition seadDelegate.h:460
void operator()(A1 a1, A2 a2) const
Definition seadDelegate.h:472
void invoke(A1 a1, A2 a2) const
Definition seadDelegate.h:466
Definition seadDelegate.h:10
METHODPTR mMethod
Definition seadDelegate.h:16
void bind(T *o)
Definition seadDelegate.h:37
T * getObject() const
Definition seadDelegate.h:53
void unbind()
Definition seadDelegate.h:47
METHODPTR MethodPtr
Definition seadDelegate.h:12
T * mObject
Definition seadDelegate.h:15
DelegateBase(T *o, METHODPTR m)
Definition seadDelegate.h:25
void bind(METHODPTR m)
Definition seadDelegate.h:42
DelegateBase()
Definition seadDelegate.h:19
void bind(T *o, METHODPTR m)
Definition seadDelegate.h:31
Definition seadDelegate.h:177
void operator()() const
Definition seadDelegate.h:205
virtual IDelegate * clone(Heap *heap) const
Definition seadDelegate.h:210
DelegateConst(const T *o, MethodPtr m)
Definition seadDelegate.h:188
virtual void invoke()
Definition seadDelegate.h:193
DelegateTraitsConst< T, void >::MethodPtr MethodPtr
Definition seadDelegate.h:180
void invoke() const
Definition seadDelegate.h:199
DelegateConst()
Definition seadDelegate.h:183
DelegateConst< T > self
Definition seadDelegate.h:179
Definition seadDelegate.h:252
Definition seadDelegate.h:259
Definition seadDelegate.h:245
Definition seadDelegate.h:419
Definition seadDelegate.h:426
Definition seadDelegate.h:412
Definition seadDelegate.h:68
Definition seadDelegate.h:75
Definition seadDelegate.h:61
Definition seadDelegate.h:95
virtual void invoke()
Definition seadDelegate.h:111
DelegateTraits< T, void >::MethodPtr MethodPtr
Definition seadDelegate.h:98
Delegate()
Definition seadDelegate.h:101
void operator()() const
Definition seadDelegate.h:123
virtual IDelegate * clone(Heap *heap) const
Definition seadDelegate.h:128
Delegate(T *o, MethodPtr m)
Definition seadDelegate.h:106
void invoke() const
Definition seadDelegate.h:117
Delegate< T > self
Definition seadDelegate.h:97
Definition seadHeap.h:23
Definition seadDelegate.h:266
void operator()(A a)
Definition seadDelegate.h:270
virtual void invoke(A a)=0
Definition seadDelegate.h:433
virtual void invoke(A1 a1, A2 a2)=0
void operator()(A1 a1, A2 a2)
Definition seadDelegate.h:437
Definition seadDelegate.h:81
virtual void invoke()=0
virtual IDelegate * clone(Heap *) const =0
void operator()()
Definition seadDelegate.h:85
Definition seadDelegate.h:383
StaticDelegate1()
Definition seadDelegate.h:385
StaticDelegate1(typename Delegate1< void, A >::MethodPtr method)
Definition seadDelegate.h:390
Definition seadDelegate.h:550
StaticDelegate2(typename Delegate2< void, A1, A2 >::MethodPtr method)
Definition seadDelegate.h:557
StaticDelegate2()
Definition seadDelegate.h:552
Definition seadDelegate.h:217
StaticDelegate(typename Delegate< void >::MethodPtr m)
Definition seadDelegate.h:224
StaticDelegate()
Definition seadDelegate.h:219
Definition seadAssert.h:44
Delegate< void > FunctionDelegateCreator(void(*m)())
Definition seadDelegate.h:238
Delegate< T > DelegateCreator(T *obj, void(T::*m)())
Definition seadDelegate.h:232
Delegate1< void, A > FunctionDelegateCreator(void(*m)(A))
Definition seadDelegate.h:405
Delegate2< T, A1, A2 > DelegateCreator(T *obj, void(T::*m)(A1, A2))
Definition seadDelegate.h:565
Delegate2< void, A1, A2 > FunctionDelegateCreator(void(*m)(A1, A2))
Definition seadDelegate.h:572
Delegate1< T, A > DelegateCreator(T *obj, void(T::*m)(A))
Definition seadDelegate.h:398