| The Gaudi Framework v28r2-patches (bc4ad17a)

- 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 <unordered_map>
5 #include <mutex>
6 #include <numeric>
7 #include <functional>
8 #include <type_traits>
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 public:
35
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 typename std::result_of<Mapper(const T*)>::type
76 accumulate(Mapper f, typename std::result_of<Mapper(const T*)>::type init) const {
77 typedef typename std::result_of<Mapper(const T*)>::type R;
78 return accumulate(f, init, std::plus<R>());
79 }
80
84 template< class Mapper, class BinaryOperation >
85 typename std::result_of<Mapper(const T*)>::type
86 accumulate(Mapper f, typename std::result_of<Mapper(const T*)>::type init,
87 BinaryOperation op) const {
88 typedef typename std::result_of<Mapper(const T*)>::type R;
89 typedef typename StorageType::value_type V;
90 std::lock_guard<std::mutex> lock(m_ptrs_lock);
91 return std::accumulate(m_ptrs.begin(), m_ptrs.end(), init,
92 [&f, &op](const R& partial, const V& p) -> R {
93 return op(partial, f(p.second));
94 });
95 }
96
98 template< class F >
99 void for_each(F f) const {
100 std::lock_guard<std::mutex> lock(m_ptrs_lock);
101 for(auto& i: m_ptrs) f(i.second);
102 }
103
105 template< class F >
106 void for_each(F f) {
107 std::lock_guard<std::mutex> lock(m_ptrs_lock);
108 for(auto& i: m_ptrs) f(i.second);
109 }
110
112 template< class F >
113 void for_all(F f) const {
114 std::lock_guard<std::mutex> lock(m_ptrs_lock);
115 for(auto& i: m_ptrs) f(i.first,i.second);
116 }
117 template< class F >
118 void for_all(F f) {
119 std::lock_guard<std::mutex> lock(m_ptrs_lock);
120 for(auto& i: m_ptrs) f(i.first,i.second);
121 }
122
123
124 void deleteAll() {
125 for_each([](T*& p) {delete p; p = nullptr;});
126 }
127
128
129 private:
131 mutable StorageType m_ptrs;
133 mutable std::mutex m_ptrs_lock;
134 };
135
136
143 template< typename T >
144 class ContextSpecificData {
145 public:
147 ContextSpecificData(): m_proto() {}
149 ContextSpecificData(const T& proto): m_proto(proto) {}
150
152 ~ContextSpecificData() {
153 m_ptr.deleteAll();
154 }
155
156 inline operator T& () {
157 if (m_ptr)
158 return *m_ptr;
159 else
160 return *(m_ptr = new T(m_proto));
161 }
162
163 inline operator T& () const {
164 if (m_ptr)
165 return *m_ptr;
166 else
167 return *(m_ptr = new T(m_proto));
168 }
169
171 inline T& operator= (const T& other) {
172 return (T&)(*this) = other;
173 }
174
176 inline T accumulate(T init) const {
177 return accumulate(init, std::plus<T>());
178 }
179
182 template< class T1, class BinaryOperation >
183 T1 accumulate(T1 init, BinaryOperation op) const {
184 return m_ptr.accumulate([] (const T* p) -> T { return *p; },
185 init, op);
186 }
187
189 template< class F >
190 void for_each(F f) const {
191 m_ptr.for_each([&f] (const T* p) { f(*p); });
192 }
193
195 template< class F >
196 void for_each(F f) {
197 m_ptr.for_each([&f] (T* p) { f(*p); });
198 }
199
201 template< class F >
202 void for_all(F f) const {
203 m_ptr.for_all([&f] (size_t s, const T* p) { f(s,*p); });
204 }
205 template< class F >
206 void for_all(F f) {
207 m_ptr.for_all([&f] (size_t s, T* p) { f(s,*p); });
208 }
209
210
211
212 private:
213 // FIXME: implement a proper copy constructor
214 ContextSpecificData(const ContextSpecificData&) = delete;
215
217 T m_proto;
219 mutable ContextSpecificPtr<T> m_ptr;
220
221 };
222 }
223 }
224
225 #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
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData(const T &proto)
Constructor with prototype value.
Definition: ContextSpecificPtr.h:149
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:113
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:106
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f) const
Call a function on each element, passing slot# as well.
Definition: ContextSpecificPtr.h:202
Gaudi::Hive::ContextSpecificPtr::operator*
T & operator*()
Dereference operators.
Definition: ContextSpecificPtr.h:60
ThreadLocalContext.h
Gaudi::Hive::ContextSpecificData::for_each
void for_each(F f) const
Call a function on each contained value.
Definition: ContextSpecificPtr.h:190
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:51
Gaudi::Hive::ContextSpecificPtr::deleteAll
void deleteAll()
Definition: ContextSpecificPtr.h:124
Gaudi::Hive::ContextSpecificPtr::operator==
bool operator==(T *rhs) const
Comparison with another pointer.
Definition: ContextSpecificPtr.h:57
EventContext
This class represents an entry point to all the event specific data.
Definition: EventContext.h:25
Gaudi::Hive::ContextSpecificData::m_proto
T m_proto
Prototype value.
Definition: ContextSpecificPtr.h:217
std::mutex
Gaudi::Hive::ContextSpecificPtr
Simple implementation of a smart pointer with different values for different event contexts (slots)...
Definition: ContextSpecificPtr.h:30
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:196
Gaudi::Hive::ContextSpecificData::m_ptr
ContextSpecificPtr< T > m_ptr
Internal implementation.
Definition: ContextSpecificPtr.h:219
Gaudi::Hive::ContextSpecificPtr::m_ptrs_lock
std::mutex m_ptrs_lock
Mutex for the m_ptrs container.
Definition: ContextSpecificPtr.h:133
gaudirun.type
type
Definition: gaudirun.py:125
std::result_of
Gaudi::Hive::ContextSpecificData::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:206
std::unordered_map::clear
T clear(T...args)
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:86
Gaudi::Hive::ContextSpecificPtr::for_each
void for_each(F f) const
Call a function on each contained pointer.
Definition: ContextSpecificPtr.h:99
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:76
Gaudi::Hive::ContextSpecificData
Implementation of a context specific storage accessible as a sort of smart reference class...
Definition: ContextSpecificPtr.h:144
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:131
Hive
Definition: HiveEventRegistryEntry.h:24
gaudirun.s
string s
Definition: gaudirun.py:245
Gaudi::Hive::ContextSpecificPtr::for_all
void for_all(F f)
Definition: ContextSpecificPtr.h:118
Gaudi::Hive::ContextSpecificData::~ContextSpecificData
~ContextSpecificData()
Destructor.
Definition: ContextSpecificPtr.h:152
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:183
Gaudi::Hive::ContextSpecificPtr::StorageType
std::unordered_map< ContextIdType, T * > StorageType
Type used for the internal storage.
Definition: ContextSpecificPtr.h:33
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:176
Gaudi::Hive::ContextSpecificData::ContextSpecificData
ContextSpecificData()
Constructor with default initialization.
Definition: ContextSpecificPtr.h:147
std::accumulate
T accumulate(T...args)
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
std::unordered_map< ContextIdType, T * >