The Gaudi Framework  v29r0 (ff2e7097)
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>
81  accumulate( Mapper f, typename std::result_of<Mapper( const T* )>::type init ) const
82  {
84  return accumulate( f, init, std::plus<R>() );
85  }
86 
90  template <class Mapper, class BinaryOperation>
92  accumulate( Mapper f, typename std::result_of<Mapper( const T* )>::type init, BinaryOperation op ) const
93  {
95  typedef typename StorageType::value_type V;
97  return std::accumulate( m_ptrs.begin(), m_ptrs.end(), init, [&f, &op]( const R& partial, const V& p ) -> R {
98  return op( partial, f( p.second ) );
99  } );
100  }
101 
103  template <class F>
104  void for_each( F f ) const
105  {
107  for ( auto& i : m_ptrs ) f( i.second );
108  }
109 
111  template <class F>
112  void for_each( F f )
113  {
115  for ( auto& i : m_ptrs ) f( i.second );
116  }
117 
119  template <class F>
120  void for_all( F f ) const
121  {
123  for ( auto& i : m_ptrs ) f( i.first, i.second );
124  }
125  template <class F>
126  void for_all( F f )
127  {
129  for ( auto& i : m_ptrs ) f( i.first, i.second );
130  }
131 
132  void deleteAll()
133  {
134  for_each( []( T*& p ) {
135  delete p;
136  p = nullptr;
137  } );
138  }
139 
140  private:
142  mutable StorageType m_ptrs;
145  };
146 
153  template <typename T>
155  {
156  public:
158  ContextSpecificData() : m_proto() {}
160  ContextSpecificData( const T& proto ) : m_proto( proto ) {}
161 
163  ~ContextSpecificData() { m_ptr.deleteAll(); }
164 
165  inline operator T&()
166  {
167  if ( m_ptr )
168  return *m_ptr;
169  else
170  return *( m_ptr = new T( m_proto ) );
171  }
172 
173  inline operator T&() const
174  {
175  if ( m_ptr )
176  return *m_ptr;
177  else
178  return *( m_ptr = new T( m_proto ) );
179  }
180 
182  inline T& operator=( const T& other ) { return (T&)( *this ) = other; }
183 
185  inline T accumulate( T init ) const { return accumulate( init, std::plus<T>() ); }
186 
189  template <class T1, class BinaryOperation>
190  T1 accumulate( T1 init, BinaryOperation op ) const
191  {
192  return m_ptr.accumulate( []( const T* p ) -> T { return *p; }, init, op );
193  }
194 
196  template <class F>
197  void for_each( F f ) const
198  {
199  m_ptr.for_each( [&f]( const T* p ) { f( *p ); } );
200  }
201 
203  template <class F>
204  void for_each( F f )
205  {
206  m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
207  }
208 
210  template <class F>
211  void for_all( F f ) const
212  {
213  m_ptr.for_all( [&f]( size_t s, const T* p ) { f( s, *p ); } );
214  }
215  template <class F>
216  void for_all( F f )
217  {
218  m_ptr.for_all( [&f]( size_t s, T* p ) { f( s, *p ); } );
219  }
220 
221  private:
222  // FIXME: implement a proper copy constructor
223  ContextSpecificData( const ContextSpecificData& ) = delete;
224 
229  };
230  }
231 }
232 
233 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
GAUDI_API ContextIdType currentContextId()
Return the current context id.
ContextSpecificData(const T &proto)
Constructor with prototype value.
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.
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)...
std::result_of< Mapper(const T *)>::type accumulate(Mapper f, typename std::result_of< Mapper(const T *)>::type init, BinaryOperation op) const
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.
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)
void for_each(F f) const
Call a function on each contained pointer.
std::result_of< Mapper(const T *)>::type accumulate(Mapper f, typename std::result_of< Mapper(const T *)>::type init) const
Taking a function f that from a T* produces a value, return the sum of all the values corresponding t...
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.
ContextSpecificData()
Constructor with default initialization.
T accumulate(T...args)
Helper functions to set/get the application return code.
Definition: __init__.py:1