Skip to content

Commit

Permalink
Merge pull request #404 from liangzai12/main
Browse files Browse the repository at this point in the history
simple function reflection
  • Loading branch information
liangzai12 authored Dec 12, 2022
2 parents 31d9942 + 627567c commit 65379de
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 28 deletions.
2 changes: 1 addition & 1 deletion engine/asset/objects/character/player/player.object.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
},
{
"$context": {
"lua_script": "print(\"hello world\")"
"lua_script": "if (get_bool(GameObject, \"MotorComponent.m_is_moving\")) then set_float(GameObject, \"MotorComponent.m_motor_res.m_jump_height\", 10) else set_float(GameObject, \"MotorComponent.m_motor_res.m_jump_height\", 5) end invoke(GameObject, \"MotorComponent.getOffStuckDead\")"
},
"$typeName": "LuaComponent"
}
Expand Down
18 changes: 18 additions & 0 deletions engine/source/meta_parser/parser/generator/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ namespace Generator
Mustache::data class_field_defines = Mustache::data::type::list;
genClassFieldRenderData(class_temp, class_field_defines);
class_def.set("class_field_defines", class_field_defines);


Mustache::data class_method_defines = Mustache::data::type::list;
genClassMethodRenderData(class_temp, class_method_defines);
class_def.set("class_method_defines", class_method_defines);
}
void GeneratorInterface::genClassFieldRenderData(std::shared_ptr<Class> class_temp, Mustache::data& feild_defs)
{
Expand All @@ -54,4 +59,17 @@ namespace Generator
feild_defs.push_back(filed_define);
}
}

void GeneratorInterface::genClassMethodRenderData(std::shared_ptr<Class> class_temp, Mustache::data& method_defs)
{
for (auto& method : class_temp->m_methods)
{
if (!method->shouldCompile())
continue;
Mustache::data method_define;

method_define.set("class_method_name", method->m_name);
method_defs.push_back(method_define);
}
}
} // namespace Generator
8 changes: 5 additions & 3 deletions engine/source/meta_parser/parser/generator/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ namespace Generator
virtual ~GeneratorInterface() {};

protected:
virtual void prepareStatus(std::string path);
virtual void genClassRenderData(std::shared_ptr<Class> class_temp, Mustache::data& class_def);
virtual void genClassFieldRenderData(std::shared_ptr<Class> class_temp, Mustache::data& feild_defs);
virtual void prepareStatus(std::string path);
virtual void genClassRenderData(std::shared_ptr<Class> class_temp, Mustache::data& class_def);
virtual void genClassFieldRenderData(std::shared_ptr<Class> class_temp, Mustache::data& feild_defs);
virtual void genClassMethodRenderData(std::shared_ptr<Class> class_temp, Mustache::data& method_defs);

virtual std::string processFileName(std::string path) = 0;

std::string m_out_path {"gen_src"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace Generator
// class defs
for (auto class_temp : schema.classes)
{
if (!class_temp->shouldCompileFilds())
if (!class_temp->shouldCompile())
continue;

class_names.insert_or_assign(class_temp->getClassName(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Generator
Mustache::data("headfile_name", Utils::makeRelativePath(m_root_path, path).string()));
for (auto class_temp : schema.classes)
{
if (!class_temp->shouldCompileFilds())
if (!class_temp->shouldCompileFields())
continue;

Mustache::data class_def;
Expand Down
13 changes: 11 additions & 2 deletions engine/source/meta_parser/parser/language_types/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,29 @@ Class::Class(const Cursor& cursor, const Namespace& current_namespace) :
case CXCursor_FieldDecl:
m_fields.emplace_back(new Field(child, current_namespace, this));
break;
// method
case CXCursor_CXXMethod:
m_methods.emplace_back(new Method(child, current_namespace, this));
default:
break;
}
}
}

bool Class::shouldCompile(void) const { return shouldCompileFilds(); }
bool Class::shouldCompile(void) const { return shouldCompileFields()|| shouldCompileMethods(); }

bool Class::shouldCompileFilds(void) const
bool Class::shouldCompileFields(void) const
{
return m_meta_data.getFlag(NativeProperty::All) || m_meta_data.getFlag(NativeProperty::Fields) ||
m_meta_data.getFlag(NativeProperty::WhiteListFields);
}

bool Class::shouldCompileMethods(void) const{

return m_meta_data.getFlag(NativeProperty::All) || m_meta_data.getFlag(NativeProperty::Methods) ||
m_meta_data.getFlag(NativeProperty::WhiteListMethods);
}

std::string Class::getClassName(void) { return m_name; }

bool Class::isAccessible(void) const { return m_enabled; }
6 changes: 5 additions & 1 deletion engine/source/meta_parser/parser/language_types/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "type_info.h"

#include "field.h"
#include "method.h"

struct BaseClass
{
Expand All @@ -15,14 +16,16 @@ class Class : public TypeInfo
{
// to access m_qualifiedName
friend class Field;
friend class Method;
friend class MetaParser;

public:
Class(const Cursor& cursor, const Namespace& current_namespace);

virtual bool shouldCompile(void) const;

bool shouldCompileFilds(void) const;
bool shouldCompileFields(void) const;
bool shouldCompileMethods(void) const;

template<typename T>
using SharedPtrVector = std::vector<std::shared_ptr<T>>;
Expand All @@ -37,6 +40,7 @@ class Class : public TypeInfo
std::string m_qualified_name;

SharedPtrVector<Field> m_fields;
SharedPtrVector<Method> m_methods;

std::string m_display_name;

Expand Down
19 changes: 19 additions & 0 deletions engine/source/meta_parser/parser/language_types/method.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "common/precompiled.h"

#include "class.h"
#include "method.h"

Method::Method(const Cursor& cursor, const Namespace& current_namespace, Class* parent) :
TypeInfo(cursor, current_namespace), m_parent(parent), m_name(cursor.getSpelling())
{}

bool Method::shouldCompile(void) const { return isAccessible(); }

bool Method::isAccessible(void) const
{
return ((m_parent->m_meta_data.getFlag(NativeProperty::Methods) ||
m_parent->m_meta_data.getFlag(NativeProperty::All)) &&
!m_meta_data.getFlag(NativeProperty::Disable)) ||
(m_parent->m_meta_data.getFlag(NativeProperty::WhiteListMethods) &&
m_meta_data.getFlag(NativeProperty::Enable));
}
24 changes: 24 additions & 0 deletions engine/source/meta_parser/parser/language_types/method.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "type_info.h"

class Class;

class Method : public TypeInfo
{

public:
Method(const Cursor& cursor, const Namespace& current_namespace, Class* parent = nullptr);

virtual ~Method(void) {}

bool shouldCompile(void) const;

public:

Class* m_parent;

std::string m_name;

bool isAccessible(void) const;
};
2 changes: 2 additions & 0 deletions engine/source/meta_parser/parser/meta/meta_data_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace NativeProperty
const auto All = "All";

const auto Fields = "Fields";
const auto Methods = "Methods";

const auto Enable = "Enable";
const auto Disable = "Disable";

const auto WhiteListFields = "WhiteListFields";
const auto WhiteListMethods = "WhiteListMethods";

} // namespace NativeProperty
80 changes: 75 additions & 5 deletions engine/source/runtime/core/meta/reflection/reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ namespace Piccolo
const char* k_unknown_type = "UnknownType";
const char* k_unknown = "Unknown";

static std::map<std::string, ClassFunctionTuple*> m_class_map;
static std::multimap<std::string, FieldFunctionTuple*> m_field_map;
static std::map<std::string, ArrayFunctionTuple*> m_array_map;
static std::map<std::string, ClassFunctionTuple*> m_class_map;
static std::multimap<std::string, FieldFunctionTuple*> m_field_map;
static std::multimap<std::string, MethodFunctionTuple*> m_method_map;
static std::map<std::string, ArrayFunctionTuple*> m_array_map;

void TypeMetaRegisterinterface::registerToFieldMap(const char* name, FieldFunctionTuple* value)
{
m_field_map.insert(std::make_pair(name, value));
}

void TypeMetaRegisterinterface::registerToMethodMap(const char* name, MethodFunctionTuple* value)
{
m_method_map.insert(std::make_pair(name, value));
}
void TypeMetaRegisterinterface::registerToArrayMap(const char* name, ArrayFunctionTuple* value)
{
if (m_array_map.find(name) == m_array_map.end())
Expand Down Expand Up @@ -65,6 +69,7 @@ namespace Piccolo
{
m_is_valid = false;
m_fields.clear();
m_methods.clear();

auto fileds_iter = m_field_map.equal_range(type_name);
while (fileds_iter.first != fileds_iter.second)
Expand All @@ -75,9 +80,19 @@ namespace Piccolo

++fileds_iter.first;
}

auto methods_iter = m_method_map.equal_range(type_name);
while (methods_iter.first != methods_iter.second)
{
MethodAccessor f_method(methods_iter.first->second);
m_methods.emplace_back(f_method);
m_is_valid = true;

++methods_iter.first;
}
}

TypeMeta::TypeMeta() : m_type_name(k_unknown_type), m_is_valid(false) { m_fields.clear(); }
TypeMeta::TypeMeta() : m_type_name(k_unknown_type), m_is_valid(false) { m_fields.clear(); m_methods.clear(); }

TypeMeta TypeMeta::newMetaFromName(std::string type_name)
{
Expand Down Expand Up @@ -134,6 +149,17 @@ namespace Piccolo
return count;
}

int TypeMeta::getMethodsList(MethodAccessor*& out_list)
{
int count = m_methods.size();
out_list = new MethodAccessor[count];
for (int i = 0; i < count; ++i)
{
out_list[i] = m_methods[i];
}
return count;
}

int TypeMeta::getBaseClassReflectionInstanceList(ReflectionInstance*& out_list, void* instance)
{
auto iter = m_class_map.find(m_type_name);
Expand All @@ -156,6 +182,16 @@ namespace Piccolo
return FieldAccessor(nullptr);
}

MethodAccessor TypeMeta::getMethodByName(const char* name)
{
const auto it = std::find_if(m_methods.begin(), m_methods.end(), [&](const auto& i) {
return std::strcmp(i.getMethodName(), name) == 0;
});
if (it != m_methods.end())
return *it;
return MethodAccessor(nullptr);
}

TypeMeta& TypeMeta::operator=(const TypeMeta& dest)
{
if (this == &dest)
Expand All @@ -165,6 +201,10 @@ namespace Piccolo
m_fields.clear();
m_fields = dest.m_fields;


m_methods.clear();
m_methods = dest.m_methods;

m_type_name = dest.m_type_name;
m_is_valid = dest.m_is_valid;

Expand Down Expand Up @@ -237,6 +277,36 @@ namespace Piccolo
return *this;
}

MethodAccessor::MethodAccessor()
{
m_method_name = k_unknown;
m_functions = nullptr;
}

MethodAccessor::MethodAccessor(MethodFunctionTuple* functions) : m_functions(functions)
{
m_method_name = k_unknown;
if (m_functions == nullptr)
{
return;
}

m_method_name = (std::get<0>(*m_functions))();
}
const char* MethodAccessor::getMethodName() const{
return (std::get<0>(*m_functions))();
}
MethodAccessor& MethodAccessor::operator=(const MethodAccessor& dest)
{
if (this == &dest)
{
return *this;
}
m_functions = dest.m_functions;
m_method_name = dest.m_method_name;
return *this;
}
void MethodAccessor::invoke(void* instance) { (std::get<1>(*m_functions))(instance); }
ArrayAccessor::ArrayAccessor() :
m_func(nullptr), m_array_type_name("UnKnownType"), m_element_type_name("UnKnownType")
{}
Expand Down
Loading

0 comments on commit 65379de

Please sign in to comment.