refl try support vector, map
This commit is contained in:
parent
d1311b829d
commit
74ccd12aa4
57
engine/3rdparty/zlib/include/refl/detail/type.h
vendored
57
engine/3rdparty/zlib/include/refl/detail/type.h
vendored
@ -12,20 +12,46 @@ namespace refl_impl {
|
||||
|
||||
template<typename T, typename M>
|
||||
struct _Meta;
|
||||
|
||||
template<typename T>
|
||||
struct _ContainerMeta;
|
||||
}
|
||||
namespace refl {
|
||||
using refl_impl::MetaImpl;
|
||||
// 定义一个模板结构体用于检测是否为 数组
|
||||
template<typename T>
|
||||
struct is_array : std::false_type {};
|
||||
|
||||
template <class MyMeta>
|
||||
concept _ReflCheck_Parent = requires { typename MyMeta::Parent;} && std::is_same_v<typename MyMeta::Parent, void>;
|
||||
template<typename T, size_t N>
|
||||
struct is_array<T[N]> : std::true_type {
|
||||
using type = T;
|
||||
consteval static size_t Size() {
|
||||
return N;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
concept _ReflCheck_Ctor = requires { T(); };
|
||||
template <class T>
|
||||
concept _ReflCheck_UClass = requires { typename MetaImpl<T>::MyMeta; };
|
||||
template <class T>
|
||||
concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor<T> && !_ReflCheck_UClass<T>;
|
||||
// 定义一个模板结构体用于检测是否为 std::pair
|
||||
template<typename T>
|
||||
struct is_pair : std::false_type {};
|
||||
|
||||
template<typename T1, typename T2>
|
||||
struct is_pair<std::pair<T1, T2>> : std::true_type {};
|
||||
|
||||
template<typename T>
|
||||
concept is_pair_v = is_pair<T>::value;
|
||||
|
||||
template<typename T>
|
||||
concept is_container_v = requires(T a) {
|
||||
{ a.begin() } -> std::input_iterator;
|
||||
{ a.end() } -> std::input_iterator;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept is_map_v = is_pair_v<typename T::value_type> && is_container_v<T>;
|
||||
|
||||
template<typename T>
|
||||
concept is_sequence_v = !is_pair_v<typename T::value_type> && is_container_v<T>;
|
||||
};
|
||||
namespace refl {
|
||||
template<typename T>
|
||||
struct real_type {
|
||||
using type = std::remove_cv_t<T>;
|
||||
@ -57,6 +83,19 @@ namespace refl {
|
||||
};
|
||||
template<typename T>
|
||||
using args_type_t = args_type<std::remove_cv_t<T>>::type;
|
||||
};
|
||||
namespace refl {
|
||||
using refl_impl::MetaImpl;
|
||||
|
||||
template <class MyMeta>
|
||||
concept _ReflCheck_Parent = requires { typename MyMeta::Parent;} && std::is_same_v<typename MyMeta::Parent, void>;
|
||||
|
||||
template <class T>
|
||||
concept _ReflCheck_Ctor = requires { T(); };
|
||||
template <class T>
|
||||
concept _ReflCheck_UClass = requires { typename MetaImpl<T>::MyMeta; };
|
||||
template <class T>
|
||||
concept _ReflCheck_Ctor_NoUClass = _ReflCheck_Ctor<T> && !_ReflCheck_UClass<T>;
|
||||
|
||||
//类型接口
|
||||
template<typename T>
|
||||
|
||||
@ -14,12 +14,20 @@ namespace refl {
|
||||
consteval static MyUClass BuildClass() {
|
||||
MyUClass cls(type_name<T>().View(), sizeof(T));
|
||||
if constexpr (std::is_pointer_v<T>){
|
||||
using RawT = std::remove_pointer_t<T>;
|
||||
using RT = std::remove_pointer_t<T>;
|
||||
cls.flag = CLASS_POINTER_FLAG;
|
||||
if constexpr (!std::is_same_v<RawT, void>) {
|
||||
cls.parent = &TypeInfo<RawT>::StaticClass;
|
||||
if constexpr (!std::is_same_v<RT, void>) {
|
||||
cls.parent = &TypeInfo<RT>::StaticClass;
|
||||
}
|
||||
}
|
||||
else if constexpr (is_array<T>::value) {
|
||||
using RT = typename is_array<T>::type;
|
||||
cls.flag = CLASS_ARRAY_FLAG;
|
||||
if constexpr (std::is_pointer_v<RT>) {
|
||||
cls.flag |= CLASS_POINTER_FLAG;
|
||||
}
|
||||
cls.parent = &TypeInfo<RT>::StaticClass;
|
||||
}
|
||||
else {
|
||||
cls.vtable.CtorObject = &MyUClass::CtorObject<T>;
|
||||
}
|
||||
@ -35,23 +43,6 @@ namespace refl {
|
||||
return *(T*)ptr;
|
||||
}
|
||||
};
|
||||
template<_ReflCheck_Ctor T, int N>
|
||||
class UClass_Array : public UClass {
|
||||
public:
|
||||
using UClass::UClass;
|
||||
using MyUClass = UClass_Array<T, N>;
|
||||
public:
|
||||
consteval static MyUClass BuildClass() {
|
||||
MyUClass cls(type_name<T[N]>().View(), sizeof(T) * N, &TypeInfo<T>::StaticClass);
|
||||
if constexpr (std::is_pointer_v<T>) {
|
||||
cls.flag = CLASS_POINTER_FLAG | CLASS_ARRAY_FLAG;
|
||||
}
|
||||
else {
|
||||
cls.flag = CLASS_ARRAY_FLAG;
|
||||
}
|
||||
return cls;
|
||||
}
|
||||
};
|
||||
/*
|
||||
* 模板优化
|
||||
* 成员参数转化为const void*
|
||||
@ -115,6 +106,17 @@ namespace refl {
|
||||
return cls;
|
||||
}
|
||||
};
|
||||
template<is_container_v T, typename value_type>
|
||||
class UClass_Container : public UClass {
|
||||
using MyUClass = UClass_Container<T, value_type>;
|
||||
using MyMeta = refl_impl::_ContainerMeta<T>;
|
||||
using FieldsType = decltype(MyMeta::__MakeFields());
|
||||
FieldsType Fields{ MyMeta::__MakeFields() };
|
||||
public:
|
||||
consteval static MyUClass BuildClass() {
|
||||
return MyMeta::__BuildClass();
|
||||
}
|
||||
};
|
||||
template<typename T, typename MyMeta>
|
||||
class UClass_Meta : public UClass {
|
||||
public:
|
||||
@ -211,9 +213,9 @@ namespace refl {
|
||||
using UClass = UMethod_Auto<R, Args...>;
|
||||
inline constexpr static UClass StaticClass = UClass::BuildClass();
|
||||
};
|
||||
template<typename T, int N>
|
||||
struct TypeInfoImpl<T[N]> {
|
||||
using UClass = UClass_Array<T,N>;
|
||||
template<is_container_v T>
|
||||
struct TypeInfoImpl<T> {
|
||||
using UClass = UClass_Container<T, typename T::value_type>;
|
||||
inline constexpr static UClass StaticClass = UClass::BuildClass();
|
||||
};
|
||||
//基础类型的偏特化
|
||||
|
||||
15
engine/3rdparty/zlib/include/refl/impl/container.inl
vendored
Normal file
15
engine/3rdparty/zlib/include/refl/impl/container.inl
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "refl/detail/uclass.h"
|
||||
namespace refl::impl {
|
||||
template<typename T>
|
||||
struct _ContainerMeta {
|
||||
auto __MakeFields() {
|
||||
return std::array{
|
||||
|
||||
};
|
||||
}
|
||||
auto __BuildClass() {
|
||||
return cls;
|
||||
}
|
||||
};
|
||||
}
|
||||
45
engine/3rdparty/zlib/test/yaml/guid.h
vendored
45
engine/3rdparty/zlib/test/yaml/guid.h
vendored
@ -4,11 +4,9 @@
|
||||
#include <vector>
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
using std::string_view;
|
||||
namespace test {
|
||||
struct Guid
|
||||
{
|
||||
struct Guid
|
||||
{
|
||||
using MyMeta = class Guid_Meta;
|
||||
UPROPERTY({})
|
||||
unsigned int Data1;
|
||||
UPROPERTY({})
|
||||
@ -20,9 +18,6 @@ namespace test {
|
||||
constexpr Guid() noexcept
|
||||
: Data1{ 0 }, Data2{ 0 }, Data3{ 0 }, Data4{ 0,0,0,0,0,0,0,0 }
|
||||
{}
|
||||
|
||||
USING_OVERLOAD_CTOR(Guid, const string_view&)
|
||||
UFUNCTION({}, ref = USING_CTOR_NAME)
|
||||
Guid(const std::string_view& str) noexcept
|
||||
: Guid{}
|
||||
{
|
||||
@ -53,17 +48,6 @@ namespace test {
|
||||
{
|
||||
return *reinterpret_cast<const GUID*>(this) != GUID_NULL;
|
||||
}
|
||||
|
||||
USING_OVERLOAD_CLASS_FUNC(void, Guid, int)
|
||||
UFUNCTION({}, ref = USING_FUNC_NAME)
|
||||
void test(int x) {
|
||||
|
||||
}
|
||||
USING_OVERLOAD_CLASS_FUNC(void, Guid, float)
|
||||
UFUNCTION({}, ref = USING_FUNC_NAME)
|
||||
void test(float x) {
|
||||
|
||||
}
|
||||
operator std::string() const
|
||||
{
|
||||
char guid_cstr[39];
|
||||
@ -74,14 +58,33 @@ namespace test {
|
||||
Data4[4], Data4[5], Data4[6], Data4[7]);
|
||||
return std::string{ guid_cstr };
|
||||
}
|
||||
UFUNCTION({})
|
||||
bool test(int a, float* b, char** c){}
|
||||
static Guid Make()
|
||||
{
|
||||
Guid guid;
|
||||
const auto res = CoCreateGuid((GUID*)&guid);
|
||||
return guid;
|
||||
}
|
||||
};
|
||||
struct SerializedMeta
|
||||
{
|
||||
UPROPERTY({})
|
||||
Guid guid;
|
||||
UPROPERTY({})
|
||||
string name;
|
||||
UPROPERTY({})
|
||||
string t_hash{};
|
||||
UPROPERTY({})
|
||||
string metadata;
|
||||
};
|
||||
struct ResourceBundle;
|
||||
|
||||
};
|
||||
}
|
||||
struct MetaBundle
|
||||
{
|
||||
UPROPERTY({})
|
||||
vector<SerializedMeta> metadatas;
|
||||
|
||||
MetaBundle() = default;
|
||||
};
|
||||
#include "guid_gen.inl"
|
||||
52
engine/3rdparty/zlib/test/yaml/main.cpp
vendored
52
engine/3rdparty/zlib/test/yaml/main.cpp
vendored
@ -4,34 +4,32 @@
|
||||
#include <cassert>
|
||||
using namespace refl;
|
||||
using namespace std;
|
||||
using namespace test;
|
||||
int main() {
|
||||
using T = test::Guid;
|
||||
auto cls = &TypeInfo<T>::StaticClass;
|
||||
int x = 123;
|
||||
Guid g1 = Guid::Make();
|
||||
void testGuid() {
|
||||
Guid guid = Guid::Make();
|
||||
YAML::TextArchive::Register<Guid>();
|
||||
string rx = YAML::Text_Serialize(x);
|
||||
string rg1 = YAML::Text_Serialize(g1);
|
||||
YAML::Node rg2 = YAML::Load(rg1);
|
||||
auto res1 = YAML::Text_Unserialize<Guid>(rg1);
|
||||
Guid g2 = res1.value();
|
||||
assert(g1 == g2);
|
||||
//auto res2 = YAML::Text_Unserialize<MetaBundle>(rg1);
|
||||
//if (res2) {
|
||||
//MetaBundle aa = res2.value();
|
||||
//}
|
||||
std::cout << rg1 << std::endl;
|
||||
YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
|
||||
for (std::size_t i = 0; i < primes.size(); i++) {
|
||||
std::cout << primes[i].as<int>() << "\n";
|
||||
string text = YAML::Text_Serialize(guid);
|
||||
auto res = YAML::Text_Unserialize<Guid>(text);
|
||||
Guid guid2 = res.value();
|
||||
assert(guid == guid2);
|
||||
std::cout << guid << std::endl;
|
||||
}
|
||||
void testMeta() {
|
||||
MetaBundle mb1;
|
||||
mb1.metadatas.push_back({ Guid::Make() , "hello1" });
|
||||
mb1.metadatas.push_back({ Guid::Make() , "hello2" });
|
||||
mb1.metadatas.push_back({ Guid::Make() , "hello3" });
|
||||
mb1.metadatas.push_back({ Guid::Make() , "hello4" });
|
||||
string rmb1 = YAML::Text_Serialize(mb1);
|
||||
auto rmb2 = YAML::Text_Unserialize<MetaBundle>(rmb1);
|
||||
if (rmb2) {
|
||||
MetaBundle aa = rmb2.value();
|
||||
}
|
||||
// or:
|
||||
for (YAML::const_iterator it = primes.begin(); it != primes.end(); ++it) {
|
||||
std::cout << it->as<int>() << "\n";
|
||||
}
|
||||
|
||||
primes.push_back(13);
|
||||
assert(primes.size() == 6);
|
||||
}
|
||||
int main() {
|
||||
constexpr bool m1 = refl::is_map_v<std::vector<int>>;
|
||||
constexpr bool m2 = refl::is_sequence_v<std::map<int, float>>;
|
||||
auto cls = &TypeInfo<Guid>::StaticClass;
|
||||
testGuid();
|
||||
testMeta();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user