Refactor Event class
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
@@ -25,6 +31,7 @@ namespace openVulkanoCpp
|
||||
private:
|
||||
enum class EventHandlerType { STATIC, INSTANCED, INSTANCED_SHARED_PTR, INSTANCED_WEAK_PTR, FUNCTIONAL };
|
||||
|
||||
//region Event Handlers
|
||||
class EventHandler : public IEventHandler
|
||||
{
|
||||
const EventHandlerType type;
|
||||
@@ -177,8 +184,9 @@ namespace openVulkanoCpp
|
||||
private:
|
||||
Function function;
|
||||
};
|
||||
//endregion
|
||||
|
||||
std::vector<EventHandler*> handlers;
|
||||
std::vector<std::unique_ptr<EventHandler>> handlers;
|
||||
mutable std::shared_mutex mutex;
|
||||
|
||||
void Remove(EventHandler* referenceHandler)
|
||||
@@ -186,15 +194,14 @@ namespace openVulkanoCpp
|
||||
std::unique_lock<std::shared_mutex> lock(mutex);
|
||||
for(size_t i = 0; i < handlers.size(); i++)
|
||||
{
|
||||
EventHandler* handler = handlers[i];
|
||||
EventHandler* handler = handlers[i].get();
|
||||
if (handler->ShouldBeDelete(referenceHandler))
|
||||
{
|
||||
size_t end = handlers.size() - 1;
|
||||
if (i < end)
|
||||
{
|
||||
handlers[i] = handlers[end];
|
||||
handlers[i] = std::move(handlers[end]);
|
||||
}
|
||||
delete handler;
|
||||
handlers.pop_back();
|
||||
}
|
||||
}
|
||||
@@ -203,7 +210,7 @@ namespace openVulkanoCpp
|
||||
IEventHandler* Add(EventHandler* handler)
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(mutex);
|
||||
handlers.push_back(handler);
|
||||
handlers.emplace_back(handler);
|
||||
return handler;
|
||||
}
|
||||
|
||||
@@ -216,7 +223,7 @@ namespace openVulkanoCpp
|
||||
void NotifyAll(Arguments... args) const
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex);
|
||||
for(EventHandler* handler : handlers)
|
||||
for(auto& handler : handlers)
|
||||
{
|
||||
if (!handler->IsInvalid())
|
||||
handler->Notify(args...);
|
||||
|
||||
Reference in New Issue
Block a user