This is an implementation of the ARC (Atomic Reference Counting) smart pointer in C++. The ARC smart pointer provides shared ownership of an object using reference counting. It allows multiple pointers to refer to the same object, and the object is deleted only when the last smart pointer referencing it goes out of scope.
Note: This implementation is my personal implementation and may not be 100% accurate or secure. It was developed as a learning exercise to understand the concepts and inner workings of reference counting pointers.
The ARC smart pointer is provided through two classes: Arc
and WeakArc
.
The Arc
class represents a strong reference to an object. It provides the following methods:
explicit Arc(T *ptr)
- Creates a new
Arc
instance that manages the given raw pointerptr
.
Arc(const Arc &other)
- Creates a new
Arc
instance as a copy of theother
Arc
instance. - Increments the reference count of the control block.
Arc &operator=(const Arc &other)
- Assigns the
other
Arc
instance to the currentArc
instance. - Decrements the reference count of the current control block and increments the reference count of the
other
control block. - Properly handles self-assignment.
~Arc()
- Decrements the reference count of the control block.
- Releases the control block if the reference count reaches zero.
T *get() const
- Returns the raw pointer to the managed object.
std::shared_mutex *mutex()
- Returns a pointer to the
shared_mutex
associated with the control block.
Arc clone() const
- Creates a new
Arc
instance as a clone of the currentArc
instance. - Acquires a shared lock on the control block's mutex.
template <typename U> friend U *get_mut(Arc<U> &arc)
- Allows mutable access to the managed object by acquiring a lock on the associated mutex.
The WeakArc
class represents a weak reference to an object managed by Arc
. It provides the following methods:
explicit WeakArc(Arc<T> &arc)
- Creates a new
WeakArc
instance from anArc
instance.
WeakArc(const WeakArc &other)
- Creates a new
WeakArc
instance as a copy of theother
WeakArc
instance. - Increments the reference count of the control block.
WeakArc &operator=(const WeakArc &other)
- Assigns the
other
WeakArc
instance to the currentWeakArc
instance. - Decrements the reference count of the current control block and increments the reference count of the
other
control block.
~WeakArc()
- Decrements the reference count of the control block.
- Releases the control block if the reference count reaches zero.
Arc<T> upgrade() const
- Upgrades the
WeakArc
to anArc
instance if the object still exists. - Returns the upgraded
Arc
instance ornullptr
if the object has been deleted.