mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-21 20:08:40 +03:00
Started generic class reflection system
This commit is contained in:
@@ -32,6 +32,30 @@ namespace Nuake
|
||||
|
||||
void ExampleFunction() { }
|
||||
|
||||
class TestClass : public Class
|
||||
{
|
||||
NUAKECLASS(TestClass)
|
||||
static void InitializeClass()
|
||||
{
|
||||
BindField<&TestClass::Intensity>("Intensity", "Intensity");
|
||||
BindProperty<&TestClass::SetIntensity, &TestClass::GetIntensity>("Intensity2", "Intensity2");
|
||||
}
|
||||
|
||||
public:
|
||||
float Intensity = 1.0f;
|
||||
|
||||
void SetIntensity(float value)
|
||||
{
|
||||
Intensity = value;
|
||||
}
|
||||
|
||||
float GetIntensity()
|
||||
{
|
||||
return Intensity;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void ExampleModuleLog(const std::string& hi)
|
||||
{
|
||||
Nuake::Logger::Log(hi, "ExampleModule", Nuake::VERBOSE);
|
||||
@@ -42,6 +66,9 @@ void ExampleModule_Startup()
|
||||
{
|
||||
using namespace Nuake;
|
||||
|
||||
TestClass::InternalInitializeClass();
|
||||
|
||||
|
||||
auto& module = ModuleDB::Get().RegisterModule<ExampleModule>();
|
||||
module.instance = module.Resolve().construct();
|
||||
|
||||
@@ -50,7 +77,7 @@ void ExampleModule_Startup()
|
||||
// This is to expose functions to the rest of the engine
|
||||
module.BindFunction<ExampleFunction>("ExampleFunction");
|
||||
module.BindFunction<ExampleModuleLog>("ExampleModuleLog", "hi");
|
||||
module.Invoke("ExampleModuleLog", "Hello World");
|
||||
//module.Invoke("ExampleModuleLog", "Hello World");
|
||||
|
||||
entt::id_type typeId = entt::hashed_string("ExampleModuleLog");
|
||||
entt::meta_type metaType = entt::resolve(typeId);
|
||||
|
||||
@@ -3,8 +3,79 @@
|
||||
#include <any>
|
||||
#include <vector>
|
||||
|
||||
#include "Nuake/Core/Object/Object.h"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
class Class
|
||||
{
|
||||
protected:
|
||||
static void InitializeClass() {}
|
||||
|
||||
static void (*GetInitializeClass(void))()
|
||||
{
|
||||
return &Class::InitializeClass;
|
||||
}
|
||||
|
||||
public:
|
||||
static std::string GetName(const entt::meta_type& componentMeta);
|
||||
};
|
||||
|
||||
#define NUAKECLASS(klass) \
|
||||
public: \
|
||||
static std::string ClassName() \
|
||||
{ \
|
||||
static std::string className = #klass; \
|
||||
return className; \
|
||||
} \
|
||||
\
|
||||
static void (*GetInitializeClass())() \
|
||||
{ \
|
||||
return &klass::InitializeClass; \
|
||||
} \
|
||||
\
|
||||
inline static auto ClassFactory = entt::meta<klass>(); \
|
||||
static void InternalInitializeClass() \
|
||||
{ \
|
||||
static bool initialized = false; \
|
||||
if (initialized) \
|
||||
return; \
|
||||
\
|
||||
ClassFactory.type(entt::hashed_string(#klass)); \
|
||||
ClassFactory.func<&klass::ClassName>(entt::hashed_string("ClassName")); \
|
||||
if (klass::GetInitializeClass() != Class::GetInitializeClass()) \
|
||||
{ \
|
||||
GetInitializeClass()(); \
|
||||
} \
|
||||
\
|
||||
initialized = true; \
|
||||
} \
|
||||
\
|
||||
template<auto Data> \
|
||||
static auto BindField(const char* varName, const char* displayName) \
|
||||
{ \
|
||||
return ClassFactory \
|
||||
.data<Data>(entt::hashed_string(varName)) \
|
||||
.prop(Nuake::HashedName::DisplayName, displayName); \
|
||||
} \
|
||||
\
|
||||
template<auto Getter, auto Setter> \
|
||||
static auto BindProperty(const char* varName, const char* displayName) \
|
||||
{ \
|
||||
return ClassFactory \
|
||||
.data<Getter, Setter>(entt::hashed_string(varName)) \
|
||||
.prop(Nuake::HashedName::DisplayName, displayName); \
|
||||
} \
|
||||
\
|
||||
template<auto Func> \
|
||||
static void BindAction(const char* funcName, const char* actionName) \
|
||||
{ \
|
||||
ClassFactory \
|
||||
.func<Func>(entt::hashed_string(funcName)) \
|
||||
.prop(Nuake::HashedName::DisplayName, actionName); \
|
||||
}
|
||||
|
||||
|
||||
#define NUAKEMODULE(moduleName) \
|
||||
using TypeNameMap = std::map<entt::id_type, std::string>; \
|
||||
\
|
||||
|
||||
Reference in New Issue
Block a user