From cbb9aa1139d6dc806873cfdc26ab0827fa5352c8 Mon Sep 17 00:00:00 2001 From: AndreaRigoni Date: Thu, 16 Apr 2026 11:30:50 +0000 Subject: [PATCH] feat: add Boost serialization support for SmartPointer and include standard smart pointer headers --- src/Core/Serializable.h | 2 ++ src/Core/SmartPointer.h | 7 +++++- src/Core/testing/SerializeTest.cpp | 39 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Core/Serializable.h b/src/Core/Serializable.h index ee14e20..9f8f85b 100644 --- a/src/Core/Serializable.h +++ b/src/Core/Serializable.h @@ -42,6 +42,8 @@ TODO: #include #include #include +#include +#include // #include // #include diff --git a/src/Core/SmartPointer.h b/src/Core/SmartPointer.h index f3ff70c..8448d4c 100644 --- a/src/Core/SmartPointer.h +++ b/src/Core/SmartPointer.h @@ -36,6 +36,9 @@ #include #include +#include +#include + namespace uLib { /** @@ -216,6 +219,7 @@ public: private: friend class boost::serialization::access; + struct ReferenceCounter { T* ptr; std::atomic count; @@ -228,7 +232,8 @@ private: ReferenceCounter(T* p, D d, uint32_t initial_count = 1) : ptr(p), count(initial_count), deleter(d) {} - ReferenceCounter() : ptr(nullptr), count(0) {} + ReferenceCounter() + : ptr(nullptr), count(0), deleter([](T* p) { delete p; }) {} private: friend class boost::serialization::access; diff --git a/src/Core/testing/SerializeTest.cpp b/src/Core/testing/SerializeTest.cpp index d996f05..c54260a 100644 --- a/src/Core/testing/SerializeTest.cpp +++ b/src/Core/testing/SerializeTest.cpp @@ -172,6 +172,26 @@ ULIB_SERIALIZE_OBJECT(B, Object) { +class C : public virtual Object { + uLibTypeMacro(C, Object) + ULIB_SERIALIZE_ACCESS + +public: + C() : m_c(1234566) {} + + int m_c; + + // reference to A (serializes through ID) + SmartPointer m_a; +}; + +ULIB_SERIALIZABLE_OBJECT(C) +ULIB_SERIALIZE_OBJECT(C, Object) { + ar & "Object C : " & "--> m_c = " & AR(m_c) & "--> m_a = " & AR(m_a); +} + + + int test_referece_serialization() { A a; { @@ -193,7 +213,25 @@ int test_referece_serialization() { return (b.m_a->a() == a.a() && b2.m_a->a() == a.a()); } +int test_referece_smartpointer_serialization() { + SmartPointer a; + a->init_properties(); + { + C c, c2; c.m_a = a; c2.m_a = a; + + std::ofstream file("test_ref_smartpointer.xml"); + Archive::xml_oarchive(file) << NVP(c) << NVP(c2); + } + + + C c, c2; + { + std::ifstream file("test_ref_smartpointer.xml"); + Archive::xml_iarchive(file) >> NVP(c) >> NVP(c2); + } + return (c.m_a->a() == a->a() && c2.m_a->a() == a->a()); +} int main() { @@ -202,6 +240,7 @@ int main() { TEST1(test_V3f()); TEST1(testing_xml_class()); TEST1(test_referece_serialization()); + TEST1(test_referece_smartpointer_serialization()); // TEST1(testing_hrt_class()); END_TESTING;