Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
/*
	Author : Tobias Stein
	Date   : 10th July, 2016
	File   : EventDispatcher.h

	Un/Registers subscribers for events and dispatches forwards incoming events.

	All Rights Reserved. (c) Copyright 2016.
*/

#ifndef __EVENT_DISPATCHER_H__
#define __EVENT_DISPATCHER_H__

#include "API.h"
#include "IEventDispatcher.h"

namespace ECS { namespace Event { namespace Internal {

	template<class T>
	class EventDispatcher : public IEventDispatcher
	{
		DECLARE_STATIC_LOGGER

		using EventDelegateList			= std::list<IEventDelegate*>;

		//using PendingAddDelegates		= std::list<IEventDelegate*>;
		using PendingRemoveDelegates	= std::list<IEventDelegate*>;

		//PendingAddDelegates		m_PendingAddDelegates;
		PendingRemoveDelegates	m_PendingRemoveDelegates;

		EventDelegateList		m_EventCallbacks;

		bool					m_Locked;

	public:
	
		// never use!
		EventDispatcher() :
			m_Locked(false)
		{}
	
		virtual ~EventDispatcher()
		{
			//this->m_PendingAddDelegates.clear();
			this->m_PendingRemoveDelegates.clear();
			this->m_EventCallbacks.clear();
		}
	
		// send event to all listener
		inline void Dispatch(IEvent* event) override
		{
			this->m_Locked = true;
			{
				LogInfo("Dispatch event %s", typeid(T).name());

				// remove pending delegates
				if (this->m_PendingRemoveDelegates.empty() == false)
				{
					for (auto EC : this->m_PendingRemoveDelegates)
					{
						auto result = std::find_if(this->m_EventCallbacks.begin(), this->m_EventCallbacks.end(),
							[&](const IEventDelegate* other)
							{
								return other->operator==(EC);
							});

						if (result != this->m_EventCallbacks.end())
						{
							IEventDelegate* ptrMem = (IEventDelegate*)(*result);

							this->m_EventCallbacks.erase(result);

							delete ptrMem;
							ptrMem = nullptr;
						}
					}
					this->m_PendingRemoveDelegates.clear();
				}

				for (auto EC : this->m_EventCallbacks)
				{
					assert(EC != nullptr && "Invalid event callback.");
					EC->invoke(event);
				}
			}
			this->m_Locked = false;
		}

		virtual void AddEventCallback(IEventDelegate* const eventDelegate) override
		{
			// if delegate wasn't deleted since last update, that is, delegate is still in pending list,
			// remove it from pending list
			auto result = std::find_if(this->m_PendingRemoveDelegates.begin(), this->m_PendingRemoveDelegates.end(),
				[&](const IEventDelegate* other)
				{
					return other->operator==(eventDelegate);
				});

			if (result != this->m_PendingRemoveDelegates.end())
			{
				this->m_PendingRemoveDelegates.erase(result);
				return;
			}

			this->m_EventCallbacks.push_back(eventDelegate);
		}

		virtual void RemoveEventCallback(IEventDelegate* eventDelegate) override
		{ 
			if (this->m_Locked == false)
			{
				auto result = std::find_if(this->m_EventCallbacks.begin(), this->m_EventCallbacks.end(),
					[&](const IEventDelegate* other)
					{
						return other->operator==(eventDelegate);
					});

				if (result != this->m_EventCallbacks.end())
				{
					IEventDelegate* ptrMem = (IEventDelegate*)(*result);

					this->m_EventCallbacks.erase(result);

					delete ptrMem;
					ptrMem = nullptr;
				}
			}
			else
			{
				auto result = std::find_if(this->m_EventCallbacks.begin(), this->m_EventCallbacks.end(),
					[&](const IEventDelegate* other)
					{
						return other->operator==(eventDelegate);
					});

				assert(result != this->m_EventCallbacks.end() && "");
				this->m_PendingRemoveDelegates.push_back((*result));
			}
		}

		virtual inline size_t GetEventCallbackCount() const override { return this->m_EventCallbacks.size(); }
	};

	DEFINE_STATIC_LOGGER_TEMPLATE(EventDispatcher, T, "EventDispatcher")

}}} // namespace ECS::Event::Internal

#endif // __EVENT_DISPATCHER_H__

Commits for Divide-Framework/trunk/Source Code/Libs/EntityComponentSystem/include/ECS/Event/EventDispatcher.h

Diff revisions: vs.
Revision Author Commited Message
1021 Diff Diff IonutCava picture IonutCava Sun 21 Jan, 2018 22:19:16 +0000

[Ionut]
- Performance optimizations

991 IonutCava picture IonutCava Tue 05 Dec, 2017 23:51:14 +0000