| The Gaudi Framework v32r1 (f65d50dc)

Public Member Functions | Private Types | List of all members
Gaudi::Hive::ContextSpecificPtr< T > Class Template Reference

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

`#include <GaudiKernel/ContextSpecificPtr.h>`

Collaboration diagram for Gaudi::Hive::ContextSpecificPtr< T >:
[legend]

## Public Member Functions
T * | get () const
| Return the pointer for the current context (null for a new context). More...
T *& | set (T *ptr)
| Set the pointer for the current context. More...
T *& | operator= (T *ptr)
| Assignment operator (. More...
bool | isValid () const
| Return true if the pointer is not null. More...
| operator bool () const
| Conversion to boolean (. More...
bool | operator== (T *rhs) const
| Comparison with another pointer. More...
T & | operator * ()
| Dereference operators. More...
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. More...
StorageType | m_ptrs
| Internal storage for the different internal pointers. More...
std::mutex | m_ptrs_lock
| Mutex for the m_ptrs container. More...
void | clear ()
| Non thread-safe methods. More...
template<class Mapper >
auto | accumulate (Mapper f, std::result_of_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. More...
template<class Mapper , class BinaryOperation >
auto | accumulate (Mapper f, std::result_of_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. More...
template<class F >
void | for_each (F f) const
| Call a function on each contained pointer. More...
template<class F >
void | for_each (F f)
| Call a function on each contained pointer. (non-const version) More...
template<class F >
void | for_all (F f) const
| Call a function on each element, passing slot# as well. More...
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 30 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 33 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::result_of_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 75 of file ContextSpecificPtr.h.

75 {
76 return accumulate( f, init, std::plus<>() );
77 }
std::plus
Gaudi::Hive::ContextSpecificPtr::accumulate
auto accumulate(Mapper f, std::result_of_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...
Definition: ContextSpecificPtr.h:75

## ◆ accumulate() [2/2]

template<typename T>
template<class Mapper , class BinaryOperation >

auto Gaudi::Hive::ContextSpecificPtr< T >::accumulate | ( | Mapper | f,
|  | std::result_of_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 83 of file ContextSpecificPtr.h.

84 {
85 std::lock_guard<std::mutex> lock( m_ptrs_lock );
86 return std::accumulate( m_ptrs.begin(), m_ptrs.end(), init, [&f, &op]( const auto& partial, const auto& p ) {
87 return op( partial, f( p.second ) );
88 } );
89 }
std::unordered_map::end
T end(T... args)
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
std::unordered_map::begin
T begin(T... args)
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126
std::accumulate
T accumulate(T... args)

## ◆ 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 69 of file ContextSpecificPtr.h.

69 { m_ptrs.clear(); }
std::unordered_map::clear
T clear(T... args)
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## ◆ deleteAll()

template<typename T>

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

Definition at line 117 of file ContextSpecificPtr.h.

117 {
118 for_each( []( T*& p ) {
119 delete p;
120 p = nullptr;
121 } );
122 }
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f) const
Call a function on each contained pointer.
Definition: ContextSpecificPtr.h:93

## ◆ for_all() [1/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 107 of file ContextSpecificPtr.h.

107 {
108 std::lock_guard<std::mutex> lock( m_ptrs_lock );
109 for ( auto& i : m_ptrs ) f( i.first, i.second );
110 }
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## ◆ for_all() [2/2]

template<typename T>
template<class F >

void Gaudi::Hive::ContextSpecificPtr< T >::for_all | ( | F | f | ) |
| inline

Definition at line 112 of file ContextSpecificPtr.h.

112 {
113 std::lock_guard<std::mutex> lock( m_ptrs_lock );
114 for ( auto& i : m_ptrs ) f( i.first, i.second );
115 }
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## ◆ for_each() [1/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 93 of file ContextSpecificPtr.h.

93 {
94 std::lock_guard<std::mutex> lock( m_ptrs_lock );
95 for ( auto& i : m_ptrs ) f( i.second );
96 }
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## ◆ for_each() [2/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 100 of file ContextSpecificPtr.h.

100 {
101 std::lock_guard<std::mutex> lock( m_ptrs_lock );
102 for ( auto& i : m_ptrs ) f( i.second );
103 }
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## ◆ 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 37 of file ContextSpecificPtr.h.

37 {
38 std::lock_guard<std::mutex> lock( m_ptrs_lock );
39 return m_ptrs[currentContextId()];
40 }
Gaudi::Hive::currentContextId
GAUDI_API ContextIdType currentContextId()
Return the current context id.
Definition: ThreadLocalContext.cpp:18
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## ◆ isValid()

template<typename T>

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

Return true if the pointer is not null.

Definition at line 51 of file ContextSpecificPtr.h.

51 { return get(); }
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37

## ◆ operator *() [1/2]

template<typename T>

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

Dereference operators.

Definition at line 60 of file ContextSpecificPtr.h.

60 { return *get(); }
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37

## ◆ operator *() [2/2]

template<typename T>

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

Definition at line 61 of file ContextSpecificPtr.h.

61 { return *get(); }
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37

## ◆ operator bool()

template<typename T>

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

Conversion to boolean (.

See also
  isValid).

Definition at line 54 of file ContextSpecificPtr.h.

54 { return isValid(); }
Gaudi::Hive::ContextSpecificPtr::isValid
bool isValid() const
Return true if the pointer is not null.
Definition: ContextSpecificPtr.h:51

## ◆ operator->() [1/2]

template<typename T>

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

Definition at line 62 of file ContextSpecificPtr.h.

62 { return get(); }
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37

## ◆ operator->() [2/2]

template<typename T>

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

Definition at line 63 of file ContextSpecificPtr.h.

63 { return get(); }
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37

## ◆ operator=()

template<typename T>

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

Assignment operator (.

See also
  set).

Definition at line 48 of file ContextSpecificPtr.h.

48 { return set( ptr ); }
Gaudi::Hive::ContextSpecificPtr::set
T *& set(T *ptr)
Set the pointer for the current context.
Definition: ContextSpecificPtr.h:42

## ◆ operator==()

template<typename T>

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

Comparison with another pointer.

Definition at line 57 of file ContextSpecificPtr.h.

57 { return get() == rhs; }
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37

## ◆ set()

template<typename T>

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

Set the pointer for the current context.

Definition at line 42 of file ContextSpecificPtr.h.

42 {
43 std::lock_guard<std::mutex> lock( m_ptrs_lock );
44 return m_ptrs[currentContextId()] = ptr;
45 }
Gaudi::Hive::currentContextId
GAUDI_API ContextIdType currentContextId()
Return the current context id.
Definition: ThreadLocalContext.cpp:18
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
std::lock
T lock(T... args)
std::lock_guard
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126

## Member Data Documentation

## ◆ m_ptrs

template<typename T>

StorageType Gaudi::Hive::ContextSpecificPtr< T >::m_ptrs
| mutable private

Internal storage for the different internal pointers.

Definition at line 126 of file ContextSpecificPtr.h.

## ◆ m_ptrs_lock

template<typename T>

std::mutex Gaudi::Hive::ContextSpecificPtr< T >::m_ptrs_lock
| mutable private

Mutex for the m_ptrs container.

Definition at line 128 of file ContextSpecificPtr.h.

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

- GaudiKernel/GaudiKernel/ContextSpecificPtr.h