CMS 3D CMS Logo

RefVectorHolderBase.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefVectorHolderBase_h
2 #define DataFormats_Common_RefVectorHolderBase_h
3 
6 #include <cstddef>
7 #include <memory>
8 
9 namespace edm {
10  template <typename T>
11  class RefToBase;
12  namespace reftobase {
14  public:
15  virtual ~RefVectorHolderBase() {}
16  using size_type = size_t;
18  void swap(RefVectorHolderBase&) {} // nothing to swap
19  virtual bool empty() const = 0;
20  virtual size_type size() const = 0;
21  virtual void clear() = 0;
22  virtual void reserve(size_type n) = 0;
23  virtual ProductID id() const = 0;
24  virtual EDProductGetter const* productGetter() const = 0;
25  virtual RefVectorHolderBase* clone() const = 0;
26  virtual RefVectorHolderBase* cloneEmpty() const = 0;
27  virtual void push_back(RefHolderBase const* r) = 0;
28  // the following structure is public
29  // to allow dictionary to compile
30  // protected:
32  using difference_type = ptrdiff_t;
34  virtual ~const_iterator_imp() {}
35  virtual const_iterator_imp* clone() const = 0;
36  virtual void increase() = 0;
37  virtual void decrease() = 0;
38  virtual void increase(difference_type d) = 0;
39  virtual void decrease(difference_type d) = 0;
40  virtual bool equal_to(const_iterator_imp const*) const = 0;
41  virtual bool less_than(const_iterator_imp const*) const = 0;
42  virtual void assign(const_iterator_imp const*) = 0;
43  virtual std::shared_ptr<RefHolderBase> deref() const = 0;
44  virtual difference_type difference(const_iterator_imp const*) const = 0;
45  };
46 
47  struct const_iterator {
48  using iterator_category = std::random_access_iterator_tag;
49  using value_type = std::shared_ptr<RefHolderBase>;
50  using pointer = void**;
51  using reference = void*&;
52  using difference_type = std::ptrdiff_t;
53  const_iterator() : i(nullptr) {}
55  const_iterator(const_iterator const& it) : i(it.isValid() ? it.i->clone() : nullptr) {}
56  ~const_iterator() { delete i; }
58  if (isInvalid())
59  i = it.i;
60  else
61  i->assign(it.i);
62  return *this;
63  }
65  if (isInvalid())
67  "Trying to increment an inavlid RefToBaseVector<T>::const_iterator\n");
68  i->increase();
69  return *this;
70  }
72  if (isInvalid())
74  "Trying to postincrement an inavlid RefToBaseVector<T>::const_iterator\n");
75  const_iterator ci = *this;
76  i->increase();
77  return ci;
78  }
80  if (isInvalid())
82  "Trying to decrement an inavlid RefToBaseVector<T>::const_iterator\n");
83  i->decrease();
84  return *this;
85  }
87  if (isInvalid())
89  "Trying to postdecrement an inavlid RefToBaseVector<T>::const_iterator\n");
90  const_iterator ci = *this;
91  i->decrease();
92  return ci;
93  }
95  if (isInvalid() && o.isInvalid())
96  return 0;
97  if (isInvalid() || o.isInvalid())
99  "Trying to compute difference with an inavlid RefToBaseVector<T>::const_iterator\n");
100  return i->difference(o.i);
101  }
103  if (isInvalid())
105  "Trying to compute sum with an inavlid RefToBaseVector<T>::const_iterator\n");
107  ii->increase(n);
108  return const_iterator(ii);
109  }
111  if (isInvalid())
113  "Trying to compute difference with an inavlid RefToBaseVector<T>::const_iterator\n");
115  ii->decrease(n);
116  return const_iterator(ii);
117  }
118  bool operator<(const_iterator const& o) const {
119  if (isInvalid() && o.isInvalid())
120  return false;
121  if (isInvalid() || o.isInvalid())
123  "Trying to compute < operator with an inavlid RefToBaseVector<T>::const_iterator\n");
124  return i->less_than(o.i);
125  }
126  bool operator==(const const_iterator& ci) const {
127  if (isInvalid() && ci.isInvalid())
128  return true;
129  if (isInvalid() || ci.isInvalid())
130  return false;
131  return i->equal_to(ci.i);
132  }
133  bool operator!=(const const_iterator& ci) const {
134  if (isInvalid() && ci.isInvalid())
135  return false;
136  if (isInvalid() || ci.isInvalid())
137  return true;
138  return !i->equal_to(ci.i);
139  }
141  if (isInvalid())
143  "Trying to dereference an inavlid RefToBaseVector<T>::const_iterator\n");
144  return i->deref();
145  }
147  if (isInvalid())
149  "Trying to decrement an inavlid RefToBaseVector<T>::const_iterator\n");
150  i->decrease(d);
151  return *this;
152  }
154  if (isInvalid())
156  "Trying to increment an inavlid RefToBaseVector<T>::const_iterator\n");
157  i->increase(d);
158  return *this;
159  }
160  bool isValid() const { return i != nullptr; }
161  bool isInvalid() const { return i == nullptr; }
162 
163  private:
165  };
166 
167  virtual const_iterator begin() const = 0;
168  virtual const_iterator end() const = 0;
169  template <typename T>
170  RefToBase<T> getRef(size_t idx) const;
171  virtual size_t keyForIndex(size_t idx) const = 0;
172 
175  virtual bool isAvailable() const = 0;
176 
177  private:
178  virtual std::shared_ptr<reftobase::RefHolderBase> refBase(size_t idx) const = 0;
179  };
180 
181  template <typename T>
183  std::shared_ptr<reftobase::RefHolderBase> rb = refBase(idx);
184  return RefToBase<T>(rb);
185  }
186 
187  // Free swap function
188  inline void swap(RefVectorHolderBase& lhs, RefVectorHolderBase& rhs) { lhs.swap(rhs); }
189  } // namespace reftobase
190 } // namespace edm
191 
192 #endif
void swap(RefVectorHolderBase &)
virtual const_iterator_imp * clone() const =0
virtual RefVectorHolderBase * clone() const =0
virtual bool empty() const =0
virtual RefVectorHolderBase * cloneEmpty() const =0
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:86
virtual size_t keyForIndex(size_t idx) const =0
virtual size_type size() const =0
const_iterator operator+(difference_type n) const
virtual std::shared_ptr< RefHolderBase > deref() const =0
virtual void assign(const_iterator_imp const *)=0
virtual EDProductGetter const * productGetter() const =0
d
Definition: ztail.py:151
ii
Definition: cuy.py:589
virtual difference_type difference(const_iterator_imp const *) const =0
virtual const_iterator begin() const =0
RefToBase< T > getRef(size_t idx) const
virtual bool less_than(const_iterator_imp const *) const =0
bool operator!=(const const_iterator &ci) const
virtual void reserve(size_type n)=0
const_iterator & operator=(const_iterator const &it)
HLT enums.
const_iterator operator-(difference_type n) const
virtual const_iterator end() const =0
virtual ProductID id() const =0
virtual void push_back(RefHolderBase const *r)=0
bool operator==(const const_iterator &ci) const
virtual bool isAvailable() const =0
virtual std::shared_ptr< reftobase::RefHolderBase > refBase(size_t idx) const =0
virtual bool equal_to(const_iterator_imp const *) const =0
difference_type operator-(const_iterator const &o) const
void swap(BaseHolder< T > &lhs, BaseHolder< T > &rhs)
Definition: BaseHolder.h:105