| The Gaudi Framework v32r1 (f65d50dc)

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"
12 #include "GaudiKernel/ThreadLocalContext.h"
13
14 class EventContext;
15
16 namespace Gaudi {
17 namespace Hive {
29 template <typename T>
30 class ContextSpecificPtr {
31 private:
33 typedef std::unordered_map<ContextIdType, T*> StorageType;
34
35 public:
37 inline T* get() const {
38 std::lock_guard<std::mutex> lock( m_ptrs_lock );
39 return m_ptrs[currentContextId()];
40 }
42 inline T*& set( T* ptr ) {
43 std::lock_guard<std::mutex> lock( m_ptrs_lock );
44 return m_ptrs[currentContextId()] = ptr;
45 }
46
48 inline T*& operator=( T* ptr ) { return set( ptr ); }
49
51 inline bool isValid() const { return get(); }
52
54 inline operator bool() const { return isValid(); }
55
57 inline bool operator==( T* rhs ) const { return get() == rhs; }
58
60 inline T& operator*() { return *get(); }
61 inline const T& operator*() const { return *get(); }
62 inline T* operator->() { return get(); }
63 inline const T* operator->() const { return get(); }
65
67
69 void clear() { m_ptrs.clear(); }
70
74 template <class Mapper>
75 auto accumulate( Mapper f, std::result_of_t<Mapper( const T* )> init ) const -> decltype( init ) {
76 return accumulate( f, init, std::plus<>() );
77 }
78
82 template <class Mapper, class BinaryOperation>
83 auto accumulate( Mapper f, std::result_of_t<Mapper( const T* )> init, BinaryOperation op ) const
84 -> decltype( init ) {
85 std::lock_guard<std::mutex> lock( m_ptrs_lock );
86 return std::accumulate( m_ptrs.begin(), m_ptrs.end(), init, [&f, &op]( const auto& partial, const auto& p ) {
87 return op( partial, f( p.second ) );
88 } );
89 }
90
92 template <class F>
93 void for_each( F f ) const {
94 std::lock_guard<std::mutex> lock( m_ptrs_lock );
95 for ( auto& i : m_ptrs ) f( i.second );
96 }
97
99 template <class F>
100 void for_each( F f ) {
101 std::lock_guard<std::mutex> lock( m_ptrs_lock );
102 for ( auto& i : m_ptrs ) f( i.second );
103 }
104
106 template <class F>
107 void for_all( F f ) const {
108 std::lock_guard<std::mutex> lock( m_ptrs_lock );
109 for ( auto& i : m_ptrs ) f( i.first, i.second );
110 }
111 template <class F>
112 void for_all( F f ) {
113 std::lock_guard<std::mutex> lock( m_ptrs_lock );
114 for ( auto& i : m_ptrs ) f( i.first, i.second );
115 }
116
117 void deleteAll() {
118 for_each( []( T*& p ) {
119 delete p;
120 p = nullptr;
121 } );
122 }
123
124 private:
126 mutable StorageType m_ptrs;
128 mutable std::mutex m_ptrs_lock;
129 };
130
137 template <typename T>
138 class ContextSpecificData {
139 public:
141 ContextSpecificData( T proto = {} ) : m_proto( std::move( proto ) ) {}
142
144 ~ContextSpecificData() { m_ptr.deleteAll(); }
145
146 operator T&() {
147 if ( !m_ptr ) m_ptr = new T( m_proto );
148 return *m_ptr;
149 }
150
151 operator T&() const {
152 if ( !m_ptr ) m_ptr = new T( m_proto );
153 return *m_ptr;
154 }
155
157 T& operator=( const T& other ) { return (T&)( *this ) = other; }
158
160 T accumulate( T init ) const { return accumulate( init, std::plus<>() ); }
161
164 template <class T1, class BinaryOperation>
165 T1 accumulate( T1 init, BinaryOperation op ) const {
166 return m_ptr.accumulate( []( const T* p ) { return *p; }, init, op );
167 }
168
170 template <class F>
171 void for_each( F f ) const {
172 m_ptr.for_each( [&f]( const T* p ) { f( *p ); } );
173 }
174
176 template <class F>
177 void for_each( F f ) {
178 m_ptr.for_each( [&f]( T* p ) { f( *p ); } );
179 }
180
182 template <class F>
183 void for_all( F f ) const {
184 m_ptr.for_all( [&f]( size_t s, const T* p ) { f( s, *p ); } );
185 }
186 template <class F>
187 void for_all( F f ) {
188 m_ptr.for_all( [&f]( size_t s, T* p ) { f( s, *p ); } );
189 }
190
191 private:
192 // FIXME: implement a proper copy constructor
193 ContextSpecificData( const ContextSpecificData& ) = delete;
194
196 T m_proto = {};
198 mutable ContextSpecificPtr<T> m_ptr;
199 };
200 } // namespace Hive
201 } // namespace Gaudi
202
203 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
Gaudi::Hive::ContextSpecificPtr::operator->
T * operator->()
Definition: ContextSpecificPtr.h:62
std::plus
Gaudi::Hive::currentContextId
GAUDI_API ContextIdType currentContextId()
Return the current context id.
Definition: ThreadLocalContext.cpp:18
Kernel.h
Gaudi::Hive::ContextSpecificPtr::operator==
bool operator==(T *rhs) const
Comparison with another pointer.
Definition: ContextSpecificPtr.h:57
Gaudi::Hive::ContextSpecificPtr::clear
void clear()
Non thread-safe methods.
Definition: ContextSpecificPtr.h:69
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f)
Call a function on each contained pointer. (non-const version)
Definition: ContextSpecificPtr.h:100
Gaudi::Hive::ContextSpecificPtr::accumulate
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...
Definition: ContextSpecificPtr.h:75
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData(T proto={})
Constructor with prototype value.
Definition: ContextSpecificPtr.h:141
ThreadLocalContext.h
Gaudi::Hive::ContextSpecificPtr::set
T *& set(T *ptr)
Set the pointer for the current context.
Definition: ContextSpecificPtr.h:42
std::unordered_map::end
T end(T... args)
Gaudi::Hive::ContextSpecificData::operator=
T & operator=(const T &other)
Assignment operator.
Definition: ContextSpecificPtr.h:157
Gaudi::Hive::ContextSpecificPtr::deleteAll
void deleteAll()
Definition: ContextSpecificPtr.h:117
EventContext
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
Gaudi::Hive::ContextSpecificData::m_proto
T m_proto
Prototype value.
Definition: ContextSpecificPtr.h:196
std::mutex
Gaudi::Hive::ContextSpecificPtr::operator *
T & operator *()
Dereference operators.
Definition: ContextSpecificPtr.h:60
Gaudi::Hive::ContextSpecificPtr
Simple implementation of a smart pointer with different values for different event contexts (slots).
Definition: ContextSpecificPtr.h:30
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:183
Gaudi::Hive::ContextSpecificPtr::operator=
T *& operator=(T *ptr)
Assignment operator (.
Definition: ContextSpecificPtr.h:48
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f)
Call a function on each contained value. (non-const version)
Definition: ContextSpecificPtr.h:177
Gaudi::Hive::ContextSpecificData::m_ptr
ContextSpecificPtr< T > m_ptr
Internal implementation.
Definition: ContextSpecificPtr.h:198
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:107
Gaudi::Hive::ContextSpecificPtr::get
T * get() const
Return the pointer for the current context (null for a new context).
Definition: ContextSpecificPtr.h:37
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:128
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:160
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:187
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:165
std::move
T move(T... args)
Gaudi::Hive::ContextSpecificPtr::operator->
const T * operator->() const
Definition: ContextSpecificPtr.h:63
Gaudi::Hive::ContextSpecificData
Implementation of a context specific storage accessible as a sort of smart reference class.
Definition: ContextSpecificPtr.h:138
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f) const
Call a function on each contained pointer.
Definition: ContextSpecificPtr.h:93
std::lock_guard
std::unordered_map::begin
T begin(T... args)
Gaudi::Hive::ContextSpecificPtr::m_ptrs
StorageType m_ptrs
Internal storage for the different internal pointers.
Definition: ContextSpecificPtr.h:126
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f) const
Call a function on each contained value.
Definition: ContextSpecificPtr.h:171
gaudirun.s
string s
Definition: gaudirun.py:316
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:112
Gaudi::Hive::ContextSpecificData::~ContextSpecificData
~ContextSpecificData()
Destructor.
Definition: ContextSpecificPtr.h:144
Gaudi::Hive::ContextSpecificPtr::StorageType
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
Definition: ContextSpecificPtr.h:33
std::accumulate
T accumulate(T... args)
Gaudi::Hive::ContextSpecificPtr::accumulate
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,...
Definition: ContextSpecificPtr.h:83
Gaudi::Hive::ContextSpecificPtr::isValid
bool isValid() const
Return true if the pointer is not null.
Definition: ContextSpecificPtr.h:51
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
std::unordered_map< ContextIdType, T * >