feat: add Boost serialization support for SmartPointer and include standard smart pointer headers
This commit is contained in:
@@ -29,10 +29,10 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include <boost/serialization/access.hpp>
|
#include <boost/serialization/access.hpp>
|
||||||
#include <boost/serialization/nvp.hpp>
|
#include <boost/serialization/nvp.hpp>
|
||||||
#include <boost/serialization/split_member.hpp>
|
|
||||||
|
|
||||||
namespace uLib {
|
namespace uLib {
|
||||||
|
|
||||||
@@ -219,18 +219,49 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename U> friend class SmartPointer;
|
|
||||||
friend class boost::serialization::access;
|
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 {
|
||||||
|
T *ptr;
|
||||||
|
std::atomic<uint32_t> count;
|
||||||
|
std::function<void(T *)> deleter;
|
||||||
|
|
||||||
|
ReferenceCounter(T *p, uint32_t initial_count = 1)
|
||||||
|
: ptr(p), count(initial_count),
|
||||||
|
deleter([](T *ptr_to_del) { delete ptr_to_del; }) {}
|
||||||
|
|
||||||
|
template <typename D>
|
||||||
|
ReferenceCounter(T *p, D d, uint32_t initial_count = 1)
|
||||||
|
: 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;
|
||||||
|
|
||||||
void acquire(ReferenceCounter *c) noexcept {
|
void acquire(ReferenceCounter *c) noexcept {
|
||||||
|
m_counter = c;
|
||||||
if (c) {
|
if (c) {
|
||||||
m_counter = new ReferenceCounter();
|
c->count.fetch_add(1, std::memory_order_relaxed);
|
||||||
m_counter->ptr = c->ptr;
|
|
||||||
m_counter->cb = c->cb;
|
|
||||||
if (m_counter->cb)
|
|
||||||
m_counter->cb->count.fetch_add(1, std::memory_order_relaxed);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user