ATLAS Offline Software
Public Member Functions | Private Attributes | List of all members
CxxUtils::CachedUniquePtrT< T > Class Template Reference

Cached pointer with atomic update. More...

#include <CachedUniquePtr.h>

Collaboration diagram for CxxUtils::CachedUniquePtrT< T >:

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...
 
CachedUniquePtrToperator= (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...
 

Detailed Description

template<class T>
class CxxUtils::CachedUniquePtrT< T >

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,

...
const Payload* ...::get() const
{
if (!m_payload) {
m_payload.set (std::make_unique<Payload> (...));
}
return m_payload.get());
}

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.

Constructor & Destructor Documentation

◆ CachedUniquePtrT() [1/3]

template<class T >
CxxUtils::CachedUniquePtrT< T >::CachedUniquePtrT ( )

Default constructor. Sets the element to null.

◆ CachedUniquePtrT() [2/3]

template<class T >
CxxUtils::CachedUniquePtrT< T >::CachedUniquePtrT ( std::unique_ptr< T >  elt)

Constructor from an element.

◆ CachedUniquePtrT() [3/3]

template<class T >
CxxUtils::CachedUniquePtrT< T >::CachedUniquePtrT ( CachedUniquePtrT< T > &&  other)
noexcept

Move constructor.

◆ ~CachedUniquePtrT()

template<class T >
CxxUtils::CachedUniquePtrT< T >::~CachedUniquePtrT ( )

Member Function Documentation

◆ get()

template<class T >
T* CxxUtils::CachedUniquePtrT< T >::get ( ) const

Return the current value of the element.

◆ operator bool()

template<class T >
CxxUtils::CachedUniquePtrT< T >::operator bool ( ) const
explicit

Test if the element is null.

◆ operator*()

template<class T >
T& CxxUtils::CachedUniquePtrT< T >::operator* ( ) const

Dereference the element.

◆ operator->()

template<class T >
T* CxxUtils::CachedUniquePtrT< T >::operator-> ( ) const

Dereference the element.

◆ operator=()

template<class T >
CachedUniquePtrT& CxxUtils::CachedUniquePtrT< T >::operator= ( CachedUniquePtrT< T > &&  other)
noexcept

Move.

◆ release()

template<class T >
std::unique_ptr<T> CxxUtils::CachedUniquePtrT< T >::release ( )
noexcept

Transfer ownership from the element: return the current value as a unique_ptr, leaving the element null.

◆ set()

template<class T >
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.

◆ store()

template<class T >
void CxxUtils::CachedUniquePtrT< T >::store ( std::unique_ptr< T >  elt)
noexcept

Store a new value to the element.

Not compatible with other concurrent access.

Member Data Documentation

◆ m_ptr

template<class T >
std::atomic<T*> CxxUtils::CachedUniquePtrT< T >::m_ptr
mutableprivate

The cached element.

Definition at line 109 of file CachedUniquePtr.h.


The documentation for this class was generated from the following file:
CxxUtils::CachedUniquePtrT
Cached pointer with atomic update.
Definition: CachedUniquePtr.h:54
CxxUtils::CachedUniquePtrT::set
T * set(std::unique_ptr< T > elt) const
Atomically set the element.
CxxUtils::CachedUniquePtrT::get
T * get() const
Return the current value of the element.