SP3-108 Removed old event register method

added new events define
This commit is contained in:
Xiao Qi 2022-09-15 01:46:11 +08:00
parent 23a599abe8
commit a3b1660013
8 changed files with 61 additions and 131 deletions

View File

@ -118,6 +118,7 @@
<ClInclude Include="src\ECS_Base\System\SHSystemManager.h" />
<ClInclude Include="src\Engine\SHEngine.h" />
<ClInclude Include="src\Events\SHEvent.h" />
<ClInclude Include="src\Events\SHEventDefines.h" />
<ClInclude Include="src\Events\SHEventManager.h" />
<ClInclude Include="src\Events\SHEventReceiver.h" />
<ClInclude Include="src\Filesystem\SHFileSystem.h" />
@ -204,7 +205,6 @@
<ClCompile Include="src\ECS_Base\System\SHEntityManager.cpp" />
<ClCompile Include="src\ECS_Base\System\SHSystemManager.cpp" />
<ClCompile Include="src\Engine\SHEngine.cpp" />
<ClCompile Include="src\Events\SHEvent.cpp" />
<ClCompile Include="src\Events\SHEventManager.cpp" />
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
<ClCompile Include="src\Graphics\Buffers\SHVkBuffer.cpp" />
@ -292,4 +292,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -400,6 +400,7 @@
<ClInclude Include="src\Tools\SHUtilities.h">
<Filter>Tools</Filter>
</ClInclude>
<ClInclude Include="src\Events\SHEventDefines.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
@ -423,9 +424,6 @@
<ClCompile Include="src\Engine\SHEngine.cpp">
<Filter>Engine</Filter>
</ClCompile>
<ClCompile Include="src\Events\SHEvent.cpp">
<Filter>Events</Filter>
</ClCompile>
<ClCompile Include="src\Events\SHEventManager.cpp">
<Filter>Events</Filter>
</ClCompile>

View File

@ -1,49 +0,0 @@
/******************************************************************************
* \file SHEvent.cpp
* \author Loh Xiao Qi
* \brief Implementation of SHEvent.h Initialise data member of smart pointer
* to package data. Return the package pointer and package type
* accordingly.
*
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
* or disclosure of this file or its contents without the prior
* written consent of Digipen Institute of Technology is prohibited
******************************************************************************/
#include "SHpch.h"
#include "SHEvent.h"
#include <utility>
namespace SHADE
{
/****************************************************************************
* \param PackagePtr - smart pointer to package data.
* \brief Constructor for Event. User needs to dynamically allocate memory
* for package data and pass in the smart pointer so the data member
* can be initialised properly.
****************************************************************************/
SHEvent::SHEvent(PackagePtr pkg)
: package{ std::move(pkg) }
{
}
/****************************************************************************
* \brief Get pointer to data package.
*
* \return Smart pointer to Package Base, needs to be cast to correct
* derived class.
****************************************************************************/
ConstPackagePtr& SHEvent::GetData() const
{
return package;
}
/****************************************************************************
* \brief Gets the type of package.
****************************************************************************/
SHPackageType SHEvent::GetType() const
{
return package->GetPackageType();
}
}

View File

@ -1,45 +1,22 @@
/******************************************************************************
* \file SHEventBase.h
* \file SHEvent.h
* \author Loh Xiao Qi
* \brief Event base abstract class declaration
* \brief Event class declaration
*
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
* \copyright Copyright (c) 2022 Digipen Institute of Technology. Reproduction
* or disclosure of this file or its contents without the prior written consent
* of Digipen Institute of Technology is prohibited.
******************************************************************************/
#ifndef SH_EVENT_BASE_H
#define SH_EVENT_BASE_H
#pragma once
#include "SHEventDefines.h"
namespace SHADE
{
class SHEvent
struct SHEvent
{
public:
/****************************************************************************
* \param PackagePtr - smart pointer to package data.
* \brief Constructor for Event. User needs to dynamically allocate memory
* for package data and pass in the smart pointer so the data member
* can be initialised properly.
****************************************************************************/
SHEvent(PackagePtr);
/****************************************************************************
* \brief Get pointer to data package.
*
* \return Smart pointer to Package Base, needs to be cast to correct
* derived class.
****************************************************************************/
PackagePtr const& GetData() const;
/****************************************************************************
* \brief Gets the type of package.
****************************************************************************/
SHPackageType GetType() const;
private:
PackagePtr package;
SHEventIdentifier type;
SHEventDataPtr data;
SHEventHandle handle;
};
}
#endif // !SH_EVENT_BASE_H

View File

@ -0,0 +1,10 @@
#pragma once
#include "SHpch.h"
typedef uint32_t SHEventIdentifier;
typedef uint32_t SHEventHandle;
typedef void* SHEventDataPtr;
//Add your event identifiers here:
constexpr SHEventIdentifier SH_EXAMPLE_EVENT{0};

View File

@ -72,20 +72,33 @@ namespace SHADE
/****************************************************************************
* \param ResponseFunction - function pointer from receiver to be passed
* into event manager to be called when events are broadcasted.
* \param SHPackageType - package type that corresponding subscriber is
* \param SHEventIdentifier - package type that corresponding subscriber is
* subscribing to.
* \brief Links a function pointer from a subscriber to a particular
* package type
****************************************************************************/
void SHEventManager::SubscribeTo(SHPackageType pkgType, ReceiverPtr receiver)
void SHEventManager::SubscribeTo(SHEventIdentifier pkgType, ReceiverPtr receiver)
{
SHEventManager::GetEventManagerInstance().RegisterReceiverToType(pkgType, receiver);
}
void SHEventManager::StaticSubscribeTo(SHPackageType pkgType, ResponseFunction func)
template<typename T>
T* SHEventManager::BroadcastData(T data, SHEventIdentifier eventType)
{
SHEventManager::GetEventManagerInstance().RegisterStaticReceiverToType(pkgType, func);
SHEventDataPtr ptr = new std::byte[sizeof(data)];
std::memcpy(ptr, &data, sizeof(data));
CatchEvent(
{
eventType,
ptr,
handleCounter++
}
);
return reinterpret_cast<T*>(ptr);
}
/****************************************************************************
@ -96,7 +109,7 @@ namespace SHADE
* \brief Registers receiver as a subscriber to listener in the registry.
****************************************************************************/
void SHEventManager::RegisterReceiverToType(
SHPackageType pkgType, ReceiverPtr receiver)
SHEventIdentifier pkgType, ReceiverPtr receiver)
{
if (packageReceiverRegistry.contains(pkgType))
{
@ -108,18 +121,6 @@ namespace SHADE
}
}
void SHEventManager::RegisterStaticReceiverToType(SHPackageType pkgType, ResponseFunction func)
{
if (staticPackageReceiverRegistry.contains(pkgType))
{
staticPackageReceiverRegistry[pkgType].emplace_back(func);
}
else
{
staticPackageReceiverRegistry.emplace(pkgType, std::vector{ func });
}
}
/****************************************************************************
* \param ListenerConstPtr - Const pointer to listener that sent event.
* \param EventType - Event data
@ -129,16 +130,16 @@ namespace SHADE
****************************************************************************/
void SHEventManager::Broadcast(SHEvent const& event)
{
ResponseVec& receivers{ packageReceiverRegistry[event.GetType()] };
ResponseVec& receivers{ packageReceiverRegistry[event.type] };
for (auto& receiver : receivers)
{
receiver->Receive(event.GetData());
receiver->Receive(event.data);
}
auto& funcs{ staticPackageReceiverRegistry[event.GetType()] };
for (auto func : funcs)
{
func(event.GetData());
}
//auto& funcs{ staticPackageReceiverRegistry[event.GetType()] };
//for (auto func : funcs)
//{
// func(event.GetData());
//}
}
}

View File

@ -12,13 +12,12 @@
#include "SHEvent.h"
#include "SHEventReceiver.h"
#include <list>
#include <unordered_map>
#include <functional>
namespace SHADE
{
using ResponseFunction = std::function<void(ConstPackagePtr&)>;
using ResponseFunction = std::function<void(SHEvent)>;
using ReceiverPtr = std::shared_ptr<SHEventReceiver>;
using ResponseVec = std::vector<ReceiverPtr>;
using StaticResponseVec = std::vector<ResponseFunction>;
@ -68,22 +67,19 @@ namespace SHADE
* \brief Links a function pointer from a subscriber to a particular
* package type
****************************************************************************/
void SubscribeTo(SHPackageType, ReceiverPtr);
void SubscribeTo(SHEventIdentifier, ReceiverPtr);
void StaticSubscribeTo(SHPackageType, ResponseFunction);
template<typename T>
T* BroadcastData(T data, SHEventIdentifier eventType);
private:
// Singleton instance
static SHEventManager* instance;
// Registry for broadcasters and subscribers
std::unordered_map<
SHPackageType,
ResponseVec> packageReceiverRegistry;
std::unordered_map<SHEventIdentifier, ResponseVec> packageReceiverRegistry;
std::unordered_map<SHEventHandle, SHEventDataPtr> dataEventMap;
// Registry for static broadcasters and subscribers
std::unordered_map<
SHPackageType,
StaticResponseVec> staticPackageReceiverRegistry;
SHEventHandle handleCounter;
/****************************************************************************
* \param ListenerConstPtr - Const pointer to listener that sent event.
@ -101,12 +97,9 @@ namespace SHADE
* \brief Registers receiver as a subscriber to listener in the registry.
****************************************************************************/
void RegisterReceiverToType(SHPackageType, ReceiverPtr);
void RegisterStaticReceiverToType(SHPackageType, ResponseFunction);
void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr);
};
}
#endif // !SH_EVENTS_MANAGER_H

View File

@ -9,7 +9,7 @@ namespace SHADE
{
private:
public:
virtual void Receive(PackagePtr) = 0;
virtual void Receive(SHEventDataPtr) = 0;
};
template<typename T>
@ -17,16 +17,16 @@ namespace SHADE
{
private:
T* object;
void(T::*callback)(PackagePtr);
void(T::*callback)(SHEventDataPtr);
public:
SHEventReceiverSpec(T* obj, void(T::*cb)(PackagePtr))
:SHEventReceiver(), object{obj}, callback{ cb }
SHEventReceiverSpec(T* obj, void(T::* cb)(SHEventDataPtr))
:SHEventReceiver(), object{ obj }, callback{ cb }
{
}
void Receive(PackagePtr evt)
void Receive(SHEventDataPtr evt) override
{
(object->*callback)(evt);
}