ATLAS Offline Software
|
Cached pointer with atomic update. More...
#include <CachedUniquePtr.h>
Public Member Functions | |
CachedUniquePtrT () | |
Default constructor. Sets the element to null. More... | |
CachedUniquePtrT (std::unique_ptr< T > elt) | |
Constructor from an element. More... | |
CachedUniquePtrT (CachedUniquePtrT &&other) noexcept | |
Move constructor. More... | |
CachedUniquePtrT & | operator= (CachedUniquePtrT &&other) noexcept |
Move. More... | |
~CachedUniquePtrT () | |
T * | set (std::unique_ptr< T > elt) const |
Atomically set the element. More... | |
void | store (std::unique_ptr< T > elt) noexcept |
Store a new value to the element. More... | |
T * | get () const |
Return the current value of the element. More... | |
T & | operator* () const |
Dereference the element. More... | |
T * | operator-> () const |
Dereference the element. More... | |
operator bool () const | |
Test if the element is null. More... | |
std::unique_ptr< T > | release () noexcept |
Transfer ownership from the element: return the current value as a unique_ptr, leaving the element null. More... | |
Private Attributes | |
std::atomic< T * > | m_ptr |
The cached element. More... | |
Cached pointer with atomic update.
This acts as a unique_ptr
that may be set atomically through a const method. If the pointer is already set, then the new value is deleted. The intended use case is where one maintains some cached object that is computed lazily. So one can do, for example,
This only makes sense if the objects passed to set() are equivalent in all threads. This is generally the case for a lazily-computed value, but this class has no way of enforcing this.
It is recommended to generally use the template CachedUniquePtr
, which is specialized for const types. The more general template CachedUniquePtrT
can be used with a non-const type (but you don't want to do that for the lazy-cache use case).
Definition at line 53 of file CachedUniquePtr.h.
CxxUtils::CachedUniquePtrT< T >::CachedUniquePtrT | ( | ) |
Default constructor. Sets the element to null.
CxxUtils::CachedUniquePtrT< T >::CachedUniquePtrT | ( | std::unique_ptr< T > | elt | ) |
Constructor from an element.
|
noexcept |
Move constructor.
CxxUtils::CachedUniquePtrT< T >::~CachedUniquePtrT | ( | ) |
T* CxxUtils::CachedUniquePtrT< T >::get | ( | ) | const |
Return the current value of the element.
|
explicit |
Test if the element is null.
T& CxxUtils::CachedUniquePtrT< T >::operator* | ( | ) | const |
Dereference the element.
T* CxxUtils::CachedUniquePtrT< T >::operator-> | ( | ) | const |
Dereference the element.
|
noexcept |
Move.
|
noexcept |
Transfer ownership from the element: return the current value as a unique_ptr, leaving the element null.
T* CxxUtils::CachedUniquePtrT< T >::set | ( | std::unique_ptr< T > | elt | ) | const |
Atomically set the element.
If already set, then elt
is discarded. Returns the final value of the element.
|
noexcept |
Store a new value to the element.
Not compatible with other concurrent access.
|
mutableprivate |
The cached element.
Definition at line 109 of file CachedUniquePtr.h.