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

134 lines
3.2 KiB
C
Raw Normal View History

2024-04-08 21:01:50 +08:00
#pragma once
#include "field.h"
#include "view.h"
2024-04-19 22:02:27 +08:00
#include "meta.h"
2024-06-12 22:07:45 +08:00
#include "convert.h"
2024-04-08 21:01:50 +08:00
namespace refl {
2024-04-15 19:53:39 +08:00
enum ClassFlag :uint32_t {
CLASS_NONE_FLAG = 0,
CLASS_TRIVIAL_FLAG = 1 << 0,
2024-04-25 21:45:41 +08:00
CLASS_POINTER_FLAG = 1 << 1,
CLASS_ARRAY_FLAG = 1 << 2,
2024-04-15 19:53:39 +08:00
};
2024-04-16 16:17:13 +08:00
using enum ClassFlag;
2024-06-12 22:07:45 +08:00
enum EFieldFind :uint32_t {
FIND_ALL_FIELD = 0,
FIND_ALL_MEMBER,
FIND_ALL_METHOD,
FIND_FIELD,
FIND_CTOR,
FIND_MEMBER,
FIND_METHOD,
FIND_METHODS,//函数重载 特别是构造函数
};
2024-04-08 21:01:50 +08:00
struct vtable_uclass
{
//class
2024-06-12 22:07:45 +08:00
const sarray<const FieldPtr> (*GetFields)(const UClass*, EFieldFind find, const Name& name);
2024-04-08 21:01:50 +08:00
//function
2024-04-16 16:17:13 +08:00
sarray<const UClass*>(*GetParams)(const UClass*);
2024-04-08 21:01:50 +08:00
//function
2024-04-19 22:02:27 +08:00
void (*Call)(const FieldPtr*, const sarray<Any>& ArgsList);
2024-04-28 00:23:35 +08:00
//meta
const UClass* (*GetMeta)(const Name&);
2024-04-09 16:31:09 +08:00
//object
2024-06-12 22:07:45 +08:00
bool (*CtorObject)(void* ptr,const UClass* cls, const sarray<Any>& ArgsList);
bool (*DestObject)(void*);
2024-04-08 21:01:50 +08:00
};
class UClass{
public:
Name name;
uint32_t size;
2024-04-15 19:53:39 +08:00
uint32_t flag{0};
2024-04-08 21:01:50 +08:00
const UClass* parent;
vtable_uclass vtable{};
public:
constexpr UClass(std::string_view name, uint32_t size, const UClass* parent = nullptr)
:name(name), size(size), parent(parent){}
2024-06-12 22:07:45 +08:00
AnyView New(void* ptr = nullptr, const sarray<Any>& ArgsList = {}) const {
2024-04-08 21:01:50 +08:00
if (ptr == nullptr) {
ptr = malloc(size);
2024-06-12 22:07:45 +08:00
CtorObject(ptr, ArgsList);
2024-04-08 21:01:50 +08:00
}
return { ptr , this };
}
template<typename T>
2024-06-12 22:07:45 +08:00
T* New(T* ptr = nullptr, const sarray<Any>& ArgsList = {}) const{
2024-04-08 21:01:50 +08:00
if (!IsChildOf<T>()) {
return nullptr;
}
if (ptr == nullptr) {
ptr = (T*)malloc(size);
}
2024-06-12 22:07:45 +08:00
CtorObject(ptr, ArgsList);
2024-04-08 21:01:50 +08:00
return ptr;
}
bool IsChildOf(const UClass* cls) const {
const UClass* _parent = parent;
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
template<typename T>
bool IsChildOf(bool bthis = false) const {
2024-04-25 21:45:41 +08:00
return IsChildOf(&TypeInfo<T>::StaticClass);
2024-04-08 21:01:50 +08:00
}
public:
2024-06-12 22:07:45 +08:00
const sarray<const FieldPtr> GetFields(EFieldFind find, const Name& name)const {
if (vtable.GetFields) {
return vtable.GetFields(this, find, name);
2024-04-08 21:01:50 +08:00
}
2024-06-12 22:07:45 +08:00
return {};
2024-04-08 21:01:50 +08:00
}
2024-06-12 22:07:45 +08:00
bool CtorObject(void* ptr, const sarray<Any>& ArgsList = {})const {
if (vtable.CtorObject) {
if (vtable.CtorObject(ptr, this, ArgsList)) {
return true;
}
auto& fieldList = GetFields(EFieldFind::FIND_CTOR, FName("Ctor"));
if (fieldList.empty()) {
return false;
}
if (fieldList.size() == 1) {
return fieldList[0]->Invoke(ArgsList);
}
for (auto& field : fieldList) {
if (field.Invokes(ArgsList)) {
return true;
}
}
return false;
2024-04-09 16:31:09 +08:00
}
2024-06-12 22:07:45 +08:00
memset(ptr, 0, size);
return true;
2024-04-08 21:01:50 +08:00
}
2024-04-09 16:31:09 +08:00
void DestObject(void* ptr)const {
if (vtable.DestObject) {
vtable.DestObject(ptr);
}
2024-04-08 21:01:50 +08:00
}
2024-04-09 16:31:09 +08:00
public:
template<typename T>
2024-06-12 22:07:45 +08:00
static bool CtorObject(void* ptr, const UClass* cls, const sarray<Any>& ArgsList = {}) {
int size = ArgsList.size();
if (size == 0) {
if constexpr (std::is_trivially_constructible_v<T>) {
std::construct_at((T*)ptr);
return true;
}
else {
throw "refl::error:: no default construct function!!!";
}
2024-04-25 21:45:41 +08:00
}
2024-06-12 22:07:45 +08:00
if (size == 1 && ArgsList[0]->Check(cls)) {
*(T*)ptr = *(const T*)ArgsList[0]->ptr;
return true;
2024-04-25 21:45:41 +08:00
}
2024-06-12 22:07:45 +08:00
return false;
2024-04-25 21:45:41 +08:00
}
2024-04-08 21:01:50 +08:00
};
}