Удалил старый ModelManager. Доработал режим сборки "Engine" теперь копирование инициируется не bat файлом а проектом. Теперь UwURenderEngine копирует свои заголовки
This commit is contained in:
+33
-8
@@ -4,23 +4,48 @@ find_package(Vulkan REQUIRED)
|
||||
|
||||
file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp")
|
||||
|
||||
add_library(${CORE_NAME} STATIC ${SRC})
|
||||
target_compile_features(${CORE_NAME} PRIVATE cxx_std_17)
|
||||
target_include_directories(${CORE_NAME} PUBLIC
|
||||
add_library(Core STATIC ${SRC})
|
||||
target_compile_features(Core PRIVATE cxx_std_17)
|
||||
target_include_directories(Core PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/FastRTTI
|
||||
${PROJECT_SOURCE_DIR}/Delegate
|
||||
)
|
||||
target_include_directories(${CORE_NAME}
|
||||
target_include_directories(Core
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${PROJECT_SOURCE_DIR}/glfw/Include
|
||||
${PROJECT_SOURCE_DIR}/RenderEngineSDK
|
||||
${PROJECT_SOURCE_DIR}/RenderEngineSDK
|
||||
${Vulkan_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(${CORE_NAME} PRIVATE
|
||||
target_link_libraries(Core PRIVATE
|
||||
${CMAKE_DL_LIBS} # For linux
|
||||
FastRTTI
|
||||
glfw
|
||||
Vulkan::Vulkan
|
||||
)
|
||||
)
|
||||
|
||||
file(GLOB_RECURSE HEADERS
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/*.hpp"
|
||||
)
|
||||
|
||||
foreach (HEADER ${HEADERS})
|
||||
file(RELATIVE_PATH R_PATH
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
"${HEADER}"
|
||||
)
|
||||
|
||||
get_filename_component(HEADER_DIR ${R_PATH} DIRECTORY)
|
||||
|
||||
add_custom_command(
|
||||
TARGET Core
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory
|
||||
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/include/${HEADER_DIR}"
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${HEADER}"
|
||||
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/include/${R_PATH}"
|
||||
|
||||
COMMENT "Copy Core headers"
|
||||
)
|
||||
endforeach ()
|
||||
@@ -0,0 +1,333 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
|
||||
template<typename TypeParameter>
|
||||
class Delegate
|
||||
{
|
||||
struct DelegateDataBase
|
||||
{
|
||||
virtual ~DelegateDataBase() = default;
|
||||
DelegateDataBase* next = nullptr;
|
||||
long long Hash = 0;
|
||||
virtual void Call(TypeParameter) = 0;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct DelegateDataForClass : DelegateDataBase
|
||||
{
|
||||
T* object = nullptr;
|
||||
|
||||
typedef void(T::*Method)(TypeParameter);
|
||||
Method method = nullptr;
|
||||
|
||||
void Call(TypeParameter data) override
|
||||
{
|
||||
(object->*method)(data);
|
||||
}
|
||||
};
|
||||
|
||||
struct DelegateDataForFunction : DelegateDataBase
|
||||
{
|
||||
typedef void(*Func)(TypeParameter);
|
||||
Func func = nullptr;
|
||||
|
||||
void Call(TypeParameter data) override
|
||||
{
|
||||
func(data);
|
||||
}
|
||||
};
|
||||
|
||||
DelegateDataBase* firstElement = nullptr;
|
||||
|
||||
std::mutex mDelegateList;
|
||||
|
||||
static constexpr unsigned int FNV1a(const char* str, unsigned int n, unsigned int hash = 2166136261U) {
|
||||
return n == 0 ? hash : FNV1a(str + 1, n - 1, (hash ^ str[0]) * 19777619U);
|
||||
}
|
||||
public:
|
||||
template<typename T>
|
||||
void bind(T* object, void(T::*method)(TypeParameter))
|
||||
{
|
||||
char* str = new char[sizeof(T*) + sizeof(void(T::*)(TypeParameter))];
|
||||
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||
for (long unsigned int i = 0; i < sizeof(T*); ++i)
|
||||
str[i] = ptrToObjectRef[i];
|
||||
|
||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||
for (long unsigned int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)(TypeParameter)); ++i)
|
||||
str[i] = ptrToMethodRef[i - sizeof(T*)];
|
||||
|
||||
DelegateDataForClass<T>* data = new DelegateDataForClass<T>;
|
||||
data->next = firstElement;
|
||||
data->object = object;
|
||||
data->method = method;
|
||||
data->Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)(TypeParameter)));
|
||||
|
||||
delete[] str;
|
||||
|
||||
mDelegateList.lock();
|
||||
firstElement = data;
|
||||
mDelegateList.unlock();
|
||||
}
|
||||
|
||||
void bind(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];
|
||||
|
||||
DelegateDataForFunction* data = new DelegateDataForFunction;
|
||||
data->next = firstElement;
|
||||
data->func = func;
|
||||
data->Hash = FNV1a(str, sizeof(func));
|
||||
delete[] str;
|
||||
|
||||
mDelegateList.lock();
|
||||
firstElement = data;
|
||||
mDelegateList.unlock();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void unbind(T* object, void(T::*method)(TypeParameter))
|
||||
{
|
||||
if (firstElement == nullptr)
|
||||
return;
|
||||
|
||||
char* str = new char[sizeof(T*) + sizeof(void(T::*)(TypeParameter))];
|
||||
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||
for (long unsigned int i = 0; i < 8; ++i)
|
||||
str[i] = ptrToObjectRef[i];
|
||||
|
||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||
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, sizeof(T*) + sizeof(void(T::*)(TypeParameter)));
|
||||
|
||||
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<DelegateDataForClass<T>*>(iterator);
|
||||
break;
|
||||
}
|
||||
|
||||
temp = iterator;
|
||||
iterator = iterator->next;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
DelegateDataBase* iterator = firstElement;
|
||||
while (iterator != nullptr)
|
||||
{
|
||||
iterator->Call(data);
|
||||
iterator = iterator->next;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Delegate<void>
|
||||
{
|
||||
struct DelegateDataBase
|
||||
{
|
||||
virtual ~DelegateDataBase() = default;
|
||||
DelegateDataBase* next = nullptr;
|
||||
long long Hash = 0;
|
||||
virtual void call() = 0;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct DelegateDataForClass : DelegateDataBase
|
||||
{
|
||||
T* object = nullptr;
|
||||
|
||||
typedef void(T::*Method)();
|
||||
Method method = nullptr;
|
||||
|
||||
void call() override
|
||||
{
|
||||
(object->*method)();
|
||||
}
|
||||
};
|
||||
|
||||
struct DelegateDataForFunction : DelegateDataBase
|
||||
{
|
||||
typedef void(*Func)();
|
||||
Func func = nullptr;
|
||||
|
||||
void call() override
|
||||
{
|
||||
func();
|
||||
}
|
||||
};
|
||||
|
||||
DelegateDataBase* firstElement = nullptr;
|
||||
|
||||
std::mutex mDelegateList;
|
||||
|
||||
static constexpr unsigned int FNV1a(const char* str, unsigned int n, unsigned int hash = 2166136261U) {
|
||||
return n == 0 ? hash : FNV1a(str + 1, n - 1, (hash ^ str[0]) * 19777619U);
|
||||
}
|
||||
public:
|
||||
template<typename T>
|
||||
void bind(T* object, void(T::*method)())
|
||||
{
|
||||
char* str = new char[sizeof(T*) + sizeof(void(T::*)())];
|
||||
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||
for (int i = 0; i < sizeof(T*); ++i)
|
||||
str[i] = ptrToObjectRef[i];
|
||||
|
||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||
for (int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)()); ++i)
|
||||
str[i] = ptrToMethodRef[i - sizeof(T*)];
|
||||
|
||||
DelegateDataForClass<T>* data = new DelegateDataForClass<T>;
|
||||
data->next = firstElement;
|
||||
data->object = object;
|
||||
data->method = method;
|
||||
data->Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)()));
|
||||
|
||||
delete[] str;
|
||||
|
||||
mDelegateList.lock();
|
||||
firstElement = data;
|
||||
mDelegateList.unlock();
|
||||
}
|
||||
|
||||
void bind(void(*func)())
|
||||
{
|
||||
char* str = new char[sizeof(func)];
|
||||
const char* ptrToFuncRef = reinterpret_cast<const char*>(&func);
|
||||
for (int i = 0; i < sizeof(void(*)()); ++i)
|
||||
str[i] = ptrToFuncRef[i];
|
||||
|
||||
DelegateDataForFunction* data = new DelegateDataForFunction;
|
||||
data->next = firstElement;
|
||||
data->func = func;
|
||||
data->Hash = FNV1a(str, 8);
|
||||
delete[] str;
|
||||
|
||||
mDelegateList.lock();
|
||||
firstElement = data;
|
||||
mDelegateList.unlock();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void unbind(T* object, void(T::*method)())
|
||||
{
|
||||
if (firstElement == nullptr)
|
||||
return;
|
||||
|
||||
char* str = new char[sizeof(T*) + sizeof(void(T::*)())];
|
||||
const char* ptrToObjectRef = reinterpret_cast<const char*>(&object);
|
||||
for (int i = 0; i < sizeof(T*); ++i)
|
||||
str[i] = ptrToObjectRef[i];
|
||||
|
||||
const char* ptrToMethodRef = reinterpret_cast<const char*>(&method);
|
||||
for (int i = sizeof(T*); i < sizeof(T*) + sizeof(void(T::*)()); ++i)
|
||||
str[i] = ptrToMethodRef[i - sizeof(T*)];
|
||||
|
||||
unsigned int Hash = FNV1a(str, sizeof(T*) + sizeof(void(T::*)()));
|
||||
|
||||
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<DelegateDataForClass<T>*>(iterator);
|
||||
break;
|
||||
}
|
||||
|
||||
temp = iterator;
|
||||
iterator = iterator->next;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
DelegateDataBase* iterator = firstElement;
|
||||
while (iterator != nullptr)
|
||||
{
|
||||
iterator->call();
|
||||
iterator = iterator->next;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
# Delegate
|
||||
That library implement delegate for C++.
|
||||
|
||||
--------Exemple-------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "Delegate.h"
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
void Foo1(int var) { std::cout << "A::Foo1(" << var <<");\n"; }
|
||||
void Foo2(int var) { std::cout << "A::Foo2(" << var <<");\n"; }
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
Delegate<int> delegate;
|
||||
A a;
|
||||
delegate.bind(&a, &A::Foo1);
|
||||
delegate.bind(&a, &A::Foo2);
|
||||
delegate.unbind(&a, &A::Foo1);
|
||||
delegate.Call(321);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
#include "ModelManager.hpp"
|
||||
|
||||
#include "Log/Log.hpp"
|
||||
|
||||
ModelManager::owner_counter::owner_counter(const owner_counter& other) : counter(other.counter.load()), model(other.model)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned int ModelManager::FNV1aHash(const char* buf)
|
||||
{
|
||||
unsigned int h_val = 0x811c9dc5;
|
||||
|
||||
while (*buf)
|
||||
{
|
||||
h_val ^= static_cast<unsigned int>(*buf++);
|
||||
h_val *= 0x01000193;
|
||||
}
|
||||
|
||||
return h_val;
|
||||
}
|
||||
|
||||
ModelManager::~ModelManager()
|
||||
{
|
||||
models.clear();
|
||||
}
|
||||
|
||||
ModelManager::StaticModel* ModelManager::LoadModel(const std::string& name)
|
||||
{
|
||||
if (name.empty())
|
||||
return nullptr;
|
||||
unsigned int hash = FNV1aHash(name.c_str());
|
||||
auto counter = models.find(hash);
|
||||
if (counter != models.cend())
|
||||
{
|
||||
++counter->second.counter;
|
||||
return counter->second.model;
|
||||
}
|
||||
|
||||
Loging::Log("Load new model: " + name);
|
||||
|
||||
// Load model_data
|
||||
owner_counter new_counter;
|
||||
new_counter.counter.store(1);
|
||||
new_counter.model = new StaticModel();
|
||||
models.try_emplace(hash, new_counter);
|
||||
return new_counter.model;
|
||||
}
|
||||
|
||||
void ModelManager::FreeModel(const std::string& name)
|
||||
{
|
||||
if (name.empty())
|
||||
return;
|
||||
|
||||
unsigned int hash = FNV1aHash(name.c_str());
|
||||
auto counter = models.find(hash);
|
||||
--counter->second.counter;
|
||||
|
||||
StaticModel* model = counter->second.model;
|
||||
if (counter->second.counter.load() == 0)
|
||||
{
|
||||
models.erase(hash);
|
||||
delete model;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
|
||||
class ModelManager
|
||||
{
|
||||
public:
|
||||
class StaticModel
|
||||
{
|
||||
//DATA
|
||||
};
|
||||
|
||||
struct owner_counter
|
||||
{
|
||||
std::atomic_ullong counter;
|
||||
StaticModel* model;
|
||||
|
||||
owner_counter() = default;
|
||||
owner_counter(const owner_counter& other);
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
std::unordered_map<unsigned int, owner_counter> models;
|
||||
|
||||
static unsigned int FNV1aHash (const char *buf);
|
||||
|
||||
public:
|
||||
~ModelManager();
|
||||
|
||||
StaticModel* LoadModel(const std::string& name);
|
||||
void FreeModel(const std::string& name);
|
||||
};
|
||||
Reference in New Issue
Block a user