The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ContextSpecificPtr.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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  T* get() const {
48  auto lock = std::scoped_lock{ m_ptrs_lock };
49  return m_ptrs[currentContextId()];
50  }
52  T*& set( T* ptr ) {
53  auto lock = std::scoped_lock{ m_ptrs_lock };
54  return m_ptrs[currentContextId()] = ptr;
55  }
56 
58  T*& operator=( T* ptr ) { return set( ptr ); }
59 
61  bool isValid() const { return get(); }
62 
64  operator bool() const { return isValid(); }
65 
67  bool operator==( T* rhs ) const { return get() == rhs; }
68 
70  T& operator*() { return *get(); }
71  const T& operator*() const { return *get(); }
72  T* operator->() { return get(); }
73  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::invoke_result_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::invoke_result_t<Mapper, const T*> init, BinaryOperation op ) const
94  -> decltype( init ) {
95  auto lock = std::scoped_lock{ m_ptrs_lock };
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 {
104  auto lock = std::scoped_lock{ m_ptrs_lock };
105  for ( auto& i : m_ptrs ) f( i.second );
106  }
107 
109  template <class F>
110  void for_each( F f ) {
111  auto lock = std::scoped_lock{ m_ptrs_lock };
112  for ( auto& i : m_ptrs ) f( i.second );
113  }
114 
116  template <class F>
117  void for_all( F f ) const {
118  auto lock = std::scoped_lock{ m_ptrs_lock };
119  for ( auto& i : m_ptrs ) f( i.first, i.second );
120  }
121  template <class F>
122  void for_all( F f ) {
123  auto lock = std::scoped_lock{ m_ptrs_lock };
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
204 
206  T m_proto = {};
209  };
210  } // namespace Hive
211 } // namespace Gaudi
212 
213 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
Gaudi::Hive::ContextSpecificPtr::operator==
bool operator==(T *rhs) const
Comparison with another pointer.
Definition: ContextSpecificPtr.h:67
Gaudi::Hive::ContextSpecificPtr::clear
void clear()
Non thread-safe methods.
Definition: ContextSpecificPtr.h:79
Gaudi::Hive::ContextSpecificPtr::operator*
T & operator*()
Dereference operators.
Definition: ContextSpecificPtr.h:70
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData(T proto={})
Constructor with prototype value.
Definition: ContextSpecificPtr.h:151
std::move
T move(T... args)
Gaudi::Hive::ContextSpecificData::accumulate
T accumulate(T init) const
Return the sum of all the contained values using init as first value.
Definition: ContextSpecificPtr.h:170
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Hive::ContextSpecificPtr::operator->
T * operator->()
Definition: ContextSpecificPtr.h:72
Gaudi::Hive::ContextSpecificPtr::set
T *& set(T *ptr)
Set the pointer for the current context.
Definition: ContextSpecificPtr.h:52
Gaudi::Hive::ContextSpecificData::m_ptr
ContextSpecificPtr< T > m_ptr
Internal implementation.
Definition: ContextSpecificPtr.h:208
Gaudi::Hive::ContextSpecificPtr::accumulate
auto accumulate(Mapper f, std::invoke_result_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...
Definition: ContextSpecificPtr.h:85
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:138
std::unordered_map::clear
T clear(T... args)
Gaudi::Hive::ContextSpecificData::accumulate
T1 accumulate(T1 init, BinaryOperation op) const
Return the accumulated result, through the operation 'op', of all the contained values using init as ...
Definition: ContextSpecificPtr.h:175
Gaudi::Hive::ContextSpecificData::operator=
T & operator=(const T &other)
Assignment operator.
Definition: ContextSpecificPtr.h:167
Gaudi::Hive::ContextSpecificPtr::operator=
T *& operator=(T *ptr)
Assignment operator (.
Definition: ContextSpecificPtr.h:58
Gaudi::Hive::ContextSpecificPtr::deleteAll
void deleteAll()
Definition: ContextSpecificPtr.h:127
std::plus
Gaudi::Hive::ContextSpecificPtr
Simple implementation of a smart pointer with different values for different event contexts (slots).
Definition: ContextSpecificPtr.h:40
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f) const
Call a function on each contained value.
Definition: ContextSpecificPtr.h:181
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData(const ContextSpecificData &)=delete
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:193
std::accumulate
T accumulate(T... args)
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:136
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f) const
Call a function on each contained pointer.
Definition: ContextSpecificPtr.h:103
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:47
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:117
Gaudi::Hive::ContextSpecificPtr::StorageType
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
Definition: ContextSpecificPtr.h:43
Gaudi::Hive::ContextSpecificData::~ContextSpecificData
~ContextSpecificData()
Destructor.
Definition: ContextSpecificPtr.h:154
ThreadLocalContext.h
std::unordered_map::begin
T begin(T... args)
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f)
Call a function on each contained value. (non-const version)
Definition: ContextSpecificPtr.h:187
Kernel.h
EventContext
Definition: EventContext.h:34
Gaudi::Hive::ContextSpecificPtr::operator->
const T * operator->() const
Definition: ContextSpecificPtr.h:73
Gaudi::Hive::currentContextId
GAUDI_API ContextIdType currentContextId()
Return the current context id.
Definition: ThreadLocalContext.cpp:28
Gaudi::Hive::ContextSpecificPtr::operator*
const T & operator*() const
Definition: ContextSpecificPtr.h:71
std::mutex
STL class.
std::unordered_map::end
T end(T... args)
Gaudi::Hive::ContextSpecificData
Implementation of a context specific storage accessible as a sort of smart reference class.
Definition: ContextSpecificPtr.h:148
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:197
Gaudi::Hive::ContextSpecificPtr::isValid
bool isValid() const
Return true if the pointer is not null.
Definition: ContextSpecificPtr.h:61
std::unordered_map< ContextIdType, T * >
Gaudi::Hive::ContextSpecificPtr::accumulate
auto accumulate(Mapper f, std::invoke_result_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,...
Definition: ContextSpecificPtr.h:93
Gaudi::Hive::ContextSpecificData::m_proto
T m_proto
Prototype value.
Definition: ContextSpecificPtr.h:206
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f)
Call a function on each contained pointer. (non-const version)
Definition: ContextSpecificPtr.h:110
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:122