property - first attempt

This commit is contained in:
AndreaRigoni
2026-03-23 12:55:09 +00:00
parent b52ae808b8
commit 94843de711
16 changed files with 482 additions and 63 deletions

View File

@@ -21,6 +21,8 @@ set( TESTS
OptionsTest
PingPongTest
VectorMetaAllocatorTest
PropertyTypesTest
HRPTest
)
set(LIBRARIES

View File

@@ -0,0 +1,49 @@
#include <iostream>
#include <sstream>
#include "Core/Serializable.h"
#include "Core/Archives.h"
using namespace uLib;
struct SimpleObject {
int value;
std::string name;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & HRP(value);
ar & HRP(name);
}
};
int main() {
SimpleObject obj;
obj.value = 42;
obj.name = "TestObject";
std::cout << "Testing HRP Serialization to Log..." << std::endl;
std::stringstream ss;
{
uLib::Archive::log_archive ar(ss);
ar << boost::serialization::make_nvp("Object", obj);
}
std::cout << ss.str() << std::endl;
std::cout << "Testing HRP Serialization to HRT..." << std::endl;
ss.str("");
{
uLib::Archive::hrt_oarchive ar(ss);
ar << obj;
}
std::cout << ss.str() << std::endl;
std::cout << "Testing HRP Serialization to XML..." << std::endl;
ss.str("");
{
uLib::Archive::xml_oarchive ar(ss);
ar << boost::serialization::make_nvp("Object", obj);
}
std::cout << ss.str() << std::endl;
return 0;
}

View File

@@ -0,0 +1,64 @@
#include <iostream>
#include <sstream>
#include "Core/Object.h"
#include "Core/Property.h"
#include <cassert>
using namespace uLib;
class TestObject : public Object {
public:
TestObject() : Object(),
IntProp(this, "IntProp", 10),
StringProp(this, "StringProp", "Initial")
{}
virtual const char* GetClassName() const override { return "TestObject"; }
Property<int> IntProp;
Property<std::string> StringProp;
};
int main() {
TestObject obj;
std::cout << "Testing Properties..." << std::endl;
// 1. Check registration
const auto& props = obj.GetProperties();
assert(props.size() == 2);
assert(props[0]->GetName() == "IntProp");
assert(props[1]->GetName() == "StringProp");
// 2. Check value access and signals
bool signalCalled = false;
uLib::Object::connect(&obj.IntProp, &Property<int>::PropertyChanged, [&signalCalled]() {
signalCalled = true;
});
assert(obj.IntProp.Get() == 10);
obj.IntProp = 20;
assert(obj.IntProp.Get() == 20);
assert(signalCalled == true);
// 3. Check serialization
std::stringstream ss;
Object::SaveXml(ss, obj);
std::string xml = ss.str();
std::cout << "Serialized XML: \n" << xml << std::endl;
assert(xml.find("<IntProp>20</IntProp>") != std::string::npos);
assert(xml.find("<StringProp>Initial</StringProp>") != std::string::npos);
// 4. Check deserialization
TestObject obj2;
std::stringstream ss2(xml);
Object::LoadXml(ss2, obj2);
assert(obj2.IntProp.Get() == 20);
assert(obj2.StringProp.Get() == "Initial");
std::cout << "All Property Tests PASSED!" << std::endl;
return 0;
}

View File

@@ -0,0 +1,68 @@
#include <iostream>
#include <sstream>
#include <cassert>
#include "Core/Object.h"
#include "Core/Property.h"
#include "Math/Dense.h"
using namespace uLib;
class TestObject : public Object {
public:
TestObject() : Object() {}
virtual const char* GetClassName() const override { return "TestObject"; }
// Use new typedefs
StringProperty StringProp = StringProperty(this, "StringProp", "Initial");
IntProperty IntProp = IntProperty(this, "IntProp", 42);
FloatProperty FloatProp = FloatProperty(this, "FloatProp", 3.14f);
BoolProperty BoolProp = BoolProperty(this, "BoolProp", true);
// Use new macro
ULIB_PROPERTY(Matrix3f, MatrixProp, Matrix3f::Identity())
// Use new Dense typedefs
Vector3fProperty Vector3fProp = Vector3fProperty(this, "Vector3fProp", Vector3f(1.1f, 2.2f, 3.3f));
Matrix4fProperty Matrix4fProp = Matrix4fProperty(this, "Matrix4fProp", Matrix4f::Identity());
};
int main() {
TestObject obj;
std::cout << "Testing Property Types..." << std::endl;
// 1. Verify string representation
std::cout << "StringProp: " << obj.StringProp.GetValueAsString() << std::endl;
assert(obj.StringProp.GetValueAsString() == "Initial");
std::cout << "IntProp: " << obj.IntProp.GetValueAsString() << std::endl;
assert(obj.IntProp.GetValueAsString() == "42");
std::cout << "FloatProp: " << obj.FloatProp.GetValueAsString() << std::endl;
// boost::lexical_cast might have different precision, but for 3.14 it should be okay or we check find
assert(obj.FloatProp.GetValueAsString().find("3.14") != std::string::npos);
std::cout << "BoolProp: " << obj.BoolProp.GetValueAsString() << std::endl;
// Bool might be "1" or "true" depending on lexical_cast/stringstream
assert(obj.BoolProp.GetValueAsString() == "1" || obj.BoolProp.GetValueAsString() == "true");
// 2. Verify Matrix/Vector string representation (uses operator<<)
std::cout << "MatrixProp: \n" << obj.MatrixProp.GetValueAsString() << std::endl;
assert(obj.MatrixProp.GetValueAsString().find("1 0 0") != std::string::npos);
std::cout << "Vector3fProp: " << obj.Vector3fProp.GetValueAsString() << std::endl;
assert(obj.Vector3fProp.GetValueAsString().find("1.1 2.2 3.3") != std::string::npos);
std::cout << "Matrix4fProp: \n" << obj.Matrix4fProp.GetValueAsString() << std::endl;
assert(obj.Matrix4fProp.GetValueAsString().find("1 0 0 0") != std::string::npos);
// 3. Verify updates and signals
obj.IntProp = 100;
assert(obj.IntProp.Get() == 100);
assert(obj.IntProp.GetValueAsString() == "100");
std::cout << "All Property Type Tests PASSED!" << std::endl;
return 0;
}