Sleeping Wombat GUI  0.100
swGUI
EventProxy.h
Go to the documentation of this file.
1 #pragma once
2 
10 #include "DelegateContainer.h"
11 #include "EventHandlers.h"
12 #include "RegisteredEvent.h"
13 
14 
15 namespace sw {
16 namespace gui
17 {
18 
19 
23 template< typename EventArgType >
25 {
26 public:
28  typedef std::unique_ptr< EventArgType > ArgumentsOwnerPtr;
29 private:
30 
33 
34 public:
35  explicit EventProxy ( EventHandlers& delegatesContainer, const RegisteredEvent* eventInfo );
36 
37 
38  inline void operator+= ( DelegateType delegate );
39  inline bool operator-= ( DelegateType delegate );
40  inline void AddDelegate ( DelegateType delegate );
41  inline bool RemoveDelegate ( DelegateType delegate );
42 
43  bool RaiseEvent ( UIElement* sender, ArgumentsOwnerPtr&& arguments );
44 };
45 
46 
47 //====================================================================================//
48 // Implementation
49 //====================================================================================//
50 
51 // ================================ //
52 //
53 template< typename EventArgType >
54 inline EventProxy< EventArgType >::EventProxy ( EventHandlers& delegatesContainer, const RegisteredEvent* eventInfo )
55  : m_handlers( delegatesContainer )
56  , m_eventInfo( eventInfo )
57 {}
58 
59 // ================================ //
60 //
61 template< typename EventArgType >
63 {
64  AddDelegate( delegate );
65 }
66 
67 // ================================ //
68 //
69 template< typename EventArgType >
71 {
72  return RemoveDelegate( delegate );
73 }
74 
75 // ================================ //
76 //
77 template< typename EventArgType >
79 {
80  // Find delegates container.
81  DelegatesContainerBase* delegateContainer = m_handlers.FindContainer( m_eventInfo->ID );
82  DelegatesContainer< EventArgType >* typedDelegateContainer = nullptr;
83 
84  // Create delegates container if it doesn't exists.
85  if( !delegateContainer )
86  {
88  delegateContainer = m_handlers.AddContainer( std::move( DelegatesContainerBaseOPtr( container ) ) );
89  }
90 
91  typedDelegateContainer = static_cast< DelegatesContainer< EventArgType >* >( delegateContainer );
92  typedDelegateContainer->AddDelegate( delegate );
93 }
94 
95 // ================================ //
96 //
97 template< typename EventArgType >
99 {
100  DelegatesContainerBase* delegateContainer = m_handlers.FindContainer( m_eventInfo->ID );
101 
102  if( delegateContainer )
103  {
104  DelegatesContainer< EventArgType >* typedDelegateContainer = static_cast< DelegatesContainer< EventArgType >* >( delegateContainer );
105  bool result = typedDelegateContainer->RemoveDelegate( delegate );
106 
107  // Remove container if isn't usufull anymore.
108  if( typedDelegateContainer->IsEmpty() )
109  {
110  m_handlers.RemoveContainer( m_eventInfo->ID );
111  }
112 
113  return result;
114  }
115  return false;
116 }
117 
118 // ================================ //
119 //
120 template< typename EventArgType >
122 {
123  return m_handlers.RaiseEvent( m_eventInfo, sender, std::move( arguments ) );
124 }
125 
126 } // gui
127 } // sw
Derived template class for delegates.
Definition: DelegateContainer.h:91
Base class for delegates containers.
Definition: DelegateContainer.h:52
bool operator-=(DelegateType delegate)
Definition: EventProxy.h:70
EventProxy(EventHandlers &delegatesContainer, const RegisteredEvent *eventInfo)
Definition: EventProxy.h:54
Definition: DirectInputModule.cpp:11
const RegisteredEvent * m_eventInfo
Definition: EventProxy.h:32
void operator+=(DelegateType delegate)
Definition: EventProxy.h:62
Interface for all controls in tree.
Definition: UIElement.h:57
void AddDelegate(DelegateType delegate)
Definition: EventProxy.h:78
bool RemoveDelegate(DelegateType delegate)
Definition: DelegateContainer.h:192
bool RaiseEvent(UIElement *sender, ArgumentsOwnerPtr &&arguments)
Definition: EventProxy.h:121
EventDelegate< EventArgType > DelegateType
Definition: EventProxy.h:27
void AddContainer(DelegatesContainerBaseOPtr &&container)
Adds container on the end of list.
Definition: DelegateContainer.h:132
fastdelegate::FastDelegate2< UIElement *, EventArgType * > EventDelegate
Definition: DelegateContainer.h:31
void AddDelegate(DelegateType delegate)
Definition: DelegateContainer.h:183
Container class for delegates.
Definition: EventHandlers.h:24
EventHandlers & m_handlers
Definition: EventProxy.h:31
Structure describes event registered by controls.
Definition: RegisteredEvent.h:38
std::unique_ptr< EventArgType > ArgumentsOwnerPtr
Definition: EventProxy.h:28
Helper class for accessing event.
Definition: EventProxy.h:24
bool IsEmpty() const
Definition: DelegateContainer.h:225
bool RemoveDelegate(DelegateType delegate)
Definition: EventProxy.h:98