feat: add Boost serialization support for SmartPointer and include standard smart pointer headers
This commit is contained in:
@@ -42,6 +42,8 @@ TODO:
|
|||||||
#include <boost/mpl/for_each.hpp>
|
#include <boost/mpl/for_each.hpp>
|
||||||
#include <boost/mpl/remove_if.hpp>
|
#include <boost/mpl/remove_if.hpp>
|
||||||
#include <boost/serialization/nvp.hpp>
|
#include <boost/serialization/nvp.hpp>
|
||||||
|
#include <boost/serialization/shared_ptr.hpp>
|
||||||
|
#include <boost/serialization/unique_ptr.hpp>
|
||||||
|
|
||||||
// #include <boost/archive/xml_iarchive.hpp>
|
// #include <boost/archive/xml_iarchive.hpp>
|
||||||
// #include <boost/archive/xml_oarchive.hpp>
|
// #include <boost/archive/xml_oarchive.hpp>
|
||||||
|
|||||||
@@ -31,6 +31,9 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <boost/serialization/access.hpp>
|
||||||
|
#include <boost/serialization/nvp.hpp>
|
||||||
|
|
||||||
namespace uLib {
|
namespace uLib {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -184,6 +187,19 @@ public:
|
|||||||
explicit operator bool() const noexcept { return get() != nullptr; }
|
explicit operator bool() const noexcept { return get() != nullptr; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class boost::serialization::access;
|
||||||
|
|
||||||
|
template <class Archive>
|
||||||
|
void serialize(Archive& ar, const unsigned int /*version*/) {
|
||||||
|
if (Archive::is_loading::value) {
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
ar & boost::serialization::make_nvp("counter", m_counter);
|
||||||
|
if (Archive::is_loading::value && m_counter) {
|
||||||
|
m_counter->count.fetch_add(1, std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct ReferenceCounter {
|
struct ReferenceCounter {
|
||||||
T* ptr;
|
T* ptr;
|
||||||
std::atomic<uint32_t> count;
|
std::atomic<uint32_t> count;
|
||||||
@@ -195,6 +211,16 @@ private:
|
|||||||
template <typename D>
|
template <typename D>
|
||||||
ReferenceCounter(T* p, D d, uint32_t initial_count = 1)
|
ReferenceCounter(T* p, D d, uint32_t initial_count = 1)
|
||||||
: ptr(p), count(initial_count), deleter(d) {}
|
: ptr(p), count(initial_count), deleter(d) {}
|
||||||
|
|
||||||
|
ReferenceCounter()
|
||||||
|
: ptr(nullptr), count(0), deleter([](T* p) { delete p; }) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class boost::serialization::access;
|
||||||
|
template <class Archive>
|
||||||
|
void serialize(Archive& ar, const unsigned int /*version*/) {
|
||||||
|
ar & boost::serialization::make_nvp("ptr", ptr);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ReferenceCounter* m_counter;
|
ReferenceCounter* m_counter;
|
||||||
|
|||||||
@@ -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<A> 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() {
|
int test_referece_serialization() {
|
||||||
A a;
|
A a;
|
||||||
{
|
{
|
||||||
@@ -193,7 +213,25 @@ int test_referece_serialization() {
|
|||||||
return (b.m_a->a() == a.a() && b2.m_a->a() == a.a());
|
return (b.m_a->a() == a.a() && b2.m_a->a() == a.a());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int test_referece_smartpointer_serialization() {
|
||||||
|
SmartPointer<A> 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() {
|
int main() {
|
||||||
@@ -202,6 +240,7 @@ int main() {
|
|||||||
TEST1(test_V3f());
|
TEST1(test_V3f());
|
||||||
TEST1(testing_xml_class());
|
TEST1(testing_xml_class());
|
||||||
TEST1(test_referece_serialization());
|
TEST1(test_referece_serialization());
|
||||||
|
TEST1(test_referece_smartpointer_serialization());
|
||||||
// TEST1(testing_hrt_class());
|
// TEST1(testing_hrt_class());
|
||||||
|
|
||||||
END_TESTING;
|
END_TESTING;
|
||||||
|
|||||||
Reference in New Issue
Block a user