sead
Loading...
Searching...
No Matches
seadDelegateEventSlot.h
Go to the documentation of this file.
1#ifndef SEAD_DELEGATE_EVENT_SLOT_H_
2#define SEAD_DELEGATE_EVENT_SLOT_H_
3
4#include <container/seadTList.h>
5#include <heap/seadDisposer.h>
6#include <prim/seadDelegate.h>
7
8namespace sead {
9
10template <typename T>
12{
13public:
14 class Slot;
15
16private:
17 typedef TList<Slot*> SlotList;
19
20public:
21 class Slot : public IDisposer
22 {
23 protected:
26 struct { u8 data_[20]; } mDelegate;
28
29 friend class DelegateEvent;
30
31 public:
32 explicit Slot()
33 : IDisposer()
34 , mNode()
35 , mDelegatePtr(nullptr)
37 {
38 }
39
40 virtual ~Slot()
41 {
43 }
44
45 template <typename U>
46 explicit Slot(U* u, void (U::*fun)(T))
47 : IDisposer()
48 , mNode()
49 , mDelegatePtr(nullptr)
51 {
52 mDelegatePtr = new (&mDelegate) Delegate1<U, T>(u, fun);
53 }
54
55 template <typename U>
56 void bind(U* u, void (U::*fun)(T))
57 {
58 mDelegatePtr = new (&mDelegate) Delegate1<U, T>(u, fun);
59 }
60
61 void release()
62 {
64 {
65 mNode.erase();
67 }
68 }
69
71 {
73 }
74
75 protected:
76 void invoke_(T e)
77 {
78 if (mDelegatePtr)
79 mDelegatePtr->invoke(e);
80 }
81 };
82
83public:
84 explicit DelegateEvent()
85 : mList()
86 {
87 }
88
89 virtual ~DelegateEvent()
90 {
91 for (typename SlotList::iterator it = mList.begin(); it != mList.end(); )
92 {
93 Slot* s = *it;
94 ++it;
95 s->release();
96 }
97 }
98
99 void connect(Slot& slot)
100 {
101 if (slot.mConnectedToDelegateEvent)
102 slot.release();
103
104 mList.pushBack(&slot.mNode);
105 slot.mConnectedToDelegateEvent = true;
106 }
107
108 void disconnect(Slot& slot)
109 {
110 if (!slot.mConnectedToDelegateEvent)
111 return;
112
113 slot.release();
114 }
115
117 {
118 connect(slot);
119 return *this;
120 }
121
123 {
124 disconnect(slot);
125 return *this;
126 }
127
128 void fire(T e)
129 {
130 for (typename SlotList::iterator it = mList.begin(); it != mList.end(); ++it)
131 (*it)->invoke_(e);
132 }
133
135 {
136 return mList.size();
137 }
138
139protected:
141};
142#ifdef cafe
143static_assert(sizeof(DelegateEvent<float>) == 0x10, "sead::DelegateEvent<T> size mismatch");
144#endif // cafe
145
146} // namespace sead
147
148#endif // SEAD_DELEGATE_EVENT_SLOT_H_
Definition seadDelegate.h:278
Definition seadDelegateEventSlot.h:22
Slot()
Definition seadDelegateEventSlot.h:32
bool mConnectedToDelegateEvent
Definition seadDelegateEventSlot.h:27
SlotListNode mNode
Definition seadDelegateEventSlot.h:24
void invoke_(T e)
Definition seadDelegateEventSlot.h:76
bool isConnectedToDelegateEvent() const
Definition seadDelegateEventSlot.h:70
u8 data_[20]
Definition seadDelegateEventSlot.h:26
virtual ~Slot()
Definition seadDelegateEventSlot.h:40
IDelegate1< T > * mDelegatePtr
Definition seadDelegateEventSlot.h:25
void release()
Definition seadDelegateEventSlot.h:61
Slot(U *u, void(U::*fun)(T))
Definition seadDelegateEventSlot.h:46
void bind(U *u, void(U::*fun)(T))
Definition seadDelegateEventSlot.h:56
Definition seadDelegateEventSlot.h:12
DelegateEvent()
Definition seadDelegateEventSlot.h:84
virtual ~DelegateEvent()
Definition seadDelegateEventSlot.h:89
TListNode< Slot * > SlotListNode
Definition seadDelegateEventSlot.h:18
DelegateEvent< T > & operator-=(Slot &slot)
Definition seadDelegateEventSlot.h:122
TList< Slot * > SlotList
Definition seadDelegateEventSlot.h:17
s32 getSlotLength() const
Definition seadDelegateEventSlot.h:134
void fire(T e)
Definition seadDelegateEventSlot.h:128
DelegateEvent< T > & operator+=(Slot &slot)
Definition seadDelegateEventSlot.h:116
SlotList mList
Definition seadDelegateEventSlot.h:140
void disconnect(Slot &slot)
Definition seadDelegateEventSlot.h:108
void connect(Slot &slot)
Definition seadDelegateEventSlot.h:99
Definition seadDelegate.h:266
Definition seadDisposer.h:12
Definition seadTList.h:13
Definition seadTList.h:49
Definition seadAssert.h:44