CMS 3D CMS Logo

RefToBaseVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefToBaseVector_h
2 #define DataFormats_Common_RefToBaseVector_h
3 
11 #include <algorithm>
12 #include <memory>
13 #include <utility>
14 #include <vector>
15 
16 namespace edm {
17  template<typename T> class RefToBase;
18  template<typename T> class View;
19  template<typename C> class Handle;
20  class EDProductGetter;
21  namespace reftobase {
22  template<typename T> class BaseVectorHolder;
23  class RefVectorHolderBase;
24  }
25 
26  template <class T>
27  class RefToBaseVector {
28  public:
30  typedef T member_type;
33  typedef typename holder_type::const_iterator const_iterator;
34 
37  template<class REFV>
38  explicit RefToBaseVector(REFV const& );
39  template<typename C>
40  explicit RefToBaseVector(Handle<C> const& );
41  RefToBaseVector(std::shared_ptr<reftobase::RefVectorHolderBase> p);
42  RefToBaseVector& operator=(RefToBaseVector const& iRHS);
43  void swap(RefToBaseVector& other);
44 
45  ~RefToBaseVector();
46 
47  //void reserve(size_type n);
48  void clear();
49 
50  value_type at(size_type idx) const;
51  value_type operator[](size_type idx) const;
52  bool isValid() const { return holder_ != nullptr; }
53  bool isInvalid() const { return holder_ == nullptr; }
54  bool empty() const;
55  size_type size() const;
56  //size_type capacity() const;
57  ProductID id() const;
58  EDProductGetter const * productGetter() const;
59  const_iterator begin() const;
60  const_iterator end() const;
61 
62  void push_back( const RefToBase<T> & );
63 
64  void fillView(std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;
65  std::unique_ptr<reftobase::RefVectorHolderBase> vectorHolder() const;
66 
69  bool isAvailable() const { return holder_->isAvailable(); }
70 
71  //Needed for ROOT storage
73 
74  private:
75  holder_type* holder_;
76  };
77 }
78 
79 #include "DataFormats/Common/interface/RefToBase.h"
85 
86 namespace edm {
87  template <class T>
88  inline
89  void
91  a.swap(b);
92  }
93 
94  template <class T>
95  inline
96  bool
98  RefToBaseVector<T> const& b)
99  {
100  if ( a.isInvalid() && b.isInvalid() ) return true;
101  if ( a.isInvalid() || b.isInvalid() ) return false;
102  return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
103  }
104 
105  //--------------------------------------------------------------------
106  // Implementation of RefToBaseVector<T>
107  //--------------------------------------------------------------------
108 
109  template <class T>
110  inline
112  holder_(nullptr)
113  { }
114 
115  template <class T>
116  template <class REFV>
117  inline
119  holder_(new reftobase::VectorHolder<T,REFV>(iRef))
120  { }
121 
122  template <class T>
123  inline
125  holder_(iOther.holder_ ? iOther.holder_->clone() : nullptr)
126  { }
127 
128  template <class T>
129  inline
130  RefToBaseVector<T>::RefToBaseVector(std::shared_ptr<reftobase::RefVectorHolderBase> p) :
131  holder_(new reftobase::IndirectVectorHolder<T>(p)) {
132  }
133 
134  template <class T>
135  inline
138  RefToBaseVector temp(iRHS);
139  this->swap(temp);
140  return *this;
141  }
142 
143  template <class T>
144  inline
145  void
147  std::swap(holder_, other.holder_);
148  }
149 
150  template <class T>
151  inline
153  {
154  delete holder_;
155  }
156 
157  template <class T>
158  inline
160  RefToBaseVector<T>::at(size_type idx) const
161  {
162  if ( holder_ == nullptr )
164  "Trying to dereference null RefToBaseVector<T> in method: at(",
165  idx,
166  ")\n");
167  return holder_->at( idx );
168  }
169 
170  template <class T>
171  inline
174  {
175  return at( idx );
176  }
177 
178  template <class T>
179  inline
180  bool
182  {
183  return holder_ ? holder_->empty() : true;
184  }
185 
186  template <class T>
187  inline
190  {
191  return holder_ ? holder_->size() : 0;
192  }
193 
194  template <class T>
195  inline
196  void
198  {
199  if ( holder_ != nullptr )
200  holder_->clear();
201  }
202 
203  template <class T>
204  inline
205  ProductID
207  {
208  return holder_ ? holder_->id() : ProductID();
209  }
210 
211  template <class T>
212  inline
213  EDProductGetter const *
215  {
216  return holder_ ? holder_->productGetter() : nullptr;
217  }
218 
219  template <class T>
220  inline
223  {
224  return holder_ ? holder_->begin() : const_iterator();
225  }
226 
227  template <class T>
228  inline
231  {
232  return holder_ ? holder_->end() : const_iterator();
233  }
234 
235  template <typename T>
236  void
237  RefToBaseVector<T>::fillView(std::vector<void const*>& pointers, FillViewHelperVector& helpers) const
238  {
239  pointers.reserve(this->size());
240  helpers.reserve(this->size());
241  for (const_iterator i=begin(), e=end(); i!=e; ++i) {
242  RefToBase<T> ref = * i;
243  member_type const * address = ref.isNull() ? 0 : & * ref;
244  pointers.push_back(address);
245  helpers.push_back(FillViewHelperVector::value_type(ref.id(),ref.key()));
246  }
247  }
248 
249  // NOTE: the following implementation has unusual signature!
250  template <typename T>
251  inline void fillView(RefToBaseVector<T> const& obj,
252  ProductID const&,
253  std::vector<void const*>& pointers,
254  FillViewHelperVector& helpers) {
255  obj.fillView(pointers,helpers);
256  }
257 
258  template <typename T>
260  static bool const value = true;
261  };
262 
263  template <typename T>
265  if ( holder_ == nullptr ) {
266  std::unique_ptr<reftobase::BaseVectorHolder<T> > p = r.holder_->makeVectorHolder();
267  holder_ = p.release();
268  }
269  holder_->push_back( r.holder_ );
270  }
271 
272  template <typename T>
273  std::unique_ptr<reftobase::RefVectorHolderBase> RefToBaseVector<T>::vectorHolder() const {
274  return holder_ ? holder_->vectorHolder() : std::unique_ptr<reftobase::RefVectorHolderBase>();
275  }
276 }
277 
280 
281 namespace edm {
282 
283  template<typename T>
284  template<typename C>
286  holder_(new reftobase::VectorHolder<T, RefVector<C, typename refhelper::ValueTrait<C>::value,
287  typename refhelper::FindTrait<C, T>::value> >(h.id())) {
288  }
289 
290 }
291 #endif
size
Write out results.
virtual void push_back(BaseHolder< T > const *)=0
holder_type::const_iterator const_iterator
value_type operator[](size_type idx) const
std::unique_ptr< reftobase::RefVectorHolderBase > vectorHolder() const
const_iterator end() const
bool isValid() const
#define nullptr
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
EDProductGetter const * productGetter(std::atomic< void const * > const &iCache)
value_type at(size_type idx) const
RefToBase< T > value_type
ProductID id() const
Definition: RefToBase.h:242
bool isInvalid() const
reftobase::BaseVectorHolder< T > holder_type
bool equal(const T &first, const T &second)
Definition: Equal.h:34
EDProductGetter const * productGetter() const
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
holder_type * holder_
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
reftobase::BaseHolder< value_type > * holder_
Definition: RefToBase.h:122
size_t key() const
Definition: RefToBase.h:250
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
T operator[](int i) const
#define end
Definition: vmac.h:37
Definition: value.py:1
virtual base_ref_type const at(size_type idx) const =0
size_type size() const
void fillView(std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
virtual EDProductGetter const * productGetter() const =0
void swap(RefToBaseVector &other)
virtual size_type size() const =0
bool isNull() const
Checks for null.
Definition: RefToBase.h:328
virtual std::unique_ptr< RefVectorHolderBase > vectorHolder() const =0
double b
Definition: hdecay.h:120
RefToBaseVector & operator=(RefToBaseVector const &iRHS)
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
const_iterator begin() const
virtual const_iterator end() const =0
#define begin
Definition: vmac.h:30
HLT enums.
virtual const_iterator begin() const =0
double a
Definition: hdecay.h:121
void push_back(const RefToBase< T > &)
virtual ProductID id() const =0
long double T
bool isAvailable() const
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
virtual bool empty() const =0
ProductID id() const
holder_type::size_type size_type
void swap(BaseHolder< T > &lhs, BaseHolder< T > &rhs)
Definition: BaseHolder.h:116