update refl

This commit is contained in:
ouczbs 2024-04-03 17:57:38 +08:00
parent ec5ce69a80
commit faf8e9aedf
5 changed files with 95 additions and 108 deletions

View File

@ -5,27 +5,43 @@
using namespace Ubpa; using namespace Ubpa;
using namespace Ubpa::UDRefl; using namespace Ubpa::UDRefl;
struct Vec2 {
Vec2* next;
float x;
};
struct Vec { struct Vec {
float x; float x;
float y; float y;
Vec2 z;
float norm() const { float norm() const {
return std::sqrt(x * x + y * y); return std::sqrt(x * x + y * y);
} }
}; };
class ObjectView2 : public ObjectView {
int TestUDRefl() { public:
ObjectView2() {
this->type.GetName();
this->type.GetID();
}
};
int main2() {
Mngr.RegisterType<Vec>(); Mngr.RegisterType<Vec>();
Mngr.AddField<&Vec::x>("x"); Mngr.AddField<&Vec::x>("x");
Mngr.AddField<&Vec::y>("y"); Mngr.AddField<&Vec::y>("y");
Mngr.AddField<&Vec::z>("z");
Mngr.AddMethod<&Vec::norm>("norm"); Mngr.AddMethod<&Vec::norm>("norm");
SharedObject v = Mngr.MakeShared(Type_of<Vec>); SharedObject v = Mngr.MakeShared(Type_of<Vec>);
std::cout << v.GetType().GetName() << std::endl; // prints "Vec" std::cout << v.GetType().GetName() << std::endl; // prints "Vec"
Vec v1;
Vec* pv = &v1;
v.Var("x") = 3; v.Var("x") = 3;
v.Var("y") = 4; v.Var("y") = 4;
Vec2 v2{ &v1.z,1.0 };
pv = v.AsPtr<Vec>();
v.Var("z") = v2;
v.Var("z") = 2;
v.Var("xz") = 1;
std::cout << "x: " << v.Var("x") << std::endl; std::cout << "x: " << v.Var("x") << std::endl;
std::cout << "norm: " << v.Invoke("norm") << std::endl; std::cout << "norm: " << v.Invoke("norm") << std::endl;
@ -37,4 +53,5 @@ int TestUDRefl() {
for (auto&& [name, var] : v.GetVars()) for (auto&& [name, var] : v.GetVars())
std::cout << name.GetView() << ": " << var << std::endl; std::cout << name.GetView() << ": " << var << std::endl;
return 0;
} }

View File

@ -1,14 +1,21 @@
#pragma once #pragma once
#include "USRefl_99.h" #include "USRefl/USRefl.h"
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <functional> #include <functional>
using namespace Ubpa::USRefl; using namespace Ubpa::USRefl;
struct Vec1 {
struct Vec { constexpr static int a1{ 1 };
float x; float x;
float y; float y;
};
struct Vec {
constexpr static int a1{ 1 };
constexpr static int a2{ 1 };
float x;
float y;
Vec1 next;
float norm() const { float norm() const {
return std::sqrt(x * x + y * y); return std::sqrt(x * x + y * y);
} }
@ -20,9 +27,10 @@ struct Ubpa::USRefl::TypeInfo<Vec> :
{ {
static constexpr AttrList attrs = {}; static constexpr AttrList attrs = {};
static constexpr FieldList fields = { static constexpr FieldList fields = {
Field {TSTR("x"), &Type::x}, Field {TSTR("x"), &Vec::x},
Field {TSTR("y"), &Type::y}, Field {TSTR("y"), &Vec::y},
Field {TSTR("norm"), &Type::norm}, Field {TSTR("next"), &Vec::next},
Field {TSTR("norm"), &Vec::norm},
}; };
}; };
@ -32,14 +40,20 @@ int TestUSRefl() {
}); });
Vec v; Vec v;
Vec1 v1{ 1,2 };
auto t1 = TypeInfo<Vec>::fields;
auto t2 = t1.Find(TSTR("x"));
auto t3 = t2.value;
v.*(t3) = 3.f;
std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v) = 3.f; std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v) = 3.f;
std::invoke(TypeInfo<Vec>::fields.Find(TSTR("y")).value, v) = 4.f; std::invoke(TypeInfo<Vec>::fields.Find(TSTR("y")).value, v) = 4.f;
std::invoke(TypeInfo<Vec>::fields.Find(TSTR("next")).value, v) = v1;
std::cout << "x: " << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v) << std::endl; std::cout << "x: " << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v) << std::endl;
std::cout << "norm: " << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("norm")).value, v) << std::endl; std::cout << "norm: " << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("norm")).value, v) << std::endl;
TypeInfo<Vec>::ForEachVarOf(v, [](const auto& field, const auto& var) { TypeInfo<Vec>::ForEachVarOf(v, [](const auto& field, const auto& var) {
std::cout << field.name << ": " << var << std::endl; //std::cout << field.name << ": " << var << std::endl;
}); });
return 0;
} }

View File

@ -1,102 +1,57 @@
#include <USRefl/USRefl.h> #pragma once
#include <UDRefl/UDRefl.hpp>
#include <iostream> #include <iostream>
#include <cmath>
#include <cassert> using namespace Ubpa;
using namespace Ubpa::UDRefl;
using namespace Ubpa::USRefl; struct Vec2 {
using namespace std; Vec2* next;
float x;
template<std::size_t N>
constexpr std::size_t Func() { return N; }
enum class [[enum_attr("enum_attr_value")]] Color {
RED [[enumerator_attr("enumerator_attr_value"), func(&Func<1>)]] = 1,
GREEN [[func(&Func<2>)]] = 2,
BLUE [[func(&Func<3>)]] = 4
}; };
struct Vec {
template<> float x;
struct Ubpa::USRefl::TypeInfo<Color> : float y;
TypeInfoBase<Color> Vec2 z;
{ float norm() const {
static constexpr AttrList attrs = { return std::sqrt(x * x + y * y);
Attr {TSTR("enum_attr"), "enum_attr_value"}, }
}; };
static constexpr FieldList fields = { class ObjectView2 : public ObjectView {
Field {TSTR("RED"), Type::RED, AttrList { public:
Attr {TSTR("enumerator_attr"), "enumerator_attr_value"}, ObjectView2() {
Attr {TSTR("func"), &Func<1>}, this->type.GetName();
}}, this->type.GetID();
Field {TSTR("GREEN"), Type::GREEN, AttrList { }
Attr {TSTR("func"), &Func<2>},
}},
Field {TSTR("BLUE"), Type::BLUE, AttrList {
Attr {TSTR("func"), &Func<3>},
}},
}; };
};
int main() { int main() {
cout << TypeInfo<Color>::name << endl; Mngr.RegisterType<Vec>();
Mngr.AddField<&Vec::x>("x");
Mngr.AddField<&Vec::y>("y");
Mngr.AddField<&Vec::z>("z");
Mngr.AddMethod<&Vec::norm>("norm");
TypeInfo<Color>::fields.ForEach([](auto field) { SharedObject v = Mngr.MakeShared(Type_of<Vec>);
cout << field.name << endl; std::cout << v.GetType().GetName() << std::endl; // prints "Vec"
}); Vec v1;
Vec* pv = &v1;
v.Var("x") = 3;
v.Var("y") = 4;
Vec2 v2{ &v1.z,1.0 };
pv = v.AsPtr<Vec>();
v.Var("z") = v2;
v.Var("z") = 2;
v.Var("xz") = 1;
std::cout << "x: " << v.Var("x") << std::endl;
std::cout << "norm: " << v.Invoke("norm") << std::endl;
Color red = Color::RED; for (auto&& [name, info] : FieldRange_of<Vec>)
std::string_view nameof_red = "RED"; std::cout << name.GetView() << std::endl;
// name -> value for (auto&& [name, info] : MethodRange_of<Vec>)
{ std::cout << name.GetView() << std::endl;
// compile-time
static_assert(TypeInfo<Color>::fields.ValueOfName<Color>("GREEN") == Color::GREEN);
// runtime
assert(TypeInfo<Color>::fields.ValueOfName<Color>(nameof_red) == red);
}
// value -> name
{
// compile-time
static_assert(TypeInfo<Color>::fields.NameOfValue(Color::GREEN) == "GREEN");
// runtime
assert(TypeInfo<Color>::fields.NameOfValue(red) == nameof_red);
}
// name -> attr
{
// compile-time
static_assert(TypeInfo<Color>::fields.Find(TSTR("GREEN")).attrs.Find(TSTR("func")).value() == 2);
// runtime
std::size_t rst = static_cast<std::size_t>(-1);
TypeInfo<Color>::fields.FindIf([nameof_red, &rst](auto field) {
if (field.name == nameof_red) {
rst = field.attrs.Find(TSTR("func")).value();
return true;
}
else
return false;
});
assert(rst == 1);
}
// value -> attr
{
static_assert(USRefl_ElemList_GetByValue(TypeInfo<Color>::fields, Color::GREEN).attrs.Find(TSTR("func")).value() == 2);
// runtime
std::size_t rst = static_cast<std::size_t>(-1);
TypeInfo<Color>::fields.FindIf([red, &rst](auto field) {
if (field.value == red) {
rst = field.attrs.Find(TSTR("func")).value();
return true;
}
else
return false;
});
assert(rst == 1);
}
for (auto&& [name, var] : v.GetVars())
std::cout << name.GetView() << ": " << var << std::endl;
return 0;
} }

View File

@ -1,9 +1,9 @@
add_rules("mode.debug", "mode.release") add_rules("mode.debug", "mode.release")
--add_requires("UTemplate","USmallFlat","USRefl","UDRefl") add_requires("UTemplate","USmallFlat", "USRefl", "UDRefl")
target("ReflTest") target("ReflTest")
set_kind("binary") set_kind("binary")
set_languages("c++20") set_languages("c++20")
add_packages("USRefl","UDRefl") add_packages("UTemplate","USmallFlat", "USRefl", "UDRefl")
add_files("src/*.cpp") add_files("src/*.cpp")
add_headerfiles("src/*.h") add_headerfiles("src/*.h")

View File

@ -1,3 +1,4 @@
add_requires("spdlog") add_requires("spdlog")
includes("**/xmake.lua|ubpa/**/xmake.lua") --includes("**/xmake.lua|ubpa/**/xmake.lua")
includes("**/xmake.lua","ubpa/*/xmake.lua")