Brunot
Loading...
Searching...
No Matches
Messaging.h
Go to the documentation of this file.
1// File: Messaging.h
2// Description: A system that holds a map of events and functions linked to events. It can call the functions linked to an event with a passed-in message
3// Author(s): Ori Balashov (ori.balashov@digipen.edu)
4// 2025 / 10 / 22
5// (C) Digipen 2025
6// ____ __ __ __
7// /\__ _\/\ \ /\ \/\ \
8// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
9// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
10// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
11// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
12// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
13#pragma once
14
16#include "Framework/System.h"
18
19
20namespace sys
21{
26class Messaging : public System
27{
28public:
29#pragma region static_functions
30
34 static auto getEnum() -> Type
35 {
36 return Type::Messaging;
37 }
38
45 static auto Broadcast(const std::string& eventName, const Message* message) -> std::vector<Message>;
46
47 static auto bindLuaTypes(sol::state& lua) -> void;
48
49#pragma endregion
50
51 Messaging();
52 ~Messaging() override = default;
53 Messaging(const Messaging& other) = default;
54 Messaging(Messaging&& other) noexcept;
55 auto operator=(const Messaging& other) -> Messaging&;
56 auto operator=(Messaging&& other) noexcept -> Messaging& = default;
57
58
59#pragma region overridden_functions
60
61 auto clone() const -> std::unique_ptr<GameObject> override
62 {
63 return std::make_unique<Messaging>(*this);
64 }
65
66
67 auto onUpdate(float dt) -> void override;
68 auto onRender() -> void override;
69
70 auto endWindow() -> void override
71 {
72 }; // for debug ImGUI
73 auto load(const Stream& stream) -> void override;
74 auto save(Stream& stream) const -> void override;
75
76protected:
77 auto systemShowMenu() -> void override;
78
79public:
80#pragma endregion
81
82#pragma region Messaging_Functions
83
84
85#pragma endregion
86
97 template <typename classType>
98 auto connect(
99 const std::string& eventName,
100 Message (classType::*functionToLink)(const Message*),
101 const std::string& functionName,
102 classType* pointerToSelf
103 ) -> void
104 {
105 hlg::Debug("Messaging is connecting a function of name:{} to the event:{} with the owner"
106 ": {}::{}", functionName, eventName, pointerToSelf->getName(), pointerToSelf->getUUID());
107 // Create a function that is the bound function pointer to the class instance
108 std::function<Message(const Message*)> functionToCall = std::bind_front(functionToLink, pointerToSelf);
109 // Create a Member Function with the bound function, function name, and a pointer to the owner
110 th::MemberFunction<Message, const Message*> connectedFunction{functionToCall, functionName, pointerToSelf};
111 // Attach the Member Function to the event
112 functionMap.insert({eventName, connectedFunction});
113 }
114
123#define TH_CONNECT_MESSAGE(event) messaging->connect< TH_CURRENT_CLASS >( #event , & TH_CURRENT_CLASS ::receive##event ,"receive" #event ,this);
124
131 auto remove(const std::string& functionToRemove, GameObject* owner) -> void;
132
133 auto removeAll(const GameObject* owner) -> void;
134
144 auto broadcast(std::string eventName, const Message* message) -> std::vector<Message>;
145
146
160 auto forceBroadcast(std::string eventName, const Message* message) -> std::vector<Message>;
161
162private:
163#pragma region member_variables
164 // The map that stores all functions linked to a map
165 std::unordered_multimap<std::string, th::MemberFunction<Message, const Message*>> functionMap;
166 //
167#pragma endregion
168
169#pragma region helper_functions
170
171
172 // Lambda for checking if a message's owner doesn't exist anymore
173 inline static auto HasParentExpired = [](const auto& item)
174 {
175 const auto& [key, value] = item;
176 return !value.hasValidOwner();
177 };
178
185 auto getFunctionsToCall(const std::string& eventName, bool broadcastToNonReceiving) -> std::vector<th::MemberFunction<Message, const Message*>*>;
186
194 auto broadcastToFunctions(const std::string& eventName, const Message* message,
195 const std::vector<th::MemberFunction<Message, const Message*>*>& functionsToCall) -> std::vector<
196 Message>;
197
198#pragma endregion
199
200};
201}
sys::Json Stream
Definition AudioObject.h:20
the base class for the engine, most things inherit from this.
Definition GameObject.h:83
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:24
Type
Definition System.h:20
@ Messaging
Definition System.h:26
System(const std::string &typeName, Type systemType)
Definition System.cpp:75
static auto HasParentExpired
Definition Messaging.h:173
std::unordered_multimap< std::string, th::MemberFunction< Message, const Message * > > functionMap
Definition Messaging.h:165
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Messaging.h:61
auto onUpdate(float dt) -> void override
called once every frame.
Definition Messaging.cpp:73
auto remove(const std::string &functionToRemove, GameObject *owner) -> void
Removes all functions with the given name that belong to the given owner Example Call: messagingSyst...
Definition Messaging.cpp:138
Messaging(const Messaging &other)=default
auto getFunctionsToCall(const std::string &eventName, bool broadcastToNonReceiving) -> std::vector< th::MemberFunction< Message, const Message * > * >
helper function for broadcast and forceBroadcast, to collect all functions that will be called
Definition Messaging.cpp:202
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Messaging.cpp:87
auto systemShowMenu() -> void override
Specific systems should override this function to show their specific menu for the system.
Definition Messaging.cpp:92
auto broadcastToFunctions(const std::string &eventName, const Message *message, const std::vector< th::MemberFunction< Message, const Message * > * > &functionsToCall) -> std::vector< Message >
helper function that takes a list of functions to call, and broadcasts an event with a message to the...
Definition Messaging.cpp:230
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Messaging.cpp:78
Messaging()
Definition Messaging.cpp:49
auto operator=(Messaging &&other) noexcept -> Messaging &=default
~Messaging() override=default
static auto bindLuaTypes(sol::state &lua) -> void
Definition Messaging.cpp:35
auto broadcast(std::string eventName, const Message *message) -> std::vector< Message >
Calls all functions linked to eventName with the parameter message Example Call: messagingSystem->br...
Definition Messaging.cpp:257
static auto getEnum() -> Type
function required by all systems
Definition Messaging.h:34
auto forceBroadcast(std::string eventName, const Message *message) -> std::vector< Message >
Calls all functions linked to eventName with the parameter message, just like broadcast does,...
Definition Messaging.cpp:272
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Messaging.cpp:82
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Messaging.h:70
static auto Broadcast(const std::string &eventName, const Message *message) -> std::vector< Message >
A helper function that gets the messaging system and calls broadcast on it for you.
Definition Messaging.cpp:28
auto operator=(const Messaging &other) -> Messaging &
Definition Messaging.cpp:59
auto connect(const std::string &eventName, Message(classType::*functionToLink)(const Message *), const std::string &functionName, classType *pointerToSelf) -> void
Attaches a member function to a specific event.
Definition Messaging.h:98
auto removeAll(const GameObject *owner) -> void
Definition Messaging.cpp:157
Definition MemberFunction.h:27
auto Debug(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:69
the type of elements in a basic_json container
Definition GameObject.h:37
Definition Message.h:35