The Gaudi Framework  v30r3 (a5ef0a68)
ContextSpecificPtr.h
Go to the documentation of this file.
1 #ifndef _GAUDI_CONTEXTSPECIFICPTR_H_
2 #define _GAUDI_CONTEXTSPECIFICPTR_H_
3 
4 #include <functional>
5 #include <mutex>
6 #include <numeric>
7 #include <type_traits>
8 #include <unordered_map>
9 
10 // For the definition of GAUDI_API
11 #include "GaudiKernel/Kernel.h"
13 
14 class EventContext;
15 
16 namespace Gaudi
17 {
18  namespace Hive
19  {
31  template <typename T>
33  {
34  private:
37 
38  public:
40  inline T* get() const
41  {
43  return m_ptrs[currentContextId()];
44  }
46  inline T*& set( T* ptr )
47  {
49  return m_ptrs[currentContextId()] = ptr;
50  }
51 
53  inline T*& operator=( T* ptr ) { return set( ptr ); }
54 
56  inline bool isValid() const { return get(); }
57 
59  inline operator bool() const { return isValid(); }
60 
62  inline bool operator==( T* rhs ) const { return get() == rhs; }
63 
65  inline T& operator*() { return *get(); }
66  inline const T& operator*() const { return *get(); }
67  inline T* operator->() { return get(); }
68  inline const T* operator->() const { return get(); }
70 
72 
74  void clear() { m_ptrs.clear(); }
75 
79  template <class Mapper>
80  auto accumulate( Mapper f, std::result_of_t<Mapper( const T* )> init ) const -> decltype( init )
81  {
82  return accumulate( f, init, std::plus<>() );
83  }
84 
88  template <class Mapper, class BinaryOperation>
89  auto accumulate( Mapper f, std::result_of_t<Mapper( const T* )> init, BinaryOperation op ) const
90  -> decltype( init )
91  {
93  return std::accumulate( m_ptrs.begin(), m_ptrs.end(), init, [&f, &op]( const auto& partial, const auto& p ) {
94  return op( partial, f( p.second ) );
95  } );
96  }
97 
99  template <class F>
100  void for_each( F f ) const
101  {
103  for ( auto& i : m_ptrs ) f( i.second );
104  }
105 
107  template <class F>
108  void for_each( F f )
109  {
111  for ( auto& i : m_ptrs ) f( i.second );
112  }
113 
115  template <class F>
116  void for_all( F f ) const
117  {
119  for ( auto& i : m_ptrs ) f( i.first, i.second );
120  }
121  template <class F>
122  void for_all( F f )
123  {
125  for ( auto& i : m_ptrs ) f( i.first, i.second );
126  }
127 
128  void deleteAll()
129  {
130  for_each( []( T*& p ) {
131  delete p;
132  p = nullptr;
133  } );
134  }
135 
136  private:
138  mutable StorageType m_ptrs;
141  };
142 
149  template <typename T>
151  {
152  public:
154  ContextSpecificData( T proto = {} ) : m_proto( std::move( proto ) ) {}
155 
157  ~ContextSpecificData() { m_ptr.deleteAll(); }
158 
159  operator T&()
160  {
161  if ( !m_ptr ) m_ptr = new T( m_proto );
162  return *m_ptr;
163  }
164 
165  operator T&() const
166  {
167  if ( !m_ptr ) m_ptr = new T( m_proto );
168  return *m_ptr;
169  }
170 
172  T& operator=( const T& other ) { return (T&)( *this ) = other; }
173 
175  T accumulate( T init ) const { return accumulate( init, std::plus<>() ); }
176 
179  template <class T1, class BinaryOperation>
180  T1 accumulate( T1 init, BinaryOperation op ) const
181  {
182  return m_ptr.accumulate( []( const T* p ) { return *p; }, init, op );
183  }
184 
186  template <class F>
187  void for_each( F f ) const
188  {
189  m_ptr.for_each( [&f]( const T* p ) { f( *p ); } );
190  }
191 
193  template <class F>
194  void for_each( F f )
195  {
196  m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
197  }
198 
200  template <class F>
201  void for_all( F f ) const
202  {
203  m_ptr.for_all( [&f]( size_t s, const T* p ) { f( s, *p ); } );
204  }
205  template <class F>
206  void for_all( F f )
207  {
208  m_ptr.for_all( [&f]( size_t s, T* p ) { f( s, *p ); } );
209  }
210 
211  private:
212  // FIXME: implement a proper copy constructor
213  ContextSpecificData( const ContextSpecificData& ) = delete;
214 
216  T m_proto = {};
219  };
220  }
221 }
222 
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.
Definition: EventContext.h:24
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.
T clear(T...args)
T move(T...args)
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...
T begin(T...args)
StorageType m_ptrs
Internal storage for the different internal pointers.
string s
Definition: gaudirun.py:253
T1 accumulate(T1 init, BinaryOperation op) const
Return the accumulated result, through the operation &#39;op&#39;, 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.
T accumulate(T...args)
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 &#39;op&#39;, of all the values corresponding to the contained pointers using init as first value.
Helper functions to set/get the application return code.
Definition: __init__.py:1