|
casacore
|
A smart pointer class that allows to work with complex cycles without using weak_ptrs. More...
#include <CyclicPtr.h>
Classes | |
| struct | Data |
Public Member Functions | |
| CyclicPtr ()=default | |
| CyclicPtr (std::nullptr_t) | |
| CyclicPtr (T *object) | |
| CyclicPtr (const CyclicPtr &source) | |
| CyclicPtr (CyclicPtr &&source) | |
| ~CyclicPtr () | |
| T * | Get () const |
| void | Reset () |
| CyclicPtr & | operator= (const CyclicPtr &other) |
| CyclicPtr & | operator= (CyclicPtr &&other) |
| operator bool () const | |
| T & | operator* () const |
| T * | operator-> () const |
| CyclicState | Freeze () const |
| Freeze the reference counter. | |
| void | Unfreeze (const CyclicState &frozen_state) |
| Continue 'normal' reference counting. | |
Private Member Functions | |
| void | Increase () |
| void | Decrease () |
Private Attributes | |
| std::shared_ptr< Data > | data_ |
Friends | |
| bool | operator== (const CyclicPtr< T > &lhs, const CyclicPtr< T > &rhs) |
| bool | operator!= (const CyclicPtr< T > &lhs, const CyclicPtr< T > &rhs) |
A smart pointer class that allows to work with complex cycles without using weak_ptrs.
Instead, the pointer needs to be manually frozen during the creation of cycles, such that cycle-links are not counted.
This class was specifically written for the MeasFrame class, which can have cycles via various routes. Originally, the FrameRep class held a counter and was destructed when the counter reached zero. While this worked, it had issues:
Having a separate class that also holds the counter avoids these problems. Introducing weak_ptrs to break the cycles in the measures code has been tried, but probably requires major changes in the structure.
This is an example of how to use the class:
struct Node { CyclicPtr<Node> link; };
CyclicPtr<Node> node = MakeCyclic<Node>();
const CyclicState state = node.Freeze();
node->link = node;
node.Unfreeze(state);
By freezing the state when creating the link, the link is not counted as a reference. As soon as either node->link or node is reset or goes out of scope, the Node object is destroyed and the other CyclicPtr will become nullptr.
This class implements the counter in a thread-safe way, such that the pointer can be copied around in different threads as if it is a normal pointer. Freeze() and Unfreeze() are not thread safe. In the MeasFrame class, these functions are only used during initialization or write-to actions of the MeasFrame, which remains valid.
It should be clear that this is a dangerous class to use correctly.
Definition at line 78 of file CyclicPtr.h.
|
default |
Referenced by CyclicPtr(), CyclicPtr(), operator!=, operator=(), operator=(), and operator==.
|
inline |
Definition at line 83 of file CyclicPtr.h.
|
inlineexplicit |
Definition at line 85 of file CyclicPtr.h.
References data_.
|
inline |
Definition at line 88 of file CyclicPtr.h.
References CyclicPtr(), data_, and Increase().
|
inline |
Definition at line 93 of file CyclicPtr.h.
References CyclicPtr(), data_, and casacore::move().
|
inline |
Definition at line 96 of file CyclicPtr.h.
References Decrease().
|
inlineprivate |
Definition at line 171 of file CyclicPtr.h.
References data_.
Referenced by operator=(), operator=(), Reset(), and ~CyclicPtr().
|
inlinenodiscard |
Freeze the reference counter.
The object is guaranteed not to be destructed until Unfreeze() is called.
The caller is responsible for calling Unfreeze(); letting CyclicState go out of scope without unfreezing will not cause an error, so will lead to an undetected state error. While it would be possible to detect this, it would increase the size of CyclicState.
Definition at line 144 of file CyclicPtr.h.
References data_.
|
inline |
Definition at line 100 of file CyclicPtr.h.
References data_.
|
inlineprivate |
Definition at line 165 of file CyclicPtr.h.
References data_.
Referenced by CyclicPtr(), and operator=().
|
inlineexplicit |
Definition at line 122 of file CyclicPtr.h.
References data_.
|
inline |
Definition at line 124 of file CyclicPtr.h.
References data_.
|
inline |
Definition at line 125 of file CyclicPtr.h.
References data_.
|
inline |
Definition at line 109 of file CyclicPtr.h.
References CyclicPtr(), data_, Decrease(), and Increase().
|
inline |
Definition at line 116 of file CyclicPtr.h.
References CyclicPtr(), data_, and Decrease().
|
inline |
Definition at line 104 of file CyclicPtr.h.
References data_, and Decrease().
|
inline |
Continue 'normal' reference counting.
Definition at line 151 of file CyclicPtr.h.
References data_, and casacore::details::CyclicState::value_.
|
friend |
Definition at line 131 of file CyclicPtr.h.
References CyclicPtr(), and data_.
|
friend |
Definition at line 127 of file CyclicPtr.h.
References CyclicPtr(), and data_.
|
private |
Definition at line 181 of file CyclicPtr.h.
Referenced by CyclicPtr(), CyclicPtr(), CyclicPtr(), Decrease(), Freeze(), Get(), Increase(), operator bool(), operator!=, operator*(), operator->(), operator=(), operator=(), operator==, Reset(), and Unfreeze().