1 #ifndef _GAUDI_CONTEXTSPECIFICPTR_H_ 2 #define _GAUDI_CONTEXTSPECIFICPTR_H_ 8 #include <unordered_map> 46 inline T*&
set( T* ptr )
53 inline T*&
operator=( T* ptr ) {
return set( ptr ); }
56 inline bool isValid()
const {
return get(); }
59 inline operator bool()
const {
return isValid(); }
62 inline bool operator==( T* rhs )
const {
return get() == rhs; }
66 inline const T&
operator*()
const {
return *
get(); }
79 template <
class Mapper>
80 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init ) const -> decltype( init )
88 template <
class Mapper,
class BinaryOperation>
89 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init, BinaryOperation op )
const 94 return op( partial, f( p.second ) );
103 for (
auto& i :
m_ptrs ) f( i.second );
111 for (
auto& i :
m_ptrs ) f( i.second );
119 for (
auto& i :
m_ptrs ) f( i.first, i.second );
125 for (
auto& i :
m_ptrs ) f( i.first, i.second );
149 template <
typename T>
161 if ( !m_ptr ) m_ptr =
new T( m_proto );
167 if ( !m_ptr ) m_ptr =
new T( m_proto );
172 T&
operator=(
const T& other ) {
return (T&)( *this ) = other; }
179 template <
class T1,
class BinaryOperation>
182 return m_ptr.accumulate( [](
const T* p ) {
return *p; }, init, op );
189 m_ptr.for_each( [&f](
const T* p ) { f( *p ); } );
196 m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
203 m_ptr.for_all( [&f](
size_t s,
const T* p ) { f( s, *p ); } );
208 m_ptr.for_all( [&f](
size_t s, T* p ) { f( s, *p ); } );
223 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
GAUDI_API ContextIdType currentContextId()
Return the current context id.
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_all(F f) const
Call a function on each element, passing slot# as well.
void clear()
Non thread-safe methods.
void for_each(F f)
Call a function on each contained pointer. (non-const version)
void for_all(F f) const
Call a function on each element, passing slot# as well.
T & operator*()
Dereference operators.
ContextSpecificData(T proto={})
Constructor with prototype value.
void for_each(F f) const
Call a function on each contained value.
bool isValid() const
Return true if the pointer is not null.
T & operator=(const T &other)
Assignment operator.
bool operator==(T *rhs) const
Comparison with another pointer.
This class represents an entry point to all the event specific data.
Simple implementation of a smart pointer with different values for different event contexts (slots)...
T *& operator=(T *ptr)
Assignment operator (.
void for_each(F f)
Call a function on each contained value. (non-const version)
ContextSpecificPtr< T > m_ptr
Internal implementation.
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
const T & operator*() const
void for_each(F f) const
Call a function on each contained pointer.
Implementation of a context specific storage accessible as a sort of smart reference class...
StorageType m_ptrs
Internal storage for the different internal pointers.
~ContextSpecificData()
Destructor.
T1 accumulate(T1 init, BinaryOperation op) const
Return the accumulated result, through the operation 'op', of all the contained values using init as ...
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
T accumulate(T init) const
Return the sum of all the contained values using init as first value.
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.
Helper functions to set/get the application return code.
const T * operator->() const