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

116 lines
2.9 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 {
2024-06-24 00:26:52 +08:00
CLASS_NONE_FLAG = 0,
CLASS_TRIVIAL_FLAG = 1 << 0,
CLASS_POINTER_FLAG = 1 << 1,
CLASS_ARRAY_FLAG = 1 << 2,
CLASS_CONTAINER_FLAG = 1 << 3,
CLASS_SEQUENCE_FLAG = 1 << 4,
CLASS_MAP_FLAG = 1 << 5,
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-24 00:26:52 +08:00
bool (*Construct)(void* ptr,const UClass* cls, const sarray<Any>& ArgsList);
void (*Destruct)(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{};
2024-06-30 21:46:26 +08:00
inline static std::unordered_map<Name, const UClass*> MetaTable;
2024-04-08 21:01:50 +08:00
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-24 00:26:52 +08:00
Construct(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-06-14 22:24:52 +08:00
if (!IsChildOf<T>(true)) {
2024-04-08 21:01:50 +08:00
return nullptr;
}
if (ptr == nullptr) {
ptr = (T*)malloc(size);
}
2024-06-14 22:24:52 +08:00
2024-06-24 00:26:52 +08:00
Construct(ptr, ArgsList);
2024-04-08 21:01:50 +08:00
return ptr;
}
2024-06-14 22:24:52 +08:00
bool IsChildOf(const UClass* cls, bool bthis = false) const {
const UClass* _parent = bthis ? this : parent;
2024-04-08 21:01:50 +08:00
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
template<typename T>
bool IsChildOf(bool bthis = false) const {
2024-06-14 22:24:52 +08:00
return IsChildOf(&TypeInfo<T>::StaticClass, bthis);
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-24 00:26:52 +08:00
bool Construct(void* ptr, const sarray<Any>& ArgsList = {})const;
void Destruct(void* ptr)const {
if (vtable.Destruct) {
vtable.Destruct(ptr);
2024-04-09 16:31:09 +08:00
}
2024-04-08 21:01:50 +08:00
}
2024-04-09 16:31:09 +08:00
public:
template<typename T>
2024-06-24 00:26:52 +08:00
static bool Construct(void* ptr, const UClass* cls, const sarray<Any>& ArgsList = {}) {
2024-06-14 22:24:52 +08:00
int argsSize = ArgsList.size();
if (argsSize == 0) {
2024-06-12 22:07:45 +08:00
if constexpr (std::is_trivially_constructible_v<T>) {
std::construct_at((T*)ptr);
return true;
}
2024-06-14 22:24:52 +08:00
return false;
2024-04-25 21:45:41 +08:00
}
2024-06-14 22:24:52 +08:00
if (argsSize == 1 && ArgsList[0]->Check(cls)) {
2024-06-12 22:07:45 +08:00
*(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
};
}