11 #ifndef _GAUDI_CONTEXTSPECIFICPTR_H_ 12 #define _GAUDI_CONTEXTSPECIFICPTR_H_ 17 #include <type_traits> 18 #include <unordered_map> 47 inline T*
get()
const {
52 inline T*&
set( T* ptr ) {
64 inline operator bool()
const {
return isValid(); }
84 template <
class Mapper>
85 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init )
const -> decltype( init ) {
92 template <
class Mapper,
class BinaryOperation>
93 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init, BinaryOperation op )
const 97 return op( partial, f( p.second ) );
105 for (
auto& i :
m_ptrs ) f( i.second );
112 for (
auto& i :
m_ptrs ) f( i.second );
119 for (
auto& i :
m_ptrs ) f( i.first, i.second );
124 for (
auto& i :
m_ptrs ) f( i.first, i.second );
147 template <
typename T>
161 operator T&()
const {
167 T&
operator=(
const T& other ) {
return (T&)( *this ) = other; }
174 template <
class T1,
class BinaryOperation>
176 return m_ptr.accumulate( [](
const T* p ) {
return *p; }, init, op );
182 m_ptr.for_each( [&f](
const T* p ) { f( *p ); } );
188 m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
194 m_ptr.for_all( [&f](
size_t s,
const T* p ) { f(
s, *p ); } );
198 m_ptr.for_all( [&f](
size_t s, T* p ) { f(
s, *p ); } );
213 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
GAUDI_API ContextIdType currentContextId()
Return the current context id.
bool operator==(T *rhs) const
Comparison with another pointer.
void clear()
Non thread-safe methods.
void for_each(F f)
Call a function on each contained pointer. (non-const version)
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...
ContextSpecificData(T proto={})
Constructor with prototype value.
T *& set(T *ptr)
Set the pointer for the current context.
T & operator=(const T &other)
Assignment operator.
This class represents an entry point to all the event specific data.
T m_proto
Prototype value.
T & operator *()
Dereference operators.
Simple implementation of a smart pointer with different values for different event contexts (slots).
void for_all(F f) const
Call a function on each element, passing slot# as well.
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.
void for_all(F f) const
Call a function on each element, passing slot# as well.
T * get() const
Return the pointer for the current context (null for a new context).
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
T accumulate(T init) const
Return the sum of all the contained values using init as first value.
T1 accumulate(T1 init, BinaryOperation op) const
Return the accumulated result, through the operation 'op', of all the contained values using init as ...
const T * operator->() const
Implementation of a context specific storage accessible as a sort of smart reference class.
void for_each(F f) const
Call a function on each contained pointer.
StorageType m_ptrs
Internal storage for the different internal pointers.
void for_each(F f) const
Call a function on each contained value.
~ContextSpecificData()
Destructor.
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
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,...
bool isValid() const
Return true if the pointer is not null.
Header file for std:chrono::duration-based Counters.