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"
17
18namespace sys
19{
24class Messaging : public System
25{
26public:
27#pragma region static_functions
28
32 static auto getEnum() -> Type
33 {
34 return Type::Messaging;
35 }
36
43 static auto Broadcast(const std::string& eventName, const Message* message) -> std::vector<Message>;
44
45#pragma endregion
46
47 Messaging();
48 ~Messaging() override = default;
49 Messaging(const Messaging& other) = default;
50 Messaging(Messaging&& other) noexcept;
51 auto operator=(const Messaging& other) -> Messaging&;
52 auto operator=(Messaging&& other) noexcept -> Messaging& = default;
53
54
55#pragma region overridden_functions
56
57 auto clone() const -> std::unique_ptr<GameObject> override
58 {
59 return std::make_unique<Messaging>(*this);
60 }
61
62
63 auto onUpdate(float dt) -> void override;
64 auto onRender() -> void override;
65
66 auto endWindow() -> void override
67 {
68 }; // for debug ImGUI
69 auto load(Stream& stream) -> void override;
70 auto save(Stream& stream) const -> void override;
71
72protected:
73 auto systemShowMenu() -> void override;
74
75public:
76#pragma endregion
77
78#pragma region Messaging_Functions
79
80
81#pragma endregion
82
93 template <typename classType>
94 auto connect(
95 const std::string& eventName,
96 Message (classType::*functionToLink)(const Message*),
97 const std::string& functionName,
98 classType* pointerToSelf
99 ) -> void
100 {
101 hlg::Debug("Messaging is connecting a function of name:{} to the event:{} with the owner"
102 ": {}::{}", functionName, eventName, pointerToSelf->getName(), pointerToSelf->getUUID());
103 // Create a function that is the bound function pointer to the class instance
104 std::function<Message(const Message*)> functionToCall = std::bind_front(functionToLink, pointerToSelf);
105 // Create a Member Function with the bound function, function name, and a pointer to the owner
106 th::MemberFunction<Message, const Message*> connectedFunction{functionToCall, functionName, pointerToSelf};
107 // Attach the Member Function to the event
108 functionMap.insert({eventName, connectedFunction});
109 }
110
117 auto remove(const std::string& functionToRemove, GameObject* owner) -> void;
118
119 auto removeAll(const GameObject* owner) -> void;
120
128 auto broadcast(const std::string& eventName, const Message* message) -> std::vector<Message>;
129
130private:
131#pragma region member_variables
132 // The map that stores all functions linked to a map
133 std::unordered_multimap<std::string, th::MemberFunction<Message, const Message*>> functionMap;
134 //
135#pragma endregion
136
137#pragma region helper_functions
138
139
140 // Lambda for checking if a message's owner doesn't exist anymore
141 inline static auto HasParentExpired = [](const auto& item)
142 {
143 const auto& [key, value] = item;
144 return !value.hasValidOwner();
145 };
146
147#pragma endregion
148
149};
150}
sys::Json Stream
Definition AudioObject.h:20
the base class for the engine, most things inherit from this.
Definition GameObject.h:77
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:23
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:141
std::unordered_multimap< std::string, th::MemberFunction< Message, const Message * > > functionMap
Definition Messaging.h:133
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Messaging.h:57
auto onUpdate(float dt) -> void override
called once every frame.
Definition Messaging.cpp:59
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:124
Messaging(const Messaging &other)=default
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Messaging.cpp:73
auto systemShowMenu() -> void override
Specific systems should override this function to show their specific menu for the system.
Definition Messaging.cpp:78
auto broadcast(const std::string &eventName, const Message *message) -> std::vector< Message >
Calls all functions linked to eventName with the parameter message Example Call: messagingSystem->em...
Definition Messaging.cpp:187
auto load(Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Messaging.cpp:68
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Messaging.cpp:64
Messaging()
Definition Messaging.cpp:35
auto operator=(Messaging &&other) noexcept -> Messaging &=default
~Messaging() override=default
static auto getEnum() -> Type
function required by all systems
Definition Messaging.h:32
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Messaging.h:66
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:26
auto operator=(const Messaging &other) -> Messaging &
Definition Messaging.cpp:45
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:94
auto removeAll(const GameObject *owner) -> void
Definition Messaging.cpp:143
Definition MemberFunction.h:27
auto Debug(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:62
the type of elements in a basic_json container
Definition GameObject.h:32
Definition Message.h:34