update refl member field

This commit is contained in:
ouczbs 2024-04-04 18:18:04 +08:00
parent 6e9c97f36e
commit a8113a70b1
7 changed files with 219 additions and 37 deletions

View File

@ -0,0 +1,22 @@
#include <string>
namespace refl {
class UClass;
class FieldPtr;
class ObjectView {
public:
const char* ptr;
const UClass* cls;
const FieldPtr* cache{nullptr};
ObjectView(void* ptr, const UClass* cls) : ptr((const char*)ptr), cls(cls){}
public:
template<typename T>
bool Get(const Name& name, T& t);
template<typename T>
bool Set(const Name& name, const T& t);
template<typename ...Args>
bool Invoke(const Name& name, Args... args);
};
}

View File

@ -1,13 +1,19 @@
#pragma once
#include <type_traits>
namespace refl {
template<typename T, typename = void>
template <class T>
concept _CheckDefaultConstruct = requires { T(); };
template <class T>
concept _CheckBuildUClass = requires{{T::UClass::BuildClass()} -> std::same_as<typename T::UClass>; };
template <class T>
concept _ReflCheck = _CheckDefaultConstruct<T> || _CheckBuildUClass<T>;
template<_ReflCheck T, typename = void>
struct TypeInfo;
template<typename T>
template<_ReflCheck T>
struct TypeInfo<T, std::void_t<typename T::UClass, decltype(T::UClass::BuildClass())>> {
using UClass = typename T::UClass;
//这里的特性怎么走不到呢
inline static UClass StaticClass = UClass::BuildClass();
};
}

View File

@ -1,20 +1,55 @@
#pragma once
#include "type.h"
#include "field.h"
#include "object.h"
namespace refl {
class UClass{
public:
Name name;
uint32_t size;
UClass* parent;
public:
void Var() {
constexpr UClass(Name name, uint32_t size, UClass* parent = nullptr)
:name(name),size(size), parent(parent){}
ObjectView New(void* ptr = nullptr) const{
if (ptr == nullptr) {
ptr = malloc(size);
InitObject(ptr);
}
return { ptr , this };
}
public:
template<typename T>
constexpr static UClass BuildClass() {
return {type_name<T>().View(), sizeof(T)};
T* New(T* ptr = nullptr) const{
if (!IsChildOf<T>()) {
return nullptr;
}
if (ptr == nullptr) {
ptr = (T*)malloc(size);
}
InitObject(ptr);
return ptr;
}
template<typename T>
bool IsChildOf(bool bthis = false) const {
constexpr UClass* cls = &TypeInfo<T>::StaticClass;
if (bthis) {
return cls == this;
}
const UClass* _parent = this;
while (_parent != nullptr) {
if (_parent == cls) {
return true;
}
_parent = _parent->parent;
}
return false;
}
//unsafe if called by other, need check class && size
virtual void InitObject(void* ptr)const {
memset(ptr, 0, size);
}
virtual const FieldPtr* GetField(const Name& name)const {
return nullptr;
}
protected:
template<uint32_t offset, typename T,typename U>
@ -22,11 +57,77 @@ namespace refl {
FieldPtr::Data member = { offset };
return { name, &TypeInfo<T>::StaticClass, {member} };
}
};
template<typename T, typename>
struct TypeInfo {
using UClass = UClass;
//这里的语法我也看不懂呀,防警告的,显示声明为模板调用
inline constexpr static UClass StaticClass = UClass::template BuildClass<T>();
};
template<uint32_t offset, typename T, typename U>
FieldPtr MakeMethodField(T U::* ptr, Name name) {
FieldPtr::Data member = { offset };
return { name, &TypeInfo<T>::StaticClass, {member} };
}
};
template<typename T>
requires std::is_default_constructible_v<T>
class UClass_Auto : public UClass{
public:
using UClass::UClass;
protected:
void InitObject(void* ptr)const override {
constexpr bool is_shallow_copyable = std::is_trivially_copyable<T>::value;
if constexpr (!is_shallow_copyable){
constexpr T obj{};
std::construct_at((T*)ptr, obj);
}
else {
memset(ptr, 0, size);
}
}
public:
static UClass_Auto<T> BuildClass() {
auto cls = UClass_Auto<T>(type_name<T>().View(), sizeof(T));
return cls;
}
};
template<_ReflCheck T, typename>
struct TypeInfo {
using UClass = UClass_Auto<T>;
inline static UClass StaticClass = UClass::BuildClass();
};
template<typename T>
inline bool ObjectView::Get(const Name& name, T& t)
{
if (cache && cache->name == name) {
_cache_get: bool isChild = cache->type->IsChildOf<T>(true);
if (isChild) {
t = *(T*)(ptr + std::get<uint32_t>(cache->data));
}
return isChild;
}
auto field = cls->GetField(name);
if (field) {
cache = field;
goto _cache_get;
}
return false;
}
template<typename T>
inline bool ObjectView::Set(const Name& name, const T& t)
{
if (cache && cache->name == name) {
_cache_set: bool isChild = cache->type->IsChildOf<T>(true);
if (isChild) {
*(T*)(ptr + std::get<uint32_t>(cache->data)) = t;
}
return isChild;
}
auto field = cls->GetField(name);
if (field) {
cache = field;
goto _cache_set;
}
return false;
}
template<typename ...Args>
bool ObjectView::Invoke(const Name& name, Args... args)
{
return false;
}
}

View File

@ -0,0 +1,23 @@
#include <string>
#include <iomanip>
using std::string;
template<typename T>
void PrintHex(T& obj) {
auto size = sizeof(T);
std::cout << type_name<T>().View() << "<" << size <<"> " << " = 0x";
char* ptr = (char*)& obj;
for (int i = 0; i < size; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') <<(int)*(ptr + i);
}
std::cout << "FF" << std::endl;
}
void TestString1() {
string hello = "abcdef";
int size = sizeof(string);
PrintHex(hello);
hello = "abcdefabcdefabcdefabcdef";
PrintHex(hello);
//hello.~basic_string();
PrintHex(size);
}

View File

@ -2,39 +2,69 @@
#include <array>
#include "refl/uclass.h"
#include "vertex.h"
#include "tstring.h"
using namespace std;
using namespace refl;
struct vec3 {
struct vec3_parent {
};
struct vec3 : public vec3_parent {
using UClass = class vec3_UClass;
float x;
float y;
float z;
float x = 1;
float y = 2;
float z = 3;
string name{ "hellohellohellohellohellohello" };
float norm() {
return x * y * z;
}
};
struct vec3_UClass : public UClass {
array<FieldPtr,3> FList;
constexpr static vec3_UClass BuildClass() {
vec3_UClass cls{};
cls.name = type_name<vec3>().View();
cls.size = sizeof(vec3);
array<FieldPtr,4> FList;
using UClass::UClass;
const FieldPtr* GetField(const Name& name)const override {
for (auto& field : FList) {
if (field.name == name) {
return &field;
}
}
return nullptr;
}
void InitObject(void* ptr)const override {
vec3 obj{};
std::construct_at((vec3*)ptr, obj);
}
static vec3_UClass BuildClass() {
vec3_UClass cls(type_name<vec3>().View(), sizeof(vec3), &TypeInfo<vec3_parent>::StaticClass);
//offsetof(vec3, x) 很坑,相当坑
cls.FList[0] = cls.MakeMemberField<offsetof(vec3, x)>(&vec3::x, "x");
cls.FList[1] = cls.MakeMemberField<offsetof(vec3, y)>(&vec3::y, "y");
cls.FList[2] = cls.MakeMemberField<offsetof(vec3, z)>(&vec3::z, "z");
cls.FList[3] = cls.MakeMemberField<offsetof(vec3, name)>(&vec3::name, "name");
return cls;
}
};
int main() {
using uclass = TypeInfo<vec3>::UClass;
auto& res = TypeInfo<int>::StaticClass;
auto& res2 = TypeInfo<float>::StaticClass;
auto& res2 = TypeInfo<Name>::StaticClass;
auto& res3 = TypeInfo<vec3>::StaticClass;
vec3 v1;
auto res4 = TypeInfo<size_t>::StaticClass;
auto res5 = TypeInfo<string_view>::StaticClass;
auto t1 = res3.FList[0];
//using t2 = t1::UClass;
std::cout << sizeof(Name) << "Type2: " << typeid(vec3).name() << std::endl;
vec3_parent* v2 = res3.New<vec3_parent>();
vec3* v22 = (vec3*)v2;
auto v3 = res3.New();
int x1 = 110;
float x = *res.New(&x1);
v3.Get("x", x);
v3.Set("z", (float)33);
v2 = (vec3*)v3.ptr;
using T = int;
auto b1 = res3.IsChildOf<vec3_parent>();
constexpr auto cls = UClass_Auto<int>(type_name<T>().View(), sizeof(T));
cout << sizeof(Name) << "Type2: " << typeid(vec3).name() << endl;
delete v2;
cout << "hello world\n";
}

View File

@ -16,7 +16,7 @@ namespace vulkanapi {
VulkanContext(RenderVulkanAPI* _API){
API = _API;
};
virtual void UseContext()override;
void UseContext()override;
public:
static VkDescriptorSetLayout CreateDescriptorSetLayout(const ShaderInfo& info);

View File

@ -20,24 +20,24 @@ namespace vulkanapi
vector<VulkanPipeline*> PiPelineList;
public:
RenderVulkanAPI();
virtual ~RenderVulkanAPI()override;
~RenderVulkanAPI()override;
public:
virtual RenderContext* GetContext() override {
RenderContext* GetContext() override {
return &context;
};
public:
virtual void SwitchContext()override;
void SwitchContext()override;
public:
virtual void InitRenderPass()override;
void InitRenderPass()override;
public:
virtual void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0)override;
void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0)override;
virtual void BeginFrame()override;
virtual void EndFrame()override;
virtual void SetStaticMesh(Mesh* mesh)override;
virtual void DrawStaticMesh(Mesh* mesh)override;
void BeginFrame()override;
void EndFrame()override;
void SetStaticMesh(Mesh* mesh)override;
void DrawStaticMesh(Mesh* mesh)override;
virtual void LoadShader(Shader* shader)override;
void LoadShader(Shader* shader)override;
public:
VulkanVAO* GetNextVAO(uint32_t& index);
VulkanPipeline* GetNextPipeline(uint32_t& index);