1 #ifndef _GAUDI_CONTEXTSPECIFICPTR_H_ 2 #define _GAUDI_CONTEXTSPECIFICPTR_H_ 8 #include <unordered_map> 37 inline T*
get()
const {
42 inline T*&
set( T* ptr ) {
54 inline operator bool()
const {
return isValid(); }
74 template <
class Mapper>
75 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init )
const -> decltype( init ) {
82 template <
class Mapper,
class BinaryOperation>
83 auto accumulate( Mapper f, std::result_of_t<Mapper(
const T* )> init, BinaryOperation op )
const 87 return op( partial, f( p.second ) );
95 for (
auto& i :
m_ptrs ) f( i.second );
102 for (
auto& i :
m_ptrs ) f( i.second );
109 for (
auto& i :
m_ptrs ) f( i.first, i.second );
114 for (
auto& i :
m_ptrs ) f( i.first, i.second );
137 template <
typename T>
151 operator T&()
const {
157 T&
operator=(
const T& other ) {
return (T&)( *this ) = other; }
164 template <
class T1,
class BinaryOperation>
166 return m_ptr.accumulate( [](
const T* p ) {
return *p; }, init, op );
172 m_ptr.for_each( [&f](
const T* p ) { f( *p ); } );
178 m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
184 m_ptr.for_all( [&f](
size_t s,
const T* p ) { f(
s, *p ); } );
188 m_ptr.for_all( [&f](
size_t s, T* p ) { f(
s, *p ); } );
203 #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.