1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
#ifndef DataFormats_Common_RefToBase_h
#define DataFormats_Common_RefToBase_h
// -*- C++ -*-
//
// Package: Common
// Class : RefToBase
//
/**\class RefToBase RefToBase.h DataFormats/Common/interface/RefToBase.h
Description: Interface to a reference to an item based on the base class of the item
Usage:
Using an edm:RefToBase<T> allows one to hold references to items in different containers
within the edm::Event where those objects are only related by a base class, T.
\code
edm::Ref<FooCollection> foo(...);
std::vector<edm::RefToBase<Bar> > bars;
bars.push_back(edm::RefToBase<Bar>(foo));
\endcode
Cast to concrete type can be done via the castTo<REF>
function template. This function throws an exception
if the type passed as REF does not match the concrete
reference type.
*/
//
// Original Author: Chris Jones
// Created: Mon Apr 3 16:37:59 EDT 2006
//
// system include files
// user include files
#include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
#include "DataFormats/Common/interface/EDProductfwd.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "DataFormats/Common/interface/BaseHolder.h"
#include "DataFormats/Common/interface/Holder.h"
#include "DataFormats/Common/interface/IndirectHolder.h"
#include "DataFormats/Common/interface/RefHolder.h"
#include <memory>
#include <type_traits>
namespace edm {
//--------------------------------------------------------------------
// Class template RefToBase<T>
//--------------------------------------------------------------------
/// RefToBase<T> provides a mechanism to refer to an object of type
/// T (or which has T as a public base), held in a collection (of
/// type not known to RefToBase<T>) which itself it in an Event.
template <typename T>
class RefToBaseVector;
template <typename C, typename T, typename F>
class Ref;
template <typename C>
class RefProd;
template <typename T>
class RefToBaseProd;
template <typename T>
class View;
template <class T>
class RefToBase {
public:
typedef T value_type;
RefToBase();
RefToBase(RefToBase const& other);
RefToBase(RefToBase&& other) noexcept;
RefToBase& operator=(RefToBase&& other) noexcept;
template <typename C1, typename T1, typename F1>
explicit RefToBase(Ref<C1, T1, F1> const& r);
template <typename C>
explicit RefToBase(RefProd<C> const& r);
RefToBase(RefToBaseProd<T> const& r, size_t i);
RefToBase(Handle<View<T>> const& handle, size_t i);
template <typename T1>
explicit RefToBase(RefToBase<T1> const& r);
RefToBase(std::unique_ptr<reftobase::BaseHolder<value_type>>);
RefToBase(std::shared_ptr<reftobase::RefHolderBase> p);
~RefToBase() noexcept;
RefToBase& operator=(RefToBase const& rhs);
value_type const& operator*() const;
value_type const* operator->() const;
value_type const* get() const;
ProductID id() const;
size_t key() const;
template <class REF>
requires requires {
typename REF::value_type;
requires std::is_same_v<T, typename REF::value_type> or std::is_base_of_v<T, typename REF::value_type> or
std::is_base_of_v<typename REF::value_type, T>;
}
REF castTo() const;
bool isNull() const;
bool isNonnull() const;
bool operator!() const;
bool operator==(RefToBase const& rhs) const;
bool operator!=(RefToBase const& rhs) const;
void swap(RefToBase& other);
std::unique_ptr<reftobase::RefHolderBase> holder() const;
EDProductGetter const* productGetter() const;
/// Checks if collection is in memory or available
/// in the Event. No type checking is done.
/// This function is potentially costly as it might cause a disk
/// read (note that it does not cause the data to be cached locally)
bool isAvailable() const { return holder_ ? holder_->isAvailable() : false; }
bool isTransient() const { return holder_ ? holder_->isTransient() : false; }
//Needed for ROOT storage
CMS_CLASS_VERSION(10)
private:
value_type const* getPtrImpl() const;
reftobase::BaseHolder<value_type>* holder_;
friend class RefToBaseVector<T>;
friend class RefToBaseProd<T>;
template <typename B>
friend class RefToBase;
};
//--------------------------------------------------------------------
// Implementation of RefToBase<T>
//--------------------------------------------------------------------
template <class T>
inline RefToBase<T>::RefToBase() : holder_(nullptr) {}
template <class T>
inline RefToBase<T>::RefToBase(RefToBase const& other) : holder_(other.holder_ ? other.holder_->clone() : nullptr) {}
template <class T>
inline RefToBase<T>::RefToBase(RefToBase&& other) noexcept : holder_(other.holder_) {
other.holder_ = nullptr;
}
template <class T>
inline RefToBase<T>& RefToBase<T>::operator=(RefToBase&& other) noexcept {
delete holder_;
holder_ = other.holder_;
other.holder_ = nullptr;
return *this;
}
template <class T>
template <typename C1, typename T1, typename F1>
inline RefToBase<T>::RefToBase(Ref<C1, T1, F1> const& iRef)
: holder_(new reftobase::Holder<T, Ref<C1, T1, F1>>(iRef)) {}
template <class T>
template <typename C>
inline RefToBase<T>::RefToBase(RefProd<C> const& iRef) : holder_(new reftobase::Holder<T, RefProd<C>>(iRef)) {}
template <class T>
template <typename T1>
inline RefToBase<T>::RefToBase(RefToBase<T1> const& iRef)
: holder_(
new reftobase::IndirectHolder<T>(std::shared_ptr<edm::reftobase::RefHolderBase>(iRef.holder().release()))) {
// OUT: holder_( new reftobase::Holder<T,RefToBase<T1> >(iRef ) ) {
// Forcing the conversion through IndirectHolder,
// as Holder<T,RefToBase<T1>> would need dictionaries we will never have.
// In this way we only need the IndirectHolder<T> and the RefHolder of the real type of the item
// This might cause a small performance penalty.
static_assert(std::is_base_of<T, T1>::value, "RefToBase::RefToBase T not base of T1");
}
template <class T>
inline RefToBase<T>::RefToBase(std::unique_ptr<reftobase::BaseHolder<value_type>> p) : holder_(p.release()) {}
template <class T>
inline RefToBase<T>::RefToBase(std::shared_ptr<reftobase::RefHolderBase> p)
: holder_(new reftobase::IndirectHolder<T>(p)) {}
template <class T>
inline RefToBase<T>::~RefToBase() noexcept {
delete holder_;
}
template <class T>
inline RefToBase<T>& RefToBase<T>::operator=(RefToBase<T> const& iRHS) {
RefToBase<T> temp(iRHS);
temp.swap(*this);
return *this;
}
template <class T>
inline T const& RefToBase<T>::operator*() const {
return *getPtrImpl();
}
template <class T>
inline T const* RefToBase<T>::operator->() const {
return getPtrImpl();
}
template <class T>
inline T const* RefToBase<T>::get() const {
return getPtrImpl();
}
template <class T>
inline ProductID RefToBase<T>::id() const {
return holder_ ? holder_->id() : ProductID();
}
template <class T>
inline size_t RefToBase<T>::key() const {
if (holder_ == nullptr) {
Exception::throwThis(errors::InvalidReference,
"attempting get key from null RefToBase;\n"
"You should check for nullity before calling key().");
return 0;
}
return holder_->key();
}
template <class T>
template <class REF>
requires requires {
typename REF::value_type;
requires std::is_same_v<T, typename REF::value_type> or std::is_base_of_v<T, typename REF::value_type> or
std::is_base_of_v<typename REF::value_type, T>;
}
REF RefToBase<T>::castTo() const {
if (!holder_) {
Exception::throwThis(errors::InvalidReference,
"attempting to cast a null RefToBase;\n"
"You should check for nullity before casting.");
}
// If REF is type edm::Ref<C,T,F>, then it is impossible to
// check the container type C here. We just have to assume
// that the caller provided the correct type.
EDProductGetter const* getter = productGetter();
if (getter) {
return REF(id(), key(), getter);
}
T const* value = get();
if (value == nullptr) {
return REF(id());
}
typename REF::value_type const* newValue;
if constexpr (std::is_same_v<T, typename REF::value_type> or std::is_base_of_v<typename REF::value_type, T>) {
newValue = value;
} else {
newValue = dynamic_cast<typename REF::value_type const*>(value);
}
if (newValue) {
return REF(id(), newValue, key(), isTransient());
}
Exception::throwThis(errors::InvalidReference,
"RefToBase<T>::castTo Error attempting to cast mismatched types\n"
"casting from RefToBase with T: ",
typeid(T).name(),
"\ncasting to: ",
typeid(REF).name());
return REF();
}
/// Checks for null
template <class T>
inline bool RefToBase<T>::isNull() const {
return !id().isValid();
}
/// Checks for non-null
template <class T>
inline bool RefToBase<T>::isNonnull() const {
return !isNull();
}
/// Checks for null
template <class T>
inline bool RefToBase<T>::operator!() const {
return isNull();
}
template <class T>
inline bool RefToBase<T>::operator==(RefToBase<T> const& rhs) const {
return holder_ ? holder_->isEqualTo(*rhs.holder_) : holder_ == rhs.holder_;
}
template <class T>
inline bool RefToBase<T>::operator!=(RefToBase<T> const& rhs) const {
return !(*this == rhs);
}
template <class T>
inline void RefToBase<T>::swap(RefToBase<T>& other) {
std::swap(holder_, other.holder_);
}
template <class T>
inline EDProductGetter const* RefToBase<T>::productGetter() const {
return holder_ ? holder_->productGetter() : nullptr;
}
template <class T>
inline T const* RefToBase<T>::getPtrImpl() const {
return holder_ ? holder_->getPtr() : nullptr;
}
template <class T>
std::unique_ptr<reftobase::RefHolderBase> RefToBase<T>::holder() const {
return holder_ ? holder_->holder() : std::unique_ptr<reftobase::RefHolderBase>();
}
// Free swap function
template <class T>
inline void swap(RefToBase<T>& a, RefToBase<T>& b) {
a.swap(b);
}
} // namespace edm
#include "DataFormats/Common/interface/RefToBaseProd.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/View.h"
namespace edm {
template <class T>
inline RefToBase<T>::RefToBase(RefToBaseProd<T> const& r, size_t i)
: holder_(r.operator->()->refAt(i).holder_->clone()) {}
template <typename T>
inline RefToBase<T>::RefToBase(Handle<View<T>> const& handle, size_t i)
: holder_(handle.operator->()->refAt(i).holder_->clone()) {}
} // namespace edm
#endif
|