zengine-old/engine/3rdparty/zlib/include/refl/detail/uclass.inl

173 lines
4.6 KiB
Plaintext
Raw Normal View History

2024-04-08 21:01:50 +08:00
#pragma once
2024-04-11 10:13:15 +08:00
#include <array>
2024-04-08 21:01:50 +08:00
#include "uclass.h"
namespace refl {
template<_ReflCheck_Ctor T>
class UClass_Auto : public UClass {
public:
using UClass::UClass;
2024-04-09 16:31:09 +08:00
using MyUClass = UClass_Auto<T>;
2024-04-08 21:01:50 +08:00
public:
2024-04-13 17:47:44 +08:00
consteval static MyUClass BuildClass() {
2024-04-09 16:31:09 +08:00
MyUClass cls(type_name<T>().View(), sizeof(T));
2024-04-08 21:01:50 +08:00
if constexpr (!std::is_trivially_copyable_v<T>) {
2024-04-09 16:31:09 +08:00
cls.vtable.InitObject = &MyUClass::InitObject<T>;
cls.vtable.CopyObject = &MyUClass::CopyObject<T>;
2024-04-08 21:01:50 +08:00
}
return cls;
}
2024-04-13 17:47:44 +08:00
void Set(void* ptr,const T& t) {
*(T*)ptr = t;
}
T& Get(void* ptr) {
return *(T*)ptr;
}
2024-04-08 21:01:50 +08:00
};
/*
* 模板优化
* 成员参数转化为const void*
* 引用参数转化为指针
2024-04-09 22:26:33 +08:00
* 返回引用转化为指针
2024-04-08 21:01:50 +08:00
*/
template<typename R, typename... Args>
class UMethod_Auto : public UClass {
using UClass::UClass;
using MethodType = R(*)(Args...);
2024-04-09 16:31:09 +08:00
using MyUClass = UMethod_Auto<R, Args...>;
2024-04-08 21:01:50 +08:00
public:
std::array<const UClass*, sizeof...(Args) + 1> UList{};
static std::vector<const UClass*> GetParams(const UClass* cls) {
2024-04-09 16:31:09 +08:00
auto& UList = static_cast<const MyUClass*>(cls)->UList;
2024-04-08 21:01:50 +08:00
return { UList.data(), UList.data() + UList.size() };
}
2024-04-13 17:47:44 +08:00
static R Call(const FieldPtr* field, Args... args) {
MethodType fptr = (MethodType)field->data.method;
if constexpr (std::is_same_v<R, void>) {
fptr(args...);
}
else {
return fptr(args...);
}
}
2024-04-11 10:13:15 +08:00
static void Call(const FieldPtr* field, const std::vector<ArgsView>& ArgsList) {
2024-04-08 21:01:50 +08:00
if constexpr (std::is_same_v<R, void>) {
2024-04-11 10:13:15 +08:00
MethodType fptr = (MethodType)field->data.method;
2024-04-08 21:01:50 +08:00
auto param = ArgsList.rbegin();
fptr(param++->cast_to<Args>()...);
}
else {
2024-04-11 10:13:15 +08:00
MethodType fptr = (MethodType)field->data.method;
2024-04-08 21:01:50 +08:00
auto param = ArgsList.rbegin();
auto ret = ArgsList.begin();
2024-04-09 22:26:33 +08:00
if (ret->cls == &TypeInfo<R*>::StaticClass) {
2024-04-08 21:01:50 +08:00
*(R*)ret->val = fptr(param++->cast_to<Args>()...);
}
else {
fptr(param++->cast_to<Args>()...);
}
}
}
protected:
2024-04-13 17:47:44 +08:00
consteval void BuildUList() {
2024-04-08 21:01:50 +08:00
if constexpr (!std::is_same_v<R, void>) {
2024-04-09 22:26:33 +08:00
UList[0] = &TypeInfo<R*>::StaticClass;
2024-04-08 21:01:50 +08:00
}
if constexpr (sizeof...(Args) > 0) {
auto ptr = &UList[1];
2024-04-09 22:26:33 +08:00
(..., (*ptr = &TypeInfo<std::remove_pointer_t<Args>*>::StaticClass, ptr++));
2024-04-08 21:01:50 +08:00
}
}
public:
//为了简化判断cls 对象 统统指向 T*
2024-04-13 17:47:44 +08:00
consteval static MyUClass BuildClass() {
2024-04-09 16:31:09 +08:00
MyUClass cls(type_name<MethodType>().View(), sizeof(MethodType));
cls.vtable.GetParams = &MyUClass::GetParams;
cls.vtable.Call = &MyUClass::Call;
2024-04-08 21:01:50 +08:00
cls.BuildUList();
return cls;
}
};
2024-04-13 17:47:44 +08:00
/*
2024-04-08 21:01:50 +08:00
class UClass_Custom : public UClass {
public:
using UClass::UClass;
std::vector<FieldPtr> FList{};
int32_t AttrNum{ 0 };
static const FieldPtr* GetField(const UClass* _cls,const Name& name, bool isMethod){
auto cls = static_cast<const UClass_Custom*>(_cls);
auto& FList = cls->FList;
// 指定开始位置的迭代器
auto start = FList.begin();
if (isMethod) {
start += cls->AttrNum;
}
auto end = FList.end();
for (auto it = start; it != end; ++it) {
if (it->name == name) {
return &*it;
}
}
return nullptr;
}
2024-04-13 17:47:44 +08:00
2024-04-08 21:01:50 +08:00
void BuildClass() {
2024-04-09 16:31:09 +08:00
FList.shrink_to_fit();//缩减容量FList大小不再改变了
2024-04-08 21:01:50 +08:00
vtable.GetField = &UClass_Custom::GetField;
}
2024-04-13 17:47:44 +08:00
};*/
2024-04-14 22:45:08 +08:00
2024-04-13 17:47:44 +08:00
template<typename T, typename P = void>
class UClass_Meta : public UClass {
public:
2024-04-14 22:45:08 +08:00
using FieldsType = decltype(T::__MakeFields());
FieldsType Fields{ T::__MakeFields() };
UClass_Meta() : UClass(type_name<T>().View(), sizeof(T)){
2024-04-13 17:47:44 +08:00
if constexpr (!std::is_same_v<P, void>) {
parent = &TypeInfo<P>::StaticClass;
}
2024-04-14 22:45:08 +08:00
if constexpr (has_init_object<T>::value) {
vtable.InitObject = &T::__InitObject;
}
else {
vtable.InitObject = &UClass::InitObject<T>;
}
2024-04-13 17:47:44 +08:00
}
2024-04-14 22:45:08 +08:00
const FieldPtr* GetField(int index) const {
return &Fields[index];
}
const FieldPtr* GetField(const Name& name, int index = 0) const{
constexpr int length = std::tuple_size<FieldsType>::value;
auto ptr = index < length ? &Fields[index] : nullptr;
for (int i = index; i < length; i++, ptr++) {
if (name == ptr->name) {
return ptr;
}
}
return nullptr;
}
2024-04-08 21:01:50 +08:00
};
2024-04-13 17:47:44 +08:00
2024-04-08 21:01:50 +08:00
//基础类型的偏特化
template<_ReflCheck_Ctor_NoUClass T>
struct TypeInfoImpl<T> {
using UClass = UClass_Auto<T>;
inline constexpr static UClass StaticClass = UClass::BuildClass();
};
// 函数指针类型的偏特化
template<typename R, typename... Args>
struct TypeInfoImpl<R(*)(Args...)> {
using UClass = UMethod_Auto<R, Args...>;
inline constexpr static UClass StaticClass = UClass::BuildClass();
};
2024-04-09 16:31:09 +08:00
template<_ReflCheck_UClass T>
struct TypeInfoImpl<T> {
using MyUClass = typename T::MyUClass;
2024-04-14 22:45:08 +08:00
inline static MyUClass StaticClass = MyUClass();
2024-04-09 16:31:09 +08:00
};
2024-04-08 21:01:50 +08:00
}