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

89 lines
2.4 KiB
C
Raw Normal View History

2024-04-07 21:15:34 +08:00
#pragma once
2024-04-06 23:50:17 +08:00
#include <string>
2024-04-07 21:15:34 +08:00
#include "type.h"
2024-04-14 22:45:08 +08:00
#include "../std/sarray.h"
2024-04-06 23:50:17 +08:00
namespace refl {
class UClass;
class FieldPtr;
2024-04-09 16:31:09 +08:00
//生命周期短,适用于传参,不建议保存数据
2024-04-09 22:26:33 +08:00
//只能指向指针,引用=>指针,指针=>指针T => T*,T的类型丢失
2024-04-09 16:31:09 +08:00
struct ArgsView {
2024-04-06 23:50:17 +08:00
public:
const void* val;
const UClass* cls;
2024-04-14 22:45:08 +08:00
constexpr ArgsView() : val(nullptr), cls(nullptr) {}
2024-04-09 16:31:09 +08:00
//右值=>右值压入栈caller入栈地址
//左值=>caller变量地址
2024-04-07 21:15:34 +08:00
template<typename T>
2024-04-14 22:45:08 +08:00
constexpr ArgsView(T&& v): val(&v), cls(&TypeInfo<args_type_t<T>*>::StaticClass){
2024-04-15 19:53:39 +08:00
if constexpr (std::is_same_v<args_type_t<T>, ArgsView>) {
2024-04-09 16:31:09 +08:00
val = v.val;
cls = v.cls;
}
}
2024-04-15 19:53:39 +08:00
template<typename T>
constexpr ArgsView(T* v) : val(v), cls(&TypeInfo<args_type_t<T>*>::StaticClass) {
}
2024-04-09 22:26:33 +08:00
template<typename T>//参数 T* => T*
2024-04-11 10:13:15 +08:00
constexpr inline T cast_to() const {
2024-04-07 21:15:34 +08:00
if constexpr (std::is_pointer_v<T>) {
return (T)val;
}
else if constexpr (std::is_reference_v<T>){
using RT = std::remove_reference_t<T>;
return *(RT*)val;
}else{
return *(T*)val;
}
}
2024-04-09 22:26:33 +08:00
template<typename T>
2024-04-11 10:13:15 +08:00
T cast_ret() const {
2024-04-09 22:26:33 +08:00
if constexpr (std::is_pointer_v<T>) {
return *(T*)val;
}
else if constexpr (std::is_reference_v<T>) {
using RT = std::remove_reference_t<T>;
return **(RT**)val;
}
else {
return *(T*)val;
}
}
2024-04-07 21:15:34 +08:00
bool ConvertTo(const UClass* toClass);
2024-04-06 23:50:17 +08:00
};
2024-04-09 16:31:09 +08:00
//存入的是真实的数据类型,type用于拷贝数据
struct ArgsValue : public ArgsView {
const UClass* type;
template<typename T>
2024-04-14 22:45:08 +08:00
constexpr ArgsValue(T&& t) : ArgsView(t), type(&TypeInfo<args_type_t<T>>::StaticClass){}
2024-04-09 16:31:09 +08:00
};
struct ArgsValueList{
2024-04-14 22:45:08 +08:00
void* data;
ArgsView* ptr;
2024-04-15 19:53:39 +08:00
int num;
2024-04-14 22:45:08 +08:00
bool isMemoryOwner{false};
ArgsValueList(const sarray<ArgsValue>& args,void* memory = nullptr);
2024-04-09 16:31:09 +08:00
~ArgsValueList();
2024-04-14 22:45:08 +08:00
constexpr static int GetArgsSize(const sarray<ArgsValue>& args);
2024-04-09 16:31:09 +08:00
};
2024-04-11 10:13:15 +08:00
class ObjectView {
public:
2024-04-15 19:53:39 +08:00
const void* ptr;
2024-04-11 10:13:15 +08:00
const UClass* cls;
const FieldPtr* cache{ nullptr };
2024-04-15 19:53:39 +08:00
ObjectView(const void* ptr, const UClass* cls) : ptr(ptr), cls(cls) {}
2024-04-11 10:13:15 +08:00
public:
template<typename T>
bool Get(const Name& name, T& t);
template<typename T>
bool Set(const Name& name, const T& t);
2024-04-15 19:53:39 +08:00
bool Invoke(const Name& name,const sarray<ArgsView>& ArgsList);//这里是内存分配慢呀,这里是可以改成栈容器的
2024-04-11 10:13:15 +08:00
bool Invoke(const Name& name,std::vector<ArgsView>& ArgsList);
ObjectView Parent();
};
2024-04-06 23:50:17 +08:00
}