Fix bugs
This commit is contained in:
+91
-29
@@ -7,6 +7,7 @@ class Delegate
|
|||||||
{
|
{
|
||||||
struct DelegateDataBase
|
struct DelegateDataBase
|
||||||
{
|
{
|
||||||
|
virtual ~DelegateDataBase() = default;
|
||||||
DelegateDataBase* next = nullptr;
|
DelegateDataBase* next = nullptr;
|
||||||
long long Hash = 0;
|
long long Hash = 0;
|
||||||
virtual void Call(TypeParameter) = 0;
|
virtual void Call(TypeParameter) = 0;
|
||||||
@@ -48,20 +49,20 @@ public:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void bind(T* object, void(T::*method)(TypeParameter))
|
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<const char*>(&object);
|
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||||
for (int i = 0; i < 8; ++i)
|
for (long unsigned int i = 0; i < sizeof(T*); ++i)
|
||||||
str[i] = ptrToObjectRef[i];
|
str[i] = ptrToObjectRef[i];
|
||||||
|
|
||||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||||
for (int i = 8; i < 16; ++i)
|
for (long unsigned int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)(TypeParameter)); ++i)
|
||||||
str[i] = ptrToMethodRef[i - 8];
|
str[i] = ptrToMethodRef[i - sizeof(T*)];
|
||||||
|
|
||||||
DelegateDataForClass<T>* data = new DelegateDataForClass<T>;
|
DelegateDataForClass<T>* data = new DelegateDataForClass<T>;
|
||||||
data->next = firstElement;
|
data->next = firstElement;
|
||||||
data->object = object;
|
data->object = object;
|
||||||
data->method = method;
|
data->method = method;
|
||||||
data->Hash = FNV1a(str, 16);
|
data->Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)(TypeParameter)));
|
||||||
|
|
||||||
delete[] str;
|
delete[] str;
|
||||||
|
|
||||||
@@ -72,15 +73,15 @@ public:
|
|||||||
|
|
||||||
void bind(void(*func)(TypeParameter))
|
void bind(void(*func)(TypeParameter))
|
||||||
{
|
{
|
||||||
char* str = new char[8];
|
char* str = new char[sizeof(void(*)(TypeParameter))];
|
||||||
const char* ptrToFuncRef = reinterpret_cast<const char*>(&func);
|
const char* ptrToFuncRef = reinterpret_cast<const char*>(&func);
|
||||||
for (int i = 0; i < 8; ++i)
|
for (int i = 0; i < sizeof(void(*)(TypeParameter)); ++i)
|
||||||
str[i] = ptrToFuncRef[i];
|
str[i] = ptrToFuncRef[i];
|
||||||
|
|
||||||
DelegateDataForFunction* data = new DelegateDataForFunction;
|
DelegateDataForFunction* data = new DelegateDataForFunction;
|
||||||
data->next = firstElement;
|
data->next = firstElement;
|
||||||
data->func = func;
|
data->func = func;
|
||||||
data->Hash = FNV1a(str, 8);
|
data->Hash = FNV1a(str, sizeof(func));
|
||||||
delete[] str;
|
delete[] str;
|
||||||
|
|
||||||
mDelegateList.lock();
|
mDelegateList.lock();
|
||||||
@@ -94,16 +95,16 @@ public:
|
|||||||
if (firstElement == nullptr)
|
if (firstElement == nullptr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char* str = new char[16];
|
char* str = new char[sizeof(T*) + sizeof(void(T::*)(TypeParameter))];
|
||||||
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||||
for (int i = 0; i < 8; ++i)
|
for (long unsigned int i = 0; i < 8; ++i)
|
||||||
str[i] = ptrToObjectRef[i];
|
str[i] = ptrToObjectRef[i];
|
||||||
|
|
||||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||||
for (int i = 8; i < 16; ++i)
|
for (long unsigned int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)(TypeParameter)); ++i)
|
||||||
str[i] = ptrToMethodRef[i - 8];
|
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* iterator = firstElement;
|
||||||
DelegateDataBase* temp = nullptr;
|
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<const char*>(&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<DelegateDataForFunction*>(iterator);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp = iterator;
|
||||||
|
iterator = iterator->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Call(TypeParameter data) const
|
void Call(TypeParameter data) const
|
||||||
{
|
{
|
||||||
DelegateDataBase* iterator = firstElement;
|
DelegateDataBase* iterator = firstElement;
|
||||||
@@ -141,9 +172,10 @@ class Delegate<void>
|
|||||||
{
|
{
|
||||||
struct DelegateDataBase
|
struct DelegateDataBase
|
||||||
{
|
{
|
||||||
|
virtual ~DelegateDataBase() = default;
|
||||||
DelegateDataBase* next = nullptr;
|
DelegateDataBase* next = nullptr;
|
||||||
long long Hash = 0;
|
long long Hash = 0;
|
||||||
virtual void Call() = 0;
|
virtual void call() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
@@ -154,7 +186,7 @@ class Delegate<void>
|
|||||||
typedef void(T::*Method)();
|
typedef void(T::*Method)();
|
||||||
Method method = nullptr;
|
Method method = nullptr;
|
||||||
|
|
||||||
void Call() override
|
void call() override
|
||||||
{
|
{
|
||||||
(object->*method)();
|
(object->*method)();
|
||||||
}
|
}
|
||||||
@@ -165,7 +197,7 @@ class Delegate<void>
|
|||||||
typedef void(*Func)();
|
typedef void(*Func)();
|
||||||
Func func = nullptr;
|
Func func = nullptr;
|
||||||
|
|
||||||
void Call() override
|
void call() override
|
||||||
{
|
{
|
||||||
func();
|
func();
|
||||||
}
|
}
|
||||||
@@ -182,20 +214,20 @@ public:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void bind(T* object, void(T::*method)())
|
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<const char*>(&object);
|
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||||
for (int i = 0; i < 8; ++i)
|
for (int i = 0; i < sizeof(T*); ++i)
|
||||||
str[i] = ptrToObjectRef[i];
|
str[i] = ptrToObjectRef[i];
|
||||||
|
|
||||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||||
for (int i = 8; i < 16; ++i)
|
for (int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)()); ++i)
|
||||||
str[i] = ptrToMethodRef[i - 8];
|
str[i] = ptrToMethodRef[i - sizeof(T*)];
|
||||||
|
|
||||||
DelegateDataForClass<T>* data = new DelegateDataForClass<T>;
|
DelegateDataForClass<T>* data = new DelegateDataForClass<T>;
|
||||||
data->next = firstElement;
|
data->next = firstElement;
|
||||||
data->object = object;
|
data->object = object;
|
||||||
data->method = method;
|
data->method = method;
|
||||||
data->Hash = FNV1a(str, 16);
|
data->Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)()));
|
||||||
|
|
||||||
delete[] str;
|
delete[] str;
|
||||||
|
|
||||||
@@ -206,9 +238,9 @@ public:
|
|||||||
|
|
||||||
void bind(void(*func)())
|
void bind(void(*func)())
|
||||||
{
|
{
|
||||||
char* str = new char[8];
|
char* str = new char[sizeof(func)];
|
||||||
const char* ptrToFuncRef = reinterpret_cast<const char*>(&func);
|
const char* ptrToFuncRef = reinterpret_cast<const char*>(&func);
|
||||||
for (int i = 0; i < 8; ++i)
|
for (int i = 0; i < sizeof(void(*)()); ++i)
|
||||||
str[i] = ptrToFuncRef[i];
|
str[i] = ptrToFuncRef[i];
|
||||||
|
|
||||||
DelegateDataForFunction* data = new DelegateDataForFunction;
|
DelegateDataForFunction* data = new DelegateDataForFunction;
|
||||||
@@ -228,16 +260,16 @@ public:
|
|||||||
if (firstElement == nullptr)
|
if (firstElement == nullptr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char* str = new char[16];
|
char* str = new char[sizeof(T*) + sizeof(void(T::*)())];
|
||||||
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||||
for (int i = 0; i < 8; ++i)
|
for (int i = 0; i < sizeof(T*); ++i)
|
||||||
str[i] = ptrToObjectRef[i];
|
str[i] = ptrToObjectRef[i];
|
||||||
|
|
||||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||||
for (int i = 8; i < 16; ++i)
|
for (int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)()); ++i)
|
||||||
str[i] = ptrToMethodRef[i - 8];
|
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* iterator = firstElement;
|
||||||
DelegateDataBase* temp = nullptr;
|
DelegateDataBase* temp = nullptr;
|
||||||
@@ -259,12 +291,42 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unbind(void(*func)())
|
||||||
|
{
|
||||||
|
char* str = new char[sizeof(void(*)())];
|
||||||
|
const char* ptrToFuncRef = reinterpret_cast<const char*>(&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<DelegateDataForFunction*>(iterator);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp = iterator;
|
||||||
|
iterator = iterator->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Call() const
|
void Call() const
|
||||||
{
|
{
|
||||||
DelegateDataBase* iterator = firstElement;
|
DelegateDataBase* iterator = firstElement;
|
||||||
while (iterator != nullptr)
|
while (iterator != nullptr)
|
||||||
{
|
{
|
||||||
iterator->Call();
|
iterator->call();
|
||||||
iterator = iterator->next;
|
iterator = iterator->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user