The Gaudi Framework  v33r0 (d5ea422b)
ContextSpecificPtr.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef _GAUDI_CONTEXTSPECIFICPTR_H_
12 #define _GAUDI_CONTEXTSPECIFICPTR_H_
13 
14 #include <functional>
15 #include <mutex>
16 #include <numeric>
17 #include <type_traits>
18 #include <unordered_map>
19 
20 // For the definition of GAUDI_API
21 #include "GaudiKernel/Kernel.h"
23 
24 class EventContext;
25 
26 namespace Gaudi {
27  namespace Hive {
39  template <typename T>
41  private:
44 
45  public:
47  inline T* get() const {
49  return m_ptrs[currentContextId()];
50  }
52  inline T*& set( T* ptr ) {
54  return m_ptrs[currentContextId()] = ptr;
55  }
56 
58  inline T*& operator=( T* ptr ) { return set( ptr ); }
59 
61  inline bool isValid() const { return get(); }
62 
64  inline operator bool() const { return isValid(); }
65 
67  inline bool operator==( T* rhs ) const { return get() == rhs; }
68 
70  inline T& operator*() { return *get(); }
71  inline const T& operator*() const { return *get(); }
72  inline T* operator->() { return get(); }
73  inline const T* operator->() const { return get(); }
75 
77 
79  void clear() { m_ptrs.clear(); }
80 
84  template <class Mapper>
85  auto accumulate( Mapper f, std::result_of_t<Mapper( const T* )> init ) const -> decltype( init ) {
86  return accumulate( f, init, std::plus<>() );
87  }
88 
92  template <class Mapper, class BinaryOperation>
93  auto accumulate( Mapper f, std::result_of_t<Mapper( const T* )> init, BinaryOperation op ) const
94  -> decltype( init ) {
96  return std::accumulate( m_ptrs.begin(), m_ptrs.end(), init, [&f, &op]( const auto& partial, const auto& p ) {
97  return op( partial, f( p.second ) );
98  } );
99  }
100 
102  template <class F>
103  void for_each( F f ) const {
105  for ( auto& i : m_ptrs ) f( i.second );
106  }
107 
109  template <class F>
110  void for_each( F f ) {
112  for ( auto& i : m_ptrs ) f( i.second );
113  }
114 
116  template <class F>
117  void for_all( F f ) const {
119  for ( auto& i : m_ptrs ) f( i.first, i.second );
120  }
121  template <class F>
122  void for_all( F f ) {
124  for ( auto& i : m_ptrs ) f( i.first, i.second );
125  }
126 
127  void deleteAll() {
128  for_each( []( T*& p ) {
129  delete p;
130  p = nullptr;
131  } );
132  }
133 
134  private:
139  };
140 
147  template <typename T>
149  public:
151  ContextSpecificData( T proto = {} ) : m_proto( std::move( proto ) ) {}
152 
154  ~ContextSpecificData() { m_ptr.deleteAll(); }
155 
156  operator T&() {
157  if ( !m_ptr ) m_ptr = new T( m_proto );
158  return *m_ptr;
159  }
160 
161  operator T&() const {
162  if ( !m_ptr ) m_ptr = new T( m_proto );
163  return *m_ptr;
164  }
165 
167  T& operator=( const T& other ) { return (T&)( *this ) = other; }
168 
170  T accumulate( T init ) const { return accumulate( init, std::plus<>() ); }
171 
174  template <class T1, class BinaryOperation>
175  T1 accumulate( T1 init, BinaryOperation op ) const {
176  return m_ptr.accumulate( []( const T* p ) { return *p; }, init, op );
177  }
178 
180  template <class F>
181  void for_each( F f ) const {
182  m_ptr.for_each( [&f]( const T* p ) { f( *p ); } );
183  }
184 
186  template <class F>
187  void for_each( F f ) {
188  m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
189  }
190 
192  template <class F>
193  void for_all( F f ) const {
194  m_ptr.for_all( [&f]( size_t s, const T* p ) { f( s, *p ); } );
195  }
196  template <class F>
197  void for_all( F f ) {
198  m_ptr.for_all( [&f]( size_t s, T* p ) { f( s, *p ); } );
199  }
200 
201  private:
202  // FIXME: implement a proper copy constructor
203  ContextSpecificData( const ContextSpecificData& ) = delete;
204 
206  T m_proto = {};
209  };
210  } // namespace Hive
211 } // namespace Gaudi
212 
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 end(T... args)
T & operator=(const T &other)
Assignment operator.
This class represents an entry point to all the event specific data.
Definition: EventContext.h:34
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.
T clear(T... args)
T1 accumulate(T1 init, BinaryOperation op) const
Return the accumulated result, through the operation 'op', of all the contained values using init as ...
T move(T... args)
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.
T begin(T... args)
StorageType m_ptrs
Internal storage for the different internal pointers.
void for_each(F f) const
Call a function on each contained value.
string s
Definition: gaudirun.py:328
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
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,...
bool isValid() const
Return true if the pointer is not null.
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1