refactor: decouple SmartPointer reference counting

This commit is contained in:
AndreaRigoni
2026-04-18 19:22:23 +00:00
parent b82a151330
commit 503c325f9a
2 changed files with 189 additions and 195 deletions

View File

@@ -12,6 +12,14 @@ The ObjectContext is responsible to keep track of all the objects that are added
For this reason the access to a object context for a Object via Get/Set is done using the SmartPointer instances.
## SmartPointer access
SmartPointer is a class that is used to hold a reference to another object. It is a template class that can be used to hold a reference to any object that is derived from uLib::Object. It is a smart pointer because it will automatically delete the object when it is no longer needed. It is also a smart pointer because it will automatically update the object when it is no longer needed.
The ObjectContext is responsible to keep track of all the objects that are added to it and to provide a way to access them, but also it holds the SmartPointer instances that point to the objects that are added to it. In this way Objects added to a Context are disposed only when the context is destroyed.
For this reason the access to a object context for a Object via Get/Set is done using the SmartPointer instances.
## Geant Physical Volumes
The Geant library add a further layer of complexity. The physical volumes are created from a what is called LogicalVolume (which holds information about the shape, material and daughter volumes) and represent the actual instances of the volumes in the detector. So in this sense they represent what could be the Prop3D in the uLib Vtk library. The PhysicalVolume is created from the LogicalVolume and is the one that is actually placed in the scene, with its own relative TRS: position and rotation (rotation here is a rotation matrix comprising the scaling).

View File

@@ -34,12 +34,10 @@
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/split_member.hpp>
namespace uLib {
/**
* @brief Internal control block for shared ownership across polymorphic
* SmartPointers.
* @brief Internal control block for shared ownership across polymorphic SmartPointers.
*/
struct ControlBlock {
std::atomic<uint32_t> count;
@@ -51,12 +49,11 @@ private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/) {
// ControlBlock identity is tracked by Boost via the cb pointer in
// ReferenceCounter. We only save the count value.
// ControlBlock identity is tracked by Boost via the cb pointer in ReferenceCounter.
// We only save the count value.
uint32_t c = count.load();
ar & boost::serialization::make_nvp("count", c);
if constexpr (Archive::is_loading::value)
count.store(c);
if constexpr (Archive::is_loading::value) count.store(c);
}
};
@@ -102,17 +99,15 @@ public:
SmartPointer(std::nullptr_t) noexcept : m_counter(nullptr) {}
/**
* @brief Constructor from raw pointer (Implicit conversion allowed for legacy
* compatibility).
* @brief Constructor from raw pointer (Implicit conversion allowed for legacy compatibility).
*/
SmartPointer(T* ptr) : m_counter(nullptr) {
if (ptr)
m_counter = new ReferenceCounter(ptr);
if (ptr) m_counter = new ReferenceCounter(ptr);
}
template <typename D> SmartPointer(T *ptr, D deleter) : m_counter(nullptr) {
if (ptr)
m_counter = new ReferenceCounter(ptr, deleter);
template <typename D>
SmartPointer(T* ptr, D deleter) : m_counter(nullptr) {
if (ptr) m_counter = new ReferenceCounter(ptr, deleter);
}
SmartPointer(T &ref) : m_counter(new ReferenceCounter(&ref, [](T*){})) { }
@@ -122,31 +117,26 @@ public:
}
SmartPointer(const SmartPointer* other) noexcept : m_counter(nullptr) {
if (other)
acquire(other->m_counter);
if (other) acquire(other->m_counter);
}
template <typename U,
typename = std::enable_if_t<std::is_convertible_v<U *, T *>>>
template <typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
SmartPointer(const SmartPointer<U>& other) noexcept : m_counter(nullptr) {
if (other.m_counter) {
m_counter = new ReferenceCounter();
m_counter->ptr = static_cast<T*>(other.m_counter->ptr);
m_counter->cb = other.m_counter->cb;
if (m_counter->cb)
m_counter->cb->count.fetch_add(1, std::memory_order_relaxed);
if (m_counter->cb) m_counter->cb->count.fetch_add(1, std::memory_order_relaxed);
}
}
template <typename U>
SmartPointer(const SmartPointer<U> &other, T *ptr) noexcept
: m_counter(nullptr) {
SmartPointer(const SmartPointer<U>& other, T* ptr) noexcept : m_counter(nullptr) {
if (other.m_counter) {
m_counter = new ReferenceCounter();
m_counter->ptr = ptr;
m_counter->cb = other.m_counter->cb;
if (m_counter->cb)
m_counter->cb->count.fetch_add(1, std::memory_order_relaxed);
if (m_counter->cb) m_counter->cb->count.fetch_add(1, std::memory_order_relaxed);
}
}
@@ -180,8 +170,7 @@ public:
void reset(T* ptr = nullptr) {
release();
if (ptr)
m_counter = new ReferenceCounter(ptr);
if (ptr) m_counter = new ReferenceCounter(ptr);
}
void swap(SmartPointer& other) noexcept {
@@ -195,9 +184,7 @@ public:
operator T*() const noexcept { return get(); }
uint32_t use_count() const noexcept {
return (m_counter && m_counter->cb)
? m_counter->cb->count.load(std::memory_order_relaxed)
: 0;
return (m_counter && m_counter->cb) ? m_counter->cb->count.load(std::memory_order_relaxed) : 0;
}
bool unique() const noexcept { return use_count() == 1; }
@@ -230,17 +217,14 @@ private:
m_counter = new ReferenceCounter();
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);
if (m_counter->cb) m_counter->cb->count.fetch_add(1, std::memory_order_relaxed);
}
}
void release() noexcept {
if (m_counter) {
if (m_counter->cb &&
m_counter->cb->count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
if (m_counter->cb->deleter)
m_counter->cb->deleter();
if (m_counter->cb && m_counter->cb->count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
if (m_counter->cb->deleter) m_counter->cb->deleter();
delete m_counter->cb;
}
delete m_counter;
@@ -253,16 +237,18 @@ template <typename T, typename U>
SmartPointer<T> static_pointer_cast(const SmartPointer<U>& r) noexcept {
return SmartPointer<T>(r, static_cast<T*>(r.get()));
}
template <typename T, typename U>
SmartPointer<T> dynamic_pointer_cast(const SmartPointer<U>& r) noexcept {
if (auto p = dynamic_cast<T *>(r.get()))
return SmartPointer<T>(r, p);
if (auto p = dynamic_cast<T*>(r.get())) return SmartPointer<T>(r, p);
return SmartPointer<T>(nullptr);
}
template <typename T, typename U>
SmartPointer<T> const_pointer_cast(const SmartPointer<U>& r) noexcept {
return SmartPointer<T>(r, const_cast<T*>(r.get()));
}
template <typename T, typename U>
SmartPointer<T> reinterpret_pointer_cast(const SmartPointer<U>& r) noexcept {
return SmartPointer<T>(r, reinterpret_cast<T*>(r.get()));