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

Cached value with atomic update. More...

#include <CachedValue.h>

Collaboration diagram for CxxUtils::CachedValue< T >:

Public Member Functions

 CachedValue ()
 Default constructor. Sets the value to invalid. More...
 
 CachedValue (const T &val)
 Constructor from a value. More...
 
 CachedValue (T &&val) noexcept
 Move constructor from a value. More...
 
 CachedValue (const CachedValue &other)
 Copy constructor. More...
 
 CachedValue (CachedValue &&other) noexcept
 Move constructor. More...
 
template<class U >
 CachedValue (const CachedValue< U > &other)
 Copy constructor from other type. More...
 
CachedValueoperator= (const CachedValue &other)
 Assignment. More...
 
CachedValueoperator= (CachedValue &&other) noexcept
 Move. More...
 
void set (const T &val) const
 Set the value, assuming it is currently invalid. More...
 
void set (T &&val) const noexcept
 Set the value by move, assuming it is currently invalid. More...
 
bool isValid () const
 Test to see if the value is valid. More...
 
const T * ptr () const
 Return a pointer to the cached value. More...
 
void store (const T &val)
 Store a new value. More...
 
void store (T &&val) noexcept
 Store a new value, by move. More...
 
void reset ()
 Reset the value to invalid. More...
 

Private Attributes

T m_val ATLAS_THREAD_SAFE
 The cached value. More...
 
std::atomic< CacheStatem_cacheValid
 Transient. More...
 

Friends

template<class U >
class CachedValue
 

Detailed Description

template<class T>
class CxxUtils::CachedValue< T >

Cached value with atomic update.

Hold a cache of some value. The cache may be set atomically through a const method. This can happen in multiple threads, but the value set should be the same in all threads. The value can be either valid or invalid; it can be set using the set() method. (The value can also be set using the non-const store() method; however, this is not atomic and should not be done while other threads may be accessing the value.) The validity of the value may be tested with isValid(), and ptr() will get back a pointer to the value. ptr() should not be called until isValid() has returned true or set() returned.

The state of the cached value is given by m_cacheState, which can have the values: INVALID: No value has been cached. VALID: The cached value is valid. UPDATING: Some thread is in the process of setting the value.

Definition at line 54 of file CachedValue.h.

Constructor & Destructor Documentation

◆ CachedValue() [1/6]

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

Default constructor. Sets the value to invalid.

◆ CachedValue() [2/6]

template<class T >
CxxUtils::CachedValue< T >::CachedValue ( const T &  val)

Constructor from a value.

◆ CachedValue() [3/6]

template<class T >
CxxUtils::CachedValue< T >::CachedValue ( T &&  val)
noexcept

Move constructor from a value.

◆ CachedValue() [4/6]

template<class T >
CxxUtils::CachedValue< T >::CachedValue ( const CachedValue< T > &  other)

Copy constructor.

◆ CachedValue() [5/6]

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

Move constructor.

No concurrent operations on OTHER are possible.

◆ CachedValue() [6/6]

template<class T >
template<class U >
CxxUtils::CachedValue< T >::CachedValue ( const CachedValue< U > &  other)

Copy constructor from other type.

Member Function Documentation

◆ isValid()

template<class T >
bool CxxUtils::CachedValue< T >::isValid ( ) const

Test to see if the value is valid.

May spin if another thread is currently updating the value.

◆ operator=() [1/2]

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

Move.

No concurrent operations on OTHER are possible.

◆ operator=() [2/2]

template<class T >
CachedValue& CxxUtils::CachedValue< T >::operator= ( const CachedValue< T > &  other)

Assignment.

◆ ptr()

template<class T >
const T* CxxUtils::CachedValue< T >::ptr ( ) const

Return a pointer to the cached value.

Should not be called unless isValid() has returned true or set() has returned.

◆ reset()

template<class T >
void CxxUtils::CachedValue< T >::reset ( )

Reset the value to invalid.

◆ set() [1/2]

template<class T >
void CxxUtils::CachedValue< T >::set ( const T &  val) const

Set the value, assuming it is currently invalid.

Otherwise, this method will do nothing. The value will be valid once this returns.

◆ set() [2/2]

template<class T >
void CxxUtils::CachedValue< T >::set ( T &&  val) const
noexcept

Set the value by move, assuming it is currently invalid.

Otherwise, this method will do nothing. The value will be valid once this returns.

◆ store() [1/2]

template<class T >
void CxxUtils::CachedValue< T >::store ( const T &  val)

Store a new value.

◆ store() [2/2]

template<class T >
void CxxUtils::CachedValue< T >::store ( T &&  val)
noexcept

Store a new value, by move.

Friends And Related Function Documentation

◆ CachedValue

template<class T >
template<class U >
friend class CachedValue
friend

Definition at line 58 of file CachedValue.h.

Member Data Documentation

◆ ATLAS_THREAD_SAFE

template<class T >
T m_val CxxUtils::CachedValue< T >::ATLAS_THREAD_SAFE
mutableprivate

The cached value.

Do not return a pointer to this to the user unless the state is VALID.

Definition at line 134 of file CachedValue.h.

◆ m_cacheValid

template<class T >
std::atomic<CacheState> CxxUtils::CachedValue< T >::m_cacheValid
mutableprivate

Transient.

Current state of the cached value. INVALID — value has not been set. VALID — value has been set. UPDATING — value is in the process of being set by some thread.

Definition at line 140 of file CachedValue.h.


The documentation for this class was generated from the following file: