| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
18 namespace Hive
19 {
31 template <typename T>
32 class ContextSpecificPtr
33 {
34 private:
36 typedef std::unordered_map<ContextIdType, T*> StorageType;
37
38 public:
40 inline T* get() const
41 {
42 std::lock_guard<std::mutex> lock( m_ptrs_lock );
43 return m_ptrs[currentContextId()];
44 }
46 inline T*& set( T* ptr )
47 {
48 std::lock_guard<std::mutex> lock( m_ptrs_lock );
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 typename std::result_of<Mapper( const T* )>::type
81 accumulate( Mapper f, typename std::result_of<Mapper( const T* )>::type init ) const
82 {
83 typedef typename std::result_of<Mapper( const T* )>::type R;
84 return accumulate( f, init, std::plus<R>() );
85 }
86
90 template <class Mapper, class BinaryOperation>
91 typename std::result_of<Mapper( const T* )>::type
92 accumulate( Mapper f, typename std::result_of<Mapper( const T* )>::type init, BinaryOperation op ) const
93 {
94 typedef typename std::result_of<Mapper( const T* )>::type R;
95 typedef typename StorageType::value_type V;
96 std::lock_guard<std::mutex> lock( m_ptrs_lock );
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 {
106 std::lock_guard<std::mutex> lock( m_ptrs_lock );
107 for ( auto& i : m_ptrs ) f( i.second );
108 }
109
111 template <class F>
112 void for_each( F f )
113 {
114 std::lock_guard<std::mutex> lock( m_ptrs_lock );
115 for ( auto& i : m_ptrs ) f( i.second );
116 }
117
119 template <class F>
120 void for_all( F f ) const
121 {
122 std::lock_guard<std::mutex> lock( m_ptrs_lock );
123 for ( auto& i : m_ptrs ) f( i.first, i.second );
124 }
125 template <class F>
126 void for_all( F f )
127 {
128 std::lock_guard<std::mutex> lock( m_ptrs_lock );
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;
144 mutable std::mutex m_ptrs_lock;
145 };
146
153 template <typename T>
154 class ContextSpecificData
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
226 T m_proto;
228 mutable ContextSpecificPtr<T> m_ptr;
229 };
230 }
231 }
232
233 #endif // _GAUDI_CONTEXTSPECIFICPTR_H_
Gaudi::Hive::ContextSpecificPtr::operator->
T * operator->()
Definition: ContextSpecificPtr.h:67
std::plus
Gaudi::Hive::currentContextId
GAUDI_API ContextIdType currentContextId()
Return the current context id.
Definition: ThreadLocalContext.cpp:22
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData(const T &proto)
Constructor with prototype value.
Definition: ContextSpecificPtr.h:160
Kernel.h
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:120
Gaudi::Hive::ContextSpecificPtr::clear
void clear()
Non thread-safe methods.
Definition: ContextSpecificPtr.h:74
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f)
Call a function on each contained pointer. (non-const version)
Definition: ContextSpecificPtr.h:112
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:211
Gaudi::Hive::ContextSpecificPtr::operator*
T & operator*()
Dereference operators.
Definition: ContextSpecificPtr.h:65
ThreadLocalContext.h
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f) const
Call a function on each contained value.
Definition: ContextSpecificPtr.h:197
std::unordered_map::end
T end(T...args)
Gaudi::Hive::ContextSpecificPtr::isValid
bool isValid() const
Return true if the pointer is not null.
Definition: ContextSpecificPtr.h:56
Gaudi::Hive::ContextSpecificData::operator=
T & operator=(const T &other)
Assignment operator.
Definition: ContextSpecificPtr.h:182
Gaudi::Hive::ContextSpecificPtr::deleteAll
void deleteAll()
Definition: ContextSpecificPtr.h:132
Gaudi::Hive::ContextSpecificPtr::operator==
bool operator==(T *rhs) const
Comparison with another pointer.
Definition: ContextSpecificPtr.h:62
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:226
std::mutex
Gaudi::Hive::ContextSpecificPtr
Simple implementation of a smart pointer with different values for different event contexts (slots)...
Definition: ContextSpecificPtr.h:32
Gaudi::Hive::ContextSpecificPtr::accumulate
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.
Definition: ContextSpecificPtr.h:92
Gaudi::Hive::ContextSpecificPtr::operator=
T *& operator=(T *ptr)
Assignment operator (.
Definition: ContextSpecificPtr.h:53
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f)
Call a function on each contained value. (non-const version)
Definition: ContextSpecificPtr.h:204
Gaudi::Hive::ContextSpecificData::m_ptr
ContextSpecificPtr< T > m_ptr
Internal implementation.
Definition: ContextSpecificPtr.h:228
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:144
gaudirun.type
type
Definition: gaudirun.py:133
std::result_of
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:216
std::unordered_map::clear
T clear(T...args)
Gaudi::Hive::ContextSpecificPtr::operator*
const T & operator*() const
Definition: ContextSpecificPtr.h:66
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f) const
Call a function on each contained pointer.
Definition: ContextSpecificPtr.h:104
Gaudi::Hive::ContextSpecificPtr::accumulate
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...
Definition: ContextSpecificPtr.h:81
Gaudi::Hive::ContextSpecificData
Implementation of a context specific storage accessible as a sort of smart reference class...
Definition: ContextSpecificPtr.h:154
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:142
Hive
Definition: HiveEventRegistryEntry.h:24
gaudirun.s
string s
Definition: gaudirun.py:253
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:126
Gaudi::Hive::ContextSpecificData::~ContextSpecificData
~ContextSpecificData()
Destructor.
Definition: ContextSpecificPtr.h:163
Gaudi::Hive::ContextSpecificData::accumulate
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 ...
Definition: ContextSpecificPtr.h:190
Gaudi::Hive::ContextSpecificPtr::StorageType
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
Definition: ContextSpecificPtr.h:36
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:185
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData()
Constructor with default initialization.
Definition: ContextSpecificPtr.h:158
std::accumulate
T accumulate(T...args)
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::Hive::ContextSpecificPtr::operator->
const T * operator->() const
Definition: ContextSpecificPtr.h:68
std::unordered_map< ContextIdType, T * >