Brunot
Loading...
Searching...
No Matches
MemberFunction.h
Go to the documentation of this file.
1// File: MemberFunction.h
2// Description: A wrapper around a std::function bound to an instance of a class
3// that lets us compare if they are the same and have the same owner (std::function does not natively support this functionality)
4// Basically, a way to store a member function and call it later
5// Author(s): Ori Balashov (ori.balashov@digipen.edu)
6// 2025 / 10 / 25
7// (C) Digipen 2025
8// ____ __ __ __
9// /\__ _\/\ \ /\ \/\ \
10// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
11// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
12// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
13// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
14// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
15#pragma once
16#include <functional>
17#include <string>
18
20
21class GameObject;
22
23namespace th
24{
25template <typename returnType, typename... parameter>
27{
28public:
29 MemberFunction() = delete;
30
42 MemberFunction(std::function<returnType(parameter...)> function, std::string nameOfFunction, GameObject* funcOwner)
43 : actualFunction(std::move(function))
44 , functionName(std::move(nameOfFunction))
45 , functionOwner(funcOwner)
46 {
47 }
48
55 auto operator==(const MemberFunction& rhs) const -> bool
56 {
57 if (hasValidOwner() && rhs.hasValidOwner())
58 {
59 return functionName == rhs.functionName && functionOwner->getUUID() == rhs.functionOwner->getUUID();
60 }
61 return false;
62 }
63
69 auto operator()(parameter... inputParameter) -> returnType
70 {
71 return actualFunction(inputParameter...);
72 }
73
79 auto hasValidOwner() const -> bool
80 {
81 if (functionOwner->destroyed())
82 {
83 return false;
84 }
85 return true;
86 }
87
88 [[nodiscard]] auto function_owner() const -> GameObject*
89 {
90 return functionOwner;
91 }
92
93 [[nodiscard]] auto function_name() const -> const std::string&
94 {
95 return functionName;
96 }
97
98private:
99 // The function (should be std::bound_front to the owner before being put into the class)
100 std::function<returnType(parameter...)> actualFunction;
101 // The name of the function being bound
102 std::string functionName;
103
104private:
105 // The object that is associated with the member function
107};
108}
the base class for the engine, most things inherit from this.
the base class for the engine, most things inherit from this.
Definition GameObject.h:77
auto operator==(const MemberFunction &rhs) const -> bool
Definition MemberFunction.h:55
std::function< returnType(parameter...)> actualFunction
Definition MemberFunction.h:100
auto hasValidOwner() const -> bool
Checks if the owner of the member function still exists.
Definition MemberFunction.h:79
GameObject * functionOwner
Definition MemberFunction.h:106
MemberFunction()=delete
auto operator()(parameter... inputParameter) -> returnType
Call the function associated with a Member Function.
Definition MemberFunction.h:69
auto function_name() const -> const std::string &
Definition MemberFunction.h:93
MemberFunction(std::function< returnType(parameter...)> function, std::string nameOfFunction, GameObject *funcOwner)
Creates a MemberFunction from a given std::function, a name, and an owner NOTE: The std::function r...
Definition MemberFunction.h:42
std::string functionName
Definition MemberFunction.h:102
auto function_owner() const -> GameObject *
Definition MemberFunction.h:88
Definition Factory.cpp:59