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  ~RefVectorHolder() override {}
22  void swap(RefVectorHolder& other);
24  bool empty() const override;
25  size_type size() const override;
26  void clear() override;
27  void push_back(RefHolderBase const* r) override;
28  void reserve(size_type n) override;
29  ProductID id() const override;
30  EDProductGetter const* productGetter() const override;
31  RefVectorHolder<REFV>* clone() const override;
32  RefVectorHolder<REFV>* cloneEmpty() const override;
33  void setRefs(REFV const& refs);
34  size_t keyForIndex(size_t idx) const override;
35 
36  //Needed for ROOT storage
38 
39  private:
41 
42  public:
44  typedef ptrdiff_t difference_type;
46  explicit const_iterator_imp_specific(typename REFV::const_iterator const& it) : i(it) {}
49  void increase() override { ++i; }
50  void decrease() override { --i; }
51  void increase(difference_type d) override { i += d; }
52  void decrease(difference_type d) override { i -= d; }
53  bool equal_to(const_iterator_imp const* o) const override { return i == dc(o); }
54  bool less_than(const_iterator_imp const* o) const override { return i < dc(o); }
55  void assign(const_iterator_imp const* o) override { i = dc(o); }
56  std::shared_ptr<RefHolderBase> deref() const override;
57  difference_type difference(const_iterator_imp const* o) const override { return i - dc(o); }
58 
59  private:
60  typename REFV::const_iterator const& dc(const_iterator_imp const* o) const {
61  if (o == nullptr) {
62  Exception::throwThis(errors::InvalidReference, "In RefVectorHolder trying to dereference a null pointer\n");
63  }
65  if (oo == nullptr) {
67  "In RefVectorHolder trying to cast iterator to wrong type\n");
68  }
69  return oo->i;
70  }
71  typename REFV::const_iterator i;
72  };
73 
75 
76  const_iterator begin() const override { return const_iterator(new const_iterator_imp_specific(refs_.begin())); }
77  const_iterator end() const override { return const_iterator(new const_iterator_imp_specific(refs_.end())); }
78 
81  bool isAvailable() const override { return refs_.isAvailable(); }
82 
83  private:
84  std::shared_ptr<reftobase::RefHolderBase> refBase(size_t idx) const override;
85  REFV refs_;
86  };
87 
88  //
89  // implementations for RefVectorHolder<REFV>
90  //
91 
92  template <typename REFV>
94  this->RefVectorHolderBase::swap(other);
95  refs_.swap(other.refs_);
96  }
97 
98  template <typename REFV>
101  this->swap(temp);
102  return *this;
103  }
104 
105  template <typename REFV>
106  inline bool RefVectorHolder<REFV>::empty() const {
107  return refs_.empty();
108  }
109 
110  template <typename REFV>
112  return refs_.size();
113  }
114 
115  template <typename REFV>
117  return refs_.clear();
118  }
119 
120  template <typename REFV>
122  typename REFV::size_type s = n;
123  refs_.reserve(s);
124  }
125 
126  template <typename REFV>
128  return refs_.id();
129  }
130 
131  template <typename REFV>
133  return refs_.productGetter();
134  }
135 
136  template <typename REFV>
138  return new RefVectorHolder<REFV>(*this);
139  }
140 
141  template <typename REFV>
143  return new RefVectorHolder<REFV>(id());
144  }
145 
146  template <typename REFV>
147  inline void RefVectorHolder<REFV>::setRefs(REFV const& refs) {
148  refs_ = refs;
149  }
150 
151  template <typename REFV>
152  inline size_t RefVectorHolder<REFV>::keyForIndex(size_t idx) const {
153  return refs_[idx].key();
154  }
155 
156  // Free swap function
157  template <typename REFV>
159  lhs.swap(rhs);
160  }
161  } // namespace reftobase
162 } // namespace edm
163 
165 
166 namespace edm {
167  namespace reftobase {
168 
169  template <typename REFV>
171  typedef typename REFV::value_type REF;
172  RefHolder<REF> const* rh = dynamic_cast<RefHolder<REF> const*>(h);
173  if (rh == nullptr) {
175  "RefVectorHolder: attempting to cast a RefHolderBase "
176  "to an invalid type.\nExpected: ",
177  typeid(REF).name(),
178  "\n");
179  }
180  refs_.push_back(rh->getRef());
181  }
182 
183  template <typename REFV>
184  std::shared_ptr<RefHolderBase> RefVectorHolder<REFV>::refBase(size_t idx) const {
185  return std::shared_ptr<RefHolderBase>(std::make_shared<RefHolder<typename REFV::value_type> >(refs_[idx]));
186  }
187 
188  template <typename REFV>
189  std::shared_ptr<RefHolderBase> RefVectorHolder<REFV>::const_iterator_imp_specific::deref() const {
190  return std::shared_ptr<RefHolderBase>(std::make_shared<RefHolder<typename REFV::value_type> >(*i));
191  }
192  } // namespace reftobase
193 } // namespace edm
194 
195 #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
RefVectorHolder & operator=(RefVectorHolder const &rhs)
#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:85
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
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