From c1c5ffd7af914b510f7dbee1714ec1d1e1338263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB?= <119668441+Jiga228@users.noreply.github.com> Date: Thu, 10 Apr 2025 20:22:40 +0700 Subject: [PATCH] Fix bugs --- Delegate/Delegate.h | 120 +++++++++++++++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 29 deletions(-) diff --git a/Delegate/Delegate.h b/Delegate/Delegate.h index ea5afc6..1548fb8 100644 --- a/Delegate/Delegate.h +++ b/Delegate/Delegate.h @@ -7,6 +7,7 @@ class Delegate { struct DelegateDataBase { + virtual ~DelegateDataBase() = default; DelegateDataBase* next = nullptr; long long Hash = 0; virtual void Call(TypeParameter) = 0; @@ -48,20 +49,20 @@ public: template void bind(T* object, void(T::*method)(TypeParameter)) { - char* str = new char[16]; + char* str = new char[sizeof(T*) + sizeof(void(T::*)(TypeParameter))]; const char* ptrToObjectRef = reinterpret_cast(&object); - for (int i = 0; i < 8; ++i) + for (long unsigned int i = 0; i < sizeof(T*); ++i) str[i] = ptrToObjectRef[i]; const char* ptrToMethodRef = reinterpret_cast(&method); - for (int i = 8; i < 16; ++i) - str[i] = ptrToMethodRef[i - 8]; + for (long unsigned int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)(TypeParameter)); ++i) + str[i] = ptrToMethodRef[i - sizeof(T*)]; DelegateDataForClass* data = new DelegateDataForClass; data->next = firstElement; data->object = object; data->method = method; - data->Hash = FNV1a(str, 16); + data->Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)(TypeParameter))); delete[] str; @@ -72,15 +73,15 @@ public: void bind(void(*func)(TypeParameter)) { - char* str = new char[8]; + char* str = new char[sizeof(void(*)(TypeParameter))]; const char* ptrToFuncRef = reinterpret_cast(&func); - for (int i = 0; i < 8; ++i) + for (int i = 0; i < sizeof(void(*)(TypeParameter)); ++i) str[i] = ptrToFuncRef[i]; DelegateDataForFunction* data = new DelegateDataForFunction; data->next = firstElement; data->func = func; - data->Hash = FNV1a(str, 8); + data->Hash = FNV1a(str, sizeof(func)); delete[] str; mDelegateList.lock(); @@ -94,16 +95,16 @@ public: if (firstElement == nullptr) return; - char* str = new char[16]; + char* str = new char[sizeof(T*) + sizeof(void(T::*)(TypeParameter))]; const char* ptrToObjectRef = reinterpret_cast(&object); - for (int i = 0; i < 8; ++i) + for (long unsigned int i = 0; i < 8; ++i) str[i] = ptrToObjectRef[i]; const char* ptrToMethodRef = reinterpret_cast(&method); - for (int i = 8; i < 16; ++i) - str[i] = ptrToMethodRef[i - 8]; + for (long unsigned int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)(TypeParameter)); ++i) + str[i] = ptrToMethodRef[i - sizeof(T*)]; - unsigned int Hash = FNV1a(str, 16); + unsigned int Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)(TypeParameter))); DelegateDataBase* iterator = firstElement; DelegateDataBase* temp = nullptr; @@ -125,6 +126,36 @@ public: } } + void unbind(void(*func)(TypeParameter)) + { + char* str = new char[sizeof(void(*)(TypeParameter))]; + const char* ptrToFuncRef = reinterpret_cast(&func); + for (int i = 0; i < sizeof(void(*)(TypeParameter)); ++i) + str[i] = ptrToFuncRef[i]; + + unsigned int Hash = FNV1a(str, sizeof(void(*)(TypeParameter))); + delete[] str; + + DelegateDataBase* iterator = firstElement; + DelegateDataBase* temp = nullptr; + while (iterator != nullptr) + { + if (iterator->Hash == Hash) + { + if (temp != nullptr) + temp->next = iterator->next; + else + firstElement = iterator->next; + + delete static_cast(iterator); + break; + } + + temp = iterator; + iterator = iterator->next; + } + } + void Call(TypeParameter data) const { DelegateDataBase* iterator = firstElement; @@ -141,9 +172,10 @@ class Delegate { struct DelegateDataBase { + virtual ~DelegateDataBase() = default; DelegateDataBase* next = nullptr; long long Hash = 0; - virtual void Call() = 0; + virtual void call() = 0; }; template @@ -154,7 +186,7 @@ class Delegate typedef void(T::*Method)(); Method method = nullptr; - void Call() override + void call() override { (object->*method)(); } @@ -165,7 +197,7 @@ class Delegate typedef void(*Func)(); Func func = nullptr; - void Call() override + void call() override { func(); } @@ -182,20 +214,20 @@ public: template void bind(T* object, void(T::*method)()) { - char* str = new char[16]; + char* str = new char[sizeof(T*) + sizeof(void(T::*)())]; const char* ptrToObjectRef = reinterpret_cast(&object); - for (int i = 0; i < 8; ++i) + for (int i = 0; i < sizeof(T*); ++i) str[i] = ptrToObjectRef[i]; const char* ptrToMethodRef = reinterpret_cast(&method); - for (int i = 8; i < 16; ++i) - str[i] = ptrToMethodRef[i - 8]; + for (int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)()); ++i) + str[i] = ptrToMethodRef[i - sizeof(T*)]; DelegateDataForClass* data = new DelegateDataForClass; data->next = firstElement; data->object = object; data->method = method; - data->Hash = FNV1a(str, 16); + data->Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)())); delete[] str; @@ -206,9 +238,9 @@ public: void bind(void(*func)()) { - char* str = new char[8]; + char* str = new char[sizeof(func)]; const char* ptrToFuncRef = reinterpret_cast(&func); - for (int i = 0; i < 8; ++i) + for (int i = 0; i < sizeof(void(*)()); ++i) str[i] = ptrToFuncRef[i]; DelegateDataForFunction* data = new DelegateDataForFunction; @@ -228,16 +260,16 @@ public: if (firstElement == nullptr) return; - char* str = new char[16]; + char* str = new char[sizeof(T*) + sizeof(void(T::*)())]; const char* ptrToObjectRef = reinterpret_cast(&object); - for (int i = 0; i < 8; ++i) + for (int i = 0; i < sizeof(T*); ++i) str[i] = ptrToObjectRef[i]; const char* ptrToMethodRef = reinterpret_cast(&method); - for (int i = 8; i < 16; ++i) - str[i] = ptrToMethodRef[i - 8]; + for (int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)()); ++i) + str[i] = ptrToMethodRef[i - sizeof(T*)]; - unsigned int Hash = FNV1a(str, 16); + unsigned int Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)())); DelegateDataBase* iterator = firstElement; DelegateDataBase* temp = nullptr; @@ -259,12 +291,42 @@ public: } } + void unbind(void(*func)()) + { + char* str = new char[sizeof(void(*)())]; + const char* ptrToFuncRef = reinterpret_cast(&func); + for (int i = 0; i < sizeof(void(*)()); ++i) + str[i] = ptrToFuncRef[i]; + + unsigned int Hash = FNV1a(str, sizeof(void(*)())); + delete[] str; + + DelegateDataBase* iterator = firstElement; + DelegateDataBase* temp = nullptr; + while (iterator != nullptr) + { + if (iterator->Hash == Hash) + { + if (temp != nullptr) + temp->next = iterator->next; + else + firstElement = iterator->next; + + delete static_cast(iterator); + break; + } + + temp = iterator; + iterator = iterator->next; + } + } + void Call() const { DelegateDataBase* iterator = firstElement; while (iterator != nullptr) { - iterator->Call(); + iterator->call(); iterator = iterator->next; } }