CMS 3D CMS Logo

RefVectorHolder.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefVectorHolder_h
2 #define DataFormats_Common_RefVectorHolder_h
3 
8 
9 namespace edm {
10  namespace reftobase {
11  class RefHolderBase;
12  template <typename REF>
13  class RefHolder;
14 
15  template <typename REFV>
17  public:
19  RefVectorHolder(REFV const& refs) : RefVectorHolderBase(), refs_(refs) {}
20  explicit RefVectorHolder(ProductID const& iId) : RefVectorHolderBase(), refs_(iId) {}
21  void swap(RefVectorHolder& other);
22  bool empty() const override;
23  size_type size() const override;
24  void clear() override;
25  void push_back(RefHolderBase const* r) override;
26  void reserve(size_type n) override;
27  ProductID id() const override;
28  EDProductGetter const* productGetter() const override;
29  RefVectorHolder<REFV>* clone() const override;
30  RefVectorHolder<REFV>* cloneEmpty() const override;
31  void setRefs(REFV const& refs);
32  size_t keyForIndex(size_t idx) const override;
33 
34  //Needed for ROOT storage
36 
37  private:
39 
40  public:
42  typedef ptrdiff_t difference_type;
44  explicit const_iterator_imp_specific(typename REFV::const_iterator const& it) : i(it) {}
47  void increase() override { ++i; }
48  void decrease() override { --i; }
49  void increase(difference_type d) override { i += d; }
50  void decrease(difference_type d) override { i -= d; }
51  bool equal_to(const_iterator_imp const* o) const override { return i == dc(o); }
52  bool less_than(const_iterator_imp const* o) const override { return i < dc(o); }
53  void assign(const_iterator_imp const* o) override { i = dc(o); }
54  std::shared_ptr<RefHolderBase> deref() const override;
55  difference_type difference(const_iterator_imp const* o) const override { return i - dc(o); }
56 
57  private:
58  typename REFV::const_iterator const& dc(const_iterator_imp const* o) const {
59  if (o == nullptr) {
60  Exception::throwThis(errors::InvalidReference, "In RefVectorHolder trying to dereference a null pointer\n");
61  }
63  if (oo == nullptr) {
65  "In RefVectorHolder trying to cast iterator to wrong type\n");
66  }
67  return oo->i;
68  }
69  typename REFV::const_iterator i;
70  };
71 
73 
74  const_iterator begin() const override { return const_iterator(new const_iterator_imp_specific(refs_.begin())); }
75  const_iterator end() const override { return const_iterator(new const_iterator_imp_specific(refs_.end())); }
76 
79  bool isAvailable() const override { return refs_.isAvailable(); }
80 
81  private:
82  std::shared_ptr<reftobase::RefHolderBase> refBase(size_t idx) const override;
83  REFV refs_;
84  };
85 
86  //
87  // implementations for RefVectorHolder<REFV>
88  //
89 
90  template <typename REFV>
92  this->RefVectorHolderBase::swap(other);
93  refs_.swap(other.refs_);
94  }
95 
96  template <typename REFV>
97  inline bool RefVectorHolder<REFV>::empty() const {
98  return refs_.empty();
99  }
100 
101  template <typename REFV>
103  return refs_.size();
104  }
105 
106  template <typename REFV>
108  return refs_.clear();
109  }
110 
111  template <typename REFV>
113  typename REFV::size_type s = n;
114  refs_.reserve(s);
115  }
116 
117  template <typename REFV>
119  return refs_.id();
120  }
121 
122  template <typename REFV>
124  return refs_.productGetter();
125  }
126 
127  template <typename REFV>
129  return new RefVectorHolder<REFV>(*this);
130  }
131 
132  template <typename REFV>
134  return new RefVectorHolder<REFV>(id());
135  }
136 
137  template <typename REFV>
138  inline void RefVectorHolder<REFV>::setRefs(REFV const& refs) {
139  refs_ = refs;
140  }
141 
142  template <typename REFV>
143  inline size_t RefVectorHolder<REFV>::keyForIndex(size_t idx) const {
144  return refs_[idx].key();
145  }
146 
147  // Free swap function
148  template <typename REFV>
150  lhs.swap(rhs);
151  }
152  } // namespace reftobase
153 } // namespace edm
154 
156 
157 namespace edm {
158  namespace reftobase {
159 
160  template <typename REFV>
162  typedef typename REFV::value_type REF;
163  RefHolder<REF> const* rh = dynamic_cast<RefHolder<REF> const*>(h);
164  if (rh == nullptr) {
166  "RefVectorHolder: attempting to cast a RefHolderBase "
167  "to an invalid type.\nExpected: ",
168  typeid(REF).name(),
169  "\n");
170  }
171  refs_.push_back(rh->getRef());
172  }
173 
174  template <typename REFV>
175  std::shared_ptr<RefHolderBase> RefVectorHolder<REFV>::refBase(size_t idx) const {
176  return std::shared_ptr<RefHolderBase>(std::make_shared<RefHolder<typename REFV::value_type> >(refs_[idx]));
177  }
178 
179  template <typename REFV>
180  std::shared_ptr<RefHolderBase> RefVectorHolder<REFV>::const_iterator_imp_specific::deref() const {
181  return std::shared_ptr<RefHolderBase>(std::make_shared<RefHolder<typename REFV::value_type> >(*i));
182  }
183  } // namespace reftobase
184 } // namespace edm
185 
186 #endif
bool less_than(const_iterator_imp const *o) const override
RefVectorHolderBase::const_iterator const_iterator
std::shared_ptr< reftobase::RefHolderBase > refBase(size_t idx) const override
EDProductGetter const * productGetter() const override
void assign(const_iterator_imp const *o) override
bool isAvailable() const override
void swap(RefVectorHolderBase &)
REF const & getRef() const
Definition: RefHolder_.h:78
difference_type difference(const_iterator_imp const *o) const override
#define CMS_CLASS_VERSION(_version_)
REFV::const_iterator const & dc(const_iterator_imp const *o) const
void reserve(size_type n) override
uint16_t size_type
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:86
void setRefs(REFV const &refs)
const_iterator begin() const override
std::shared_ptr< RefHolderBase > deref() const override
RefVectorHolder< REFV > * clone() const override
size_t keyForIndex(size_t idx) const override
RefVectorHolder(ProductID const &iId)
d
Definition: ztail.py:151
size_type size() const override
ProductIndex id() const
Definition: ProductID.h:35
bool empty() const override
const_iterator_imp_specific(typename REFV::const_iterator const &it)
RefVectorHolder< REFV > * cloneEmpty() const override
bool equal_to(const_iterator_imp const *o) const override
void swap(RefVectorHolder &other)
const_iterator end() const override
HLT enums.
const_iterator_imp_specific * clone() const override
void push_back(RefHolderBase const *r) override
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void swap(BaseHolder< T > &lhs, BaseHolder< T > &rhs)
Definition: BaseHolder.h:105
ProductID id() const override