The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
Gaudi::Hive::ContextSpecificPtr< T > Class Template Reference

Simple implementation of a smart pointer with different values for different event contexts (slots). More...

#include </builds/gaudi/Gaudi/GaudiKernel/include/GaudiKernel/ContextSpecificPtr.h>

Collaboration diagram for Gaudi::Hive::ContextSpecificPtr< T >:

Public Member Functions

T * get () const
 Return the pointer for the current context (null for a new context).
 
T *& set (T *ptr)
 Set the pointer for the current context.
 
T *& operator= (T *ptr)
 Assignment operator (.
 
bool isValid () const
 Return true if the pointer is not null.
 
 operator bool () const
 Conversion to boolean (.
 
bool operator== (T *rhs) const
 Comparison with another pointer.
 
T & operator* ()
 Dereference operators.
 
const T & operator* () const
 
T * operator-> ()
 
const T * operator-> () const
 

Private Types

typedef std::unordered_map< ContextIdType, T * > StorageType
 Type used for the internal storage.
 
StorageType m_ptrs
 Internal storage for the different internal pointers.
 
std::mutex m_ptrs_lock
 Mutex for the m_ptrs container.
 
void clear ()
 Non thread-safe methods.
 
template<class Mapper>
auto accumulate (Mapper f, std::invoke_result_t< Mapper, const T * > init) const -> decltype(init)
 Taking a function f that from a T* produces a value, return the sum of all the values corresponding to the contained pointers using init as first value.
 
template<class Mapper, class BinaryOperation>
auto accumulate (Mapper f, std::invoke_result_t< Mapper, const T * > init, BinaryOperation op) const -> decltype(init)
 Taking a function f that from a T* produces a value, return the accumulated result, through the operation 'op', of all the values corresponding to the contained pointers using init as first value.
 
template<class F>
void for_each (F f) const
 Call a function on each contained pointer.
 
template<class F>
void for_each (F f)
 Call a function on each contained pointer. (non-const version)
 
template<class F>
void for_all (F f) const
 Call a function on each element, passing slot# as well.
 
template<class F>
void for_all (F f)
 
void deleteAll ()
 

Detailed Description

template<typename T>
class Gaudi::Hive::ContextSpecificPtr< T >

Simple implementation of a smart pointer with different values for different event contexts (slots).

When the copy for a new context is requested, the returned pointer is null.

The interface is meant to allow for a drop-in replacement for regular pointers. It's still responsibility of the user to delete the memory associated to the pointers.

Definition at line 39 of file ContextSpecificPtr.h.

Member Typedef Documentation

◆ StorageType

template<typename T>
typedef std::unordered_map<ContextIdType, T*> Gaudi::Hive::ContextSpecificPtr< T >::StorageType
private

Type used for the internal storage.

Definition at line 42 of file ContextSpecificPtr.h.

Member Function Documentation

◆ accumulate() [1/2]

template<typename T>
template<class Mapper>
auto Gaudi::Hive::ContextSpecificPtr< T >::accumulate ( Mapper f,
std::invoke_result_t< Mapper, const T * > init ) const -> decltype( init )
inline

Taking a function f that from a T* produces a value, return the sum of all the values corresponding to the contained pointers using init as first value.

Definition at line 84 of file ContextSpecificPtr.h.

84 {
85 return accumulate( f, init, std::plus<>() );
86 }
Simple implementation of a smart pointer with different values for different event contexts (slots).
auto accumulate(Mapper f, std::invoke_result_t< Mapper, const T * > init) const -> decltype(init)
Taking a function f that from a T* produces a value, return the sum of all the values corresponding t...

◆ accumulate() [2/2]

template<typename T>
template<class Mapper, class BinaryOperation>
auto Gaudi::Hive::ContextSpecificPtr< T >::accumulate ( Mapper f,
std::invoke_result_t< Mapper, const T * > init,
BinaryOperation op ) const -> decltype( init )
inline

Taking a function f that from a T* produces a value, return the accumulated result, through the operation 'op', of all the values corresponding to the contained pointers using init as first value.

Definition at line 92 of file ContextSpecificPtr.h.

93 {
95 return std::accumulate( m_ptrs.begin(), m_ptrs.end(), init, [&f, &op]( const auto& partial, const auto& p ) {
96 return op( partial, f( p.second ) );
97 } );
98 }
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
StorageType m_ptrs
Internal storage for the different internal pointers.

◆ clear()

template<typename T>
void Gaudi::Hive::ContextSpecificPtr< T >::clear ( )
inline

Non thread-safe methods.

Set to null all the used pointers.

Definition at line 78 of file ContextSpecificPtr.h.

78{ m_ptrs.clear(); }

◆ deleteAll()

template<typename T>
void Gaudi::Hive::ContextSpecificPtr< T >::deleteAll ( )
inline

Definition at line 126 of file ContextSpecificPtr.h.

126 {
127 for_each( []( T*& p ) {
128 delete p;
129 p = nullptr;
130 } );
131 }
void for_each(F f) const
Call a function on each contained pointer.

◆ for_all() [1/2]

template<typename T>
template<class F>
void Gaudi::Hive::ContextSpecificPtr< T >::for_all ( F f)
inline

Definition at line 121 of file ContextSpecificPtr.h.

121 {
123 for ( auto& i : m_ptrs ) f( i.first, i.second );
124 }

◆ for_all() [2/2]

template<typename T>
template<class F>
void Gaudi::Hive::ContextSpecificPtr< T >::for_all ( F f) const
inline

Call a function on each element, passing slot# as well.

Definition at line 116 of file ContextSpecificPtr.h.

116 {
118 for ( auto& i : m_ptrs ) f( i.first, i.second );
119 }

◆ for_each() [1/2]

template<typename T>
template<class F>
void Gaudi::Hive::ContextSpecificPtr< T >::for_each ( F f)
inline

Call a function on each contained pointer. (non-const version)

Definition at line 109 of file ContextSpecificPtr.h.

109 {
111 for ( auto& i : m_ptrs ) f( i.second );
112 }

◆ for_each() [2/2]

template<typename T>
template<class F>
void Gaudi::Hive::ContextSpecificPtr< T >::for_each ( F f) const
inline

Call a function on each contained pointer.

Definition at line 102 of file ContextSpecificPtr.h.

102 {
104 for ( auto& i : m_ptrs ) f( i.second );
105 }

◆ get()

template<typename T>
T * Gaudi::Hive::ContextSpecificPtr< T >::get ( ) const
inline

Return the pointer for the current context (null for a new context).

Definition at line 46 of file ContextSpecificPtr.h.

46 {
48 return m_ptrs[currentContextId()];
49 }
GAUDI_API ContextIdType currentContextId()
Return the current context id.

◆ isValid()

template<typename T>
bool Gaudi::Hive::ContextSpecificPtr< T >::isValid ( ) const
inline

Return true if the pointer is not null.

Definition at line 60 of file ContextSpecificPtr.h.

60{ return get(); }
T * get() const
Return the pointer for the current context (null for a new context).

◆ operator bool()

template<typename T>
Gaudi::Hive::ContextSpecificPtr< T >::operator bool ( ) const
inline

Conversion to boolean (.

See also
isValid).

Definition at line 63 of file ContextSpecificPtr.h.

63{ return isValid(); }
bool isValid() const
Return true if the pointer is not null.

◆ operator*() [1/2]

template<typename T>
T & Gaudi::Hive::ContextSpecificPtr< T >::operator* ( )
inline

Dereference operators.

Definition at line 69 of file ContextSpecificPtr.h.

69{ return *get(); }

◆ operator*() [2/2]

template<typename T>
const T & Gaudi::Hive::ContextSpecificPtr< T >::operator* ( ) const
inline

Definition at line 70 of file ContextSpecificPtr.h.

70{ return *get(); }

◆ operator->() [1/2]

template<typename T>
T * Gaudi::Hive::ContextSpecificPtr< T >::operator-> ( )
inline

Definition at line 71 of file ContextSpecificPtr.h.

71{ return get(); }

◆ operator->() [2/2]

template<typename T>
const T * Gaudi::Hive::ContextSpecificPtr< T >::operator-> ( ) const
inline

Definition at line 72 of file ContextSpecificPtr.h.

72{ return get(); }

◆ operator=()

template<typename T>
T *& Gaudi::Hive::ContextSpecificPtr< T >::operator= ( T * ptr)
inline

Assignment operator (.

See also
set).

Definition at line 57 of file ContextSpecificPtr.h.

57{ return set( ptr ); }
T *& set(T *ptr)
Set the pointer for the current context.

◆ operator==()

template<typename T>
bool Gaudi::Hive::ContextSpecificPtr< T >::operator== ( T * rhs) const
inline

Comparison with another pointer.

Definition at line 66 of file ContextSpecificPtr.h.

66{ return get() == rhs; }

◆ set()

template<typename T>
T *& Gaudi::Hive::ContextSpecificPtr< T >::set ( T * ptr)
inline

Set the pointer for the current context.

Definition at line 51 of file ContextSpecificPtr.h.

51 {
53 return m_ptrs[currentContextId()] = ptr;
54 }

Member Data Documentation

◆ m_ptrs

template<typename T>
StorageType Gaudi::Hive::ContextSpecificPtr< T >::m_ptrs
mutableprivate

Internal storage for the different internal pointers.

Definition at line 135 of file ContextSpecificPtr.h.

◆ m_ptrs_lock

template<typename T>
std::mutex Gaudi::Hive::ContextSpecificPtr< T >::m_ptrs_lock
mutableprivate

Mutex for the m_ptrs container.

Definition at line 137 of file ContextSpecificPtr.h.


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