OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
delegate.h
1/*
2===========================================================================
3Copyright (C) 2025 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23#pragma once
24
25#include <functional>
26#include "container.h"
27
28template<typename T>
29using Delegate = std::function<T>;
30
31struct DelegateHandle {
32public:
33 DelegateHandle();
34
35 bool operator==(const DelegateHandle& other) const;
36 bool operator!=(const DelegateHandle& other) const;
37
38private:
39 static uint64_t GenerateDelegateID();
40 static uint64_t currentHandle;
41
42 uint64_t handle;
43};
44
45template<typename T>
46class DelegateMultiElement
47{
48public:
49 DelegateMultiElement(Delegate<T>&& inFunction);
50
51 template<typename... Args>
52 void Execute(Args&&...args) const;
53
54 DelegateHandle GetHandle() const;
55
56private:
57 DelegateHandle handle;
58 Delegate<T> func;
59};
60
61template<typename T>
62DelegateMultiElement<T>::DelegateMultiElement(Delegate<T>&& inFunction)
63 : func(inFunction)
64{}
65
66template<typename T>
67template<typename... Args>
68void DelegateMultiElement<T>::Execute(Args&&...args) const
69{
70 func(std::move(args)...);
71}
72
73template<typename T>
74DelegateHandle DelegateMultiElement<T>::GetHandle() const
75{
76 return handle;
77}
78
79template<typename T>
81{
82public:
83 DelegateHandle Add(Delegate<T>&& function);
84 void Remove(DelegateHandle handle);
85
86 template<typename... Args>
87 void Execute(Args&&...args);
88
89private:
91};
92
93template<typename T>
94DelegateHandle MulticastDelegate<T>::Add(Delegate<T>&& function)
95{
96 int index = delegates.AddObject(DelegateMultiElement<T>(std::move(function)));
97
98 return delegates.ObjectAt(index).GetHandle();
99}
100
101template<typename T>
102void MulticastDelegate<T>::Remove(DelegateHandle handle)
103{
104 int i;
105
106 for (i = delegates.NumObjects(); i > 0; i--) {
107 const DelegateMultiElement<T>& elem = delegates.ObjectAt(i);
108
109 if (elem.GetHandle() == handle) {
110 delegates.RemoveObjectAt(i);
111 break;
112 }
113 }
114}
115
116template<typename T>
117template<typename... Args>
118void MulticastDelegate<T>::Execute(Args&&...args)
119{
120 size_t i;
121
122 for (i = 1; i <= delegates.NumObjects(); i++) {
123 const DelegateMultiElement<T>& element = delegates.ObjectAt(i);
124
125 element.Execute(std::move(args)...);
126 }
127}
Definition container.h:85
Definition delegate.h:47
Definition delegate.h:81
Definition delegate.h:31