Go to the documentation of this file.
11 #ifndef _GAUDI_CONTEXTSPECIFICPTR_H_
12 #define _GAUDI_CONTEXTSPECIFICPTR_H_
17 #include <type_traits>
18 #include <unordered_map>
64 operator bool()
const {
return isValid(); }
84 template <
class Mapper>
85 #if __cplusplus >= 201703L
86 auto accumulate( Mapper f, std::invoke_result_t<Mapper, const T*> init )
const -> decltype( init ) {
88 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init )
const -> decltype( init ) {
96 template <
class Mapper,
class BinaryOperation>
97 #if __cplusplus >= 201703L
98 auto accumulate( Mapper f, std::invoke_result_t<Mapper, const T*> init, BinaryOperation op )
const
100 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init, BinaryOperation op )
const
102 -> decltype( init ) {
105 return op( partial, f( p.second ) );
113 for (
auto& i :
m_ptrs ) f( i.second );
120 for (
auto& i :
m_ptrs ) f( i.second );
127 for (
auto& i :
m_ptrs ) f( i.first, i.second );
132 for (
auto& i :
m_ptrs ) f( i.first, i.second );
155 template <
typename T>
165 if ( !m_ptr ) m_ptr =
new T( m_proto );
169 operator T&()
const {
170 if ( !m_ptr ) m_ptr =
new T( m_proto );
175 T&
operator=(
const T& other ) {
return (T&)( *this ) = other; }
182 template <
class T1,
class BinaryOperation>
184 return m_ptr.accumulate( [](
const T* p ) {
return *p; }, init, op );
190 m_ptr.for_each( [&f](
const T* p ) { f( *p ); } );
196 m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
202 m_ptr.for_all( [&f](
size_t s,
const T* p ) { f(
s, *p ); } );
206 m_ptr.for_all( [&f](
size_t s, T* p ) { f(
s, *p ); } );
221 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
bool operator==(T *rhs) const
Comparison with another pointer.
void clear()
Non thread-safe methods.
T & operator*()
Dereference operators.
ContextSpecificData(T proto={})
Constructor with prototype value.
T accumulate(T init) const
Return the sum of all the contained values using init as first value.
T *& set(T *ptr)
Set the pointer for the current context.
ContextSpecificPtr< T > m_ptr
Internal implementation.
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
T1 accumulate(T1 init, BinaryOperation op) const
Return the accumulated result, through the operation 'op', of all the contained values using init as ...
T & operator=(const T &other)
Assignment operator.
T *& operator=(T *ptr)
Assignment operator (.
Simple implementation of a smart pointer with different values for different event contexts (slots).
void for_each(F f) const
Call a function on each contained value.
ContextSpecificData(const ContextSpecificData &)=delete
void for_all(F f) const
Call a function on each element, passing slot# as well.
StorageType m_ptrs
Internal storage for the different internal pointers.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
void for_each(F f) const
Call a function on each contained pointer.
T * get() const
Return the pointer for the current context (null for a new context).
void for_all(F f) const
Call a function on each element, passing slot# as well.
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
~ContextSpecificData()
Destructor.
void for_each(F f)
Call a function on each contained value. (non-const version)
const T * operator->() const
GAUDI_API ContextIdType currentContextId()
Return the current context id.
const T & operator*() const
Implementation of a context specific storage accessible as a sort of smart reference class.
bool isValid() const
Return true if the pointer is not null.
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,...
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...
void for_each(F f)
Call a function on each contained pointer. (non-const version)