I would like to use a pointer-like object
Ownership<Type> m_foo
for the owning object and handle
Reference<Type> m_someFoo
as a classical "pointer" in another context, whereas my Reference should know when the original object does not exist anymore (e.g. by returning nullptr) and it should furthermore be possible to prevent the original object from deletion for a small period of time (locking).
I know that shared_ptr (Ownership) and weak_ptr (Reference) provide similar functionality. However, locking a weak_ptr and accessing the raw ptr involves creation of a shared_ptr which is rather slow. Also I cannot decide not to lock the weak_ptr before accessing the raw ptr (e.g. while knowing that the object is not being deleted right now).
Is it wise to implement Ownership and Reference as follows:
- Ownership knows addresses of its References (e.g.
std::list<Reference<Type>*>
) - When the owned object decays, these References are set to nullptr
- Reference contains ptr to an
uint m_lockCount
in Ownership - upon locking,
m_lockCount++
, upon unlockingm_lockCount--
- Ownership can't release object when
m_lockCount != 0
This solution would be especially feasible for few Reference instances and high access rates through References.