feat: add new fancy reflection

This commit is contained in:
2026-11-01 23:12:00 +01:00
parent 08acd84404
commit 6a85d17899
58 changed files with 380 additions and 1055 deletions
+25
View File
@@ -1,3 +1,4 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <filesystem>
@@ -415,6 +416,11 @@ namespace {
v.Field("target", target);
v.Field("targets", targets);
v.Field("cache", cache, {.transient = true});
v.Field("extents", extents, {.min = 0.5});
v.Property("limited", limited, [this](int value) {
limited = std::min(value, 5);
++setterCalls;
});
}
bool flag{false};
@@ -431,6 +437,9 @@ namespace {
ObjectRef target;
std::vector<ObjectRef> targets;
int cache{0};
glm::vec3 extents{1.0f};
int limited{0};
int setterCalls{0};
};
[[nodiscard]] bool throwsMentioning(ReflectedComponent& component, const nlohmann::json& data, const char* key)
@@ -493,10 +502,25 @@ namespace {
component->Deserialize({{"mode", 2}});
check(component->mode == TestMode::Fast, "named enums must still load from a number");
component->Deserialize({{"extents", {2.0f, 0.1f, -1.0f}}});
check(component->extents == glm::vec3{2.0f, 0.5f, 0.5f}, "loading must clamp every component of a vector");
check(throwsMentioning(*component, {{"mode", "Turbo"}}, "\"mode\""), "an unknown enum name must fail and name the field");
check(throwsMentioning(*component, {{"offset", "up"}}, "\"offset\""), "a wrong type must fail and name the field");
}
void testReflectedProperties()
{
GameObject object{"reflected"};
auto* component = object.AddComponent<ReflectedComponent>();
component->Deserialize({{"flag", true}});
check(component->setterCalls == 0, "a property setter must not run when its key is missing");
component->Deserialize({{"limited", 9}});
check(component->limited == 5 && component->setterCalls == 1, "loading a property must go through its setter");
check(component->Serialize().at("limited") == 5, "a property must save its current value");
}
void testReflectedReferencesResolve()
{
ComponentFactory::Register<ReflectedComponent>("ReflectedComponent");
@@ -538,6 +562,7 @@ void RunSerializationTests()
{
testReflectedRoundTrip();
testReflectedLoadClampsAndKeepsDefaults();
testReflectedProperties();
testReflectedReferencesResolve();
testFieldLabels();
testTonemapperNames();