From faf8e9aedfba7c75d35c5f9be15a11132d72385e Mon Sep 17 00:00:00 2001 From: ouczbs Date: Wed, 3 Apr 2024 17:57:38 +0800 Subject: [PATCH] update refl --- src/ReflTest/src/TestUDRefl.h | 27 +++++-- src/ReflTest/src/TestUSRefl.h | 30 ++++++-- src/ReflTest/src/main.cpp | 139 ++++++++++++---------------------- src/ReflTest/xmake.lua | 4 +- thridpart/xmake.lua | 3 +- 5 files changed, 95 insertions(+), 108 deletions(-) diff --git a/src/ReflTest/src/TestUDRefl.h b/src/ReflTest/src/TestUDRefl.h index 52c55c3..2f2bf14 100644 --- a/src/ReflTest/src/TestUDRefl.h +++ b/src/ReflTest/src/TestUDRefl.h @@ -5,27 +5,43 @@ using namespace Ubpa; using namespace Ubpa::UDRefl; - +struct Vec2 { + Vec2* next; + float x; +}; struct Vec { float x; float y; + Vec2 z; float norm() const { return std::sqrt(x * x + y * y); } }; - -int TestUDRefl() { +class ObjectView2 : public ObjectView { +public: + ObjectView2() { + this->type.GetName(); + this->type.GetID(); + } +}; +int main2() { Mngr.RegisterType(); Mngr.AddField<&Vec::x>("x"); Mngr.AddField<&Vec::y>("y"); + Mngr.AddField<&Vec::z>("z"); Mngr.AddMethod<&Vec::norm>("norm"); SharedObject v = Mngr.MakeShared(Type_of); 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(); + 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; @@ -37,4 +53,5 @@ int TestUDRefl() { for (auto&& [name, var] : v.GetVars()) std::cout << name.GetView() << ": " << var << std::endl; + return 0; } diff --git a/src/ReflTest/src/TestUSRefl.h b/src/ReflTest/src/TestUSRefl.h index c0c1432..90dcf30 100644 --- a/src/ReflTest/src/TestUSRefl.h +++ b/src/ReflTest/src/TestUSRefl.h @@ -1,14 +1,21 @@ #pragma once -#include "USRefl_99.h" +#include "USRefl/USRefl.h" #include #include #include using namespace Ubpa::USRefl; - -struct Vec { +struct Vec1 { + constexpr static int a1{ 1 }; float x; float y; +}; +struct Vec { + constexpr static int a1{ 1 }; + constexpr static int a2{ 1 }; + float x; + float y; + Vec1 next; float norm() const { return std::sqrt(x * x + y * y); } @@ -20,9 +27,10 @@ struct Ubpa::USRefl::TypeInfo : { static constexpr AttrList attrs = {}; static constexpr FieldList fields = { - Field {TSTR("x"), &Type::x}, - Field {TSTR("y"), &Type::y}, - Field {TSTR("norm"), &Type::norm}, + Field {TSTR("x"), &Vec::x}, + Field {TSTR("y"), &Vec::y}, + Field {TSTR("next"), &Vec::next}, + Field {TSTR("norm"), &Vec::norm}, }; }; @@ -32,14 +40,20 @@ int TestUSRefl() { }); Vec v; - + Vec1 v1{ 1,2 }; + auto t1 = TypeInfo::fields; + auto t2 = t1.Find(TSTR("x")); + auto t3 = t2.value; + v.*(t3) = 3.f; std::invoke(TypeInfo::fields.Find(TSTR("x")).value, v) = 3.f; std::invoke(TypeInfo::fields.Find(TSTR("y")).value, v) = 4.f; + std::invoke(TypeInfo::fields.Find(TSTR("next")).value, v) = v1; std::cout << "x: " << std::invoke(TypeInfo::fields.Find(TSTR("x")).value, v) << std::endl; std::cout << "norm: " << std::invoke(TypeInfo::fields.Find(TSTR("norm")).value, v) << std::endl; TypeInfo::ForEachVarOf(v, [](const auto& field, const auto& var) { - std::cout << field.name << ": " << var << std::endl; + //std::cout << field.name << ": " << var << std::endl; }); + return 0; } diff --git a/src/ReflTest/src/main.cpp b/src/ReflTest/src/main.cpp index 453d662..83ecd51 100644 --- a/src/ReflTest/src/main.cpp +++ b/src/ReflTest/src/main.cpp @@ -1,102 +1,57 @@ -#include - +#pragma once +#include #include +#include -#include - -using namespace Ubpa::USRefl; -using namespace std; - -template -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 +using namespace Ubpa; +using namespace Ubpa::UDRefl; +struct Vec2 { + Vec2* next; + float x; }; - -template<> -struct Ubpa::USRefl::TypeInfo : - TypeInfoBase -{ - static constexpr AttrList attrs = { - Attr {TSTR("enum_attr"), "enum_attr_value"}, - }; - static constexpr FieldList fields = { - Field {TSTR("RED"), Type::RED, AttrList { - Attr {TSTR("enumerator_attr"), "enumerator_attr_value"}, - Attr {TSTR("func"), &Func<1>}, - }}, - Field {TSTR("GREEN"), Type::GREEN, AttrList { - Attr {TSTR("func"), &Func<2>}, - }}, - Field {TSTR("BLUE"), Type::BLUE, AttrList { - Attr {TSTR("func"), &Func<3>}, - }}, - }; +struct Vec { + float x; + float y; + Vec2 z; + float norm() const { + return std::sqrt(x * x + y * y); + } +}; +class ObjectView2 : public ObjectView { +public: + ObjectView2() { + this->type.GetName(); + this->type.GetID(); + } }; - int main() { - cout << TypeInfo::name << endl; + Mngr.RegisterType(); + Mngr.AddField<&Vec::x>("x"); + Mngr.AddField<&Vec::y>("y"); + Mngr.AddField<&Vec::z>("z"); + Mngr.AddMethod<&Vec::norm>("norm"); - TypeInfo::fields.ForEach([](auto field) { - cout << field.name << endl; - }); + SharedObject v = Mngr.MakeShared(Type_of); + 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(); + 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; - std::string_view nameof_red = "RED"; + for (auto&& [name, info] : FieldRange_of) + std::cout << name.GetView() << std::endl; - // name -> value - { - // compile-time - static_assert(TypeInfo::fields.ValueOfName("GREEN") == Color::GREEN); - - // runtime - assert(TypeInfo::fields.ValueOfName(nameof_red) == red); - } - - // value -> name - { - // compile-time - static_assert(TypeInfo::fields.NameOfValue(Color::GREEN) == "GREEN"); - - // runtime - assert(TypeInfo::fields.NameOfValue(red) == nameof_red); - } - - // name -> attr - { - // compile-time - static_assert(TypeInfo::fields.Find(TSTR("GREEN")).attrs.Find(TSTR("func")).value() == 2); - // runtime - std::size_t rst = static_cast(-1); - TypeInfo::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::fields, Color::GREEN).attrs.Find(TSTR("func")).value() == 2); - - // runtime - std::size_t rst = static_cast(-1); - TypeInfo::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, info] : MethodRange_of) + std::cout << name.GetView() << std::endl; + for (auto&& [name, var] : v.GetVars()) + std::cout << name.GetView() << ": " << var << std::endl; + return 0; } diff --git a/src/ReflTest/xmake.lua b/src/ReflTest/xmake.lua index 3009cec..c4feb6a 100644 --- a/src/ReflTest/xmake.lua +++ b/src/ReflTest/xmake.lua @@ -1,9 +1,9 @@ add_rules("mode.debug", "mode.release") ---add_requires("UTemplate","USmallFlat","USRefl","UDRefl") +add_requires("UTemplate","USmallFlat", "USRefl", "UDRefl") target("ReflTest") set_kind("binary") set_languages("c++20") - add_packages("USRefl","UDRefl") + add_packages("UTemplate","USmallFlat", "USRefl", "UDRefl") add_files("src/*.cpp") add_headerfiles("src/*.h") diff --git a/thridpart/xmake.lua b/thridpart/xmake.lua index 95997db..2aa787e 100644 --- a/thridpart/xmake.lua +++ b/thridpart/xmake.lua @@ -1,3 +1,4 @@ add_requires("spdlog") -includes("**/xmake.lua|ubpa/**/xmake.lua") \ No newline at end of file +--includes("**/xmake.lua|ubpa/**/xmake.lua") +includes("**/xmake.lua","ubpa/*/xmake.lua") \ No newline at end of file