Sleeping Wombat GUI  0.100
swGUI
DelegateContainer.h
Go to the documentation of this file.
1 #pragma once
2 
10 #include "swCommonLib/External/FastDelegate/FastDelegate.h"
11 
12 #include "RegisteredEvent.h"
13 
14 #include <vector>
15 
16 
17 
18 
19 namespace sw {
20 namespace gui
21 {
22 
23 
24 
25 class DelegatesContainerBase;
26 struct IEventArgs;
27 class UIElement;
28 DEFINE_OPTR_TYPE( DelegatesContainerBase );
29 
30 template< typename EventArgType >
31 using EventDelegate = fastdelegate::FastDelegate2< UIElement*, EventArgType* >;
32 
33 
53 {
54 private:
55 
56  DelegatesContainerBaseOPtr m_next;
58 
59 protected:
60 public:
61  explicit DelegatesContainerBase ( EventType eventID );
62  virtual ~DelegatesContainerBase () = default;
63 
64 
66  inline EventType GetEventType () { return m_eventType; }
67 
68 
70  DelegatesContainerBase* GetNextContainer () { return m_next.get(); }
71 
73  void AddContainer ( DelegatesContainerBaseOPtr&& container );
74 
76  void RemoveNext ();
77 
79  DelegatesContainerBaseOPtr StealRestOfList ();
80 
82  virtual void CallDelegates ( UIElement* sender, IEventArgs* arguments ) = 0;
83 };
84 
85 
86 
90 template< typename EventArgType >
92 {
93 public:
94 
96 
97 private:
98 
99  std::vector< DelegateType > m_delegates;
100 
101 protected:
102 public:
103  explicit DelegatesContainer ( EventType type );
104  virtual ~DelegatesContainer () = default;
105 
106 
107  inline void operator+= ( DelegateType delegate );
108  void AddDelegate ( DelegateType delegate );
109  bool RemoveDelegate ( DelegateType delegate );
110 
111  bool Exists ( DelegateType delegate );
112  bool IsEmpty () const;
113 
114 
115  virtual void CallDelegates ( UIElement* sender, IEventArgs* arguments ) override;
116 };
117 
118 
119 //====================================================================================//
120 // Implementation
121 //====================================================================================//
122 
123 // ================================ //
124 //
126  : m_eventType( eventID )
127  , m_next( nullptr )
128 {}
129 
130 // ================================ //
131 //
132 inline void DelegatesContainerBase::AddContainer ( DelegatesContainerBaseOPtr&& container )
133 {
134  if( !m_next )
135  m_next = std::move( container );
136  else
137  m_next->AddContainer( std::move( container ) );
138 }
139 
140 // ================================ //
141 //
143 {
144  if( !m_next )
145  return;
146 
147  // This will release our m_next and steal followers m_next;
148  m_next = std::move( m_next->m_next );
149 }
150 
151 // ================================ //
152 //
153 inline DelegatesContainerBaseOPtr DelegatesContainerBase::StealRestOfList()
154 {
155  return std::move( m_next );
156 }
157 
158 
159 
160 //====================================================================================//
161 // DelegatesContainer
162 //====================================================================================//
163 
164 
165 // ================================ //
166 //
167 template< typename EventArgType >
169  : DelegatesContainerBase( type )
170 {}
171 
172 // ================================ //
173 //
174 template< typename EventArgType >
176 {
177  AddDelegate( delegate );
178 }
179 
180 // ================================ //
181 //
182 template< typename EventArgType >
184 {
185  if( !Exists( delegate ) )
186  m_delegates.push_back( delegate );
187 }
188 
189 // ================================ //
190 //
191 template< typename EventArgType >
193 {
194  bool removed = false;
195  for( auto iter = m_delegates.begin(); iter != m_delegates.end(); )
196  {
197  if( *iter == delegate )
198  {
199  iter = m_delegates.erase( iter );
200  removed = true;
201  }
202  else
203  iter++;
204  }
205 
206  return removed;
207 }
208 
209 // ================================ //
210 //
211 template< typename EventArgType >
213 {
214  for( auto& exisitingDelegate : m_delegates )
215  {
216  if( exisitingDelegate == delegate )
217  return true;
218  }
219  return false;
220 }
221 
222 // ================================ //
223 //
224 template< typename EventArgType >
226 {
227  return m_delegates.empty();
228 }
229 
230 // ================================ //
231 //
232 template< typename EventArgType >
234 {
235  // @todo What should be done if event will be marked as handled ??
236  EventArgType* typedArgs = static_cast< EventArgType* >( arguments );
237  for( auto& delegateFun : m_delegates )
238  delegateFun( sender, typedArgs );
239 }
240 
241 } // gui
242 } // sw
243 
244 
Derived template class for delegates.
Definition: DelegateContainer.h:91
Base class for delegates containers.
Definition: DelegateContainer.h:52
Base class for events.
Definition: IEventArgs.h:27
virtual void CallDelegates(UIElement *sender, IEventArgs *arguments)=0
Invokes delegates functions.
EventType GetEventType()
Call this function to check event type holded by derived class.
Definition: DelegateContainer.h:66
Definition: DirectInputModule.cpp:11
EventType m_eventType
Type of event used to distinguish between Delegates Containers.
Definition: DelegateContainer.h:57
DelegatesContainerBase * GetNextContainer()
Gets next element on linked list.
Definition: DelegateContainer.h:70
Interface for all controls in tree.
Definition: UIElement.h:57
uint32 EventType
Event type identifier.
Definition: RegisteredEvent.h:19
Plik zawiera definicje podstawowych typów zmiennych.
virtual ~DelegatesContainer()=default
bool RemoveDelegate(DelegateType delegate)
Definition: DelegateContainer.h:192
DelegatesContainerBaseOPtr m_next
Intrusive list.
Definition: DelegateContainer.h:56
void RemoveNext()
Removed element following this object.
Definition: DelegateContainer.h:142
virtual void CallDelegates(UIElement *sender, IEventArgs *arguments) override
Invokes delegates functions.
Definition: DelegateContainer.h:233
virtual ~DelegatesContainerBase()=default
void operator+=(DelegateType delegate)
Definition: DelegateContainer.h:175
void AddContainer(DelegatesContainerBaseOPtr &&container)
Adds container on the end of list.
Definition: DelegateContainer.h:132
fastdelegate::FastDelegate2< UIElement *, EventArgType * > EventDelegate
Definition: DelegateContainer.h:31
DelegatesContainerBaseOPtr StealRestOfList()
Steal list following this.
Definition: DelegateContainer.h:153
DEFINE_OPTR_TYPE(UIElement)
void AddDelegate(DelegateType delegate)
Definition: DelegateContainer.h:183
DelegatesContainerBase(EventType eventID)
Definition: DelegateContainer.h:125
std::vector< DelegateType > m_delegates
Delegates for EventType defined in DelegatesContainerBase.
Definition: DelegateContainer.h:99
EventDelegate< EventArgType > DelegateType
Definition: DelegateContainer.h:95
bool IsEmpty() const
Definition: DelegateContainer.h:225
DelegatesContainer(EventType type)
Definition: DelegateContainer.h:168
bool Exists(DelegateType delegate)
Definition: DelegateContainer.h:212