update refl
This commit is contained in:
parent
ec5ce69a80
commit
faf8e9aedf
@ -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<Vec>();
|
||||
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<Vec>);
|
||||
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;
|
||||
|
||||
@ -37,4 +53,5 @@ int TestUDRefl() {
|
||||
|
||||
for (auto&& [name, var] : v.GetVars())
|
||||
std::cout << name.GetView() << ": " << var << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,14 +1,21 @@
|
||||
#pragma once
|
||||
#include "USRefl_99.h"
|
||||
#include "USRefl/USRefl.h"
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
|
||||
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<Vec> :
|
||||
{
|
||||
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<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("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 << "norm: " << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("norm")).value, v) << std::endl;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -1,102 +1,57 @@
|
||||
#include <USRefl/USRefl.h>
|
||||
|
||||
#pragma once
|
||||
#include <UDRefl/UDRefl.hpp>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
using namespace Ubpa::USRefl;
|
||||
using namespace std;
|
||||
|
||||
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
|
||||
using namespace Ubpa;
|
||||
using namespace Ubpa::UDRefl;
|
||||
struct Vec2 {
|
||||
Vec2* next;
|
||||
float x;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Ubpa::USRefl::TypeInfo<Color> :
|
||||
TypeInfoBase<Color>
|
||||
{
|
||||
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<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) {
|
||||
cout << field.name << endl;
|
||||
});
|
||||
SharedObject v = Mngr.MakeShared(Type_of<Vec>);
|
||||
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;
|
||||
std::string_view nameof_red = "RED";
|
||||
for (auto&& [name, info] : FieldRange_of<Vec>)
|
||||
std::cout << name.GetView() << std::endl;
|
||||
|
||||
// name -> value
|
||||
{
|
||||
// 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, info] : MethodRange_of<Vec>)
|
||||
std::cout << name.GetView() << std::endl;
|
||||
|
||||
for (auto&& [name, var] : v.GetVars())
|
||||
std::cout << name.GetView() << ": " << var << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -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")
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
|
||||
add_requires("spdlog")
|
||||
includes("**/xmake.lua|ubpa/**/xmake.lua")
|
||||
--includes("**/xmake.lua|ubpa/**/xmake.lua")
|
||||
includes("**/xmake.lua","ubpa/*/xmake.lua")
|
||||
Loading…
Reference in New Issue
Block a user