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  typedef size_t size_type;
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  typedef ptrdiff_t difference_type;
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 : public std::iterator<std::random_access_iterator_tag, void*> {
48  typedef std::shared_ptr<RefHolderBase> value_type;
49  typedef std::ptrdiff_t difference_type;
50  const_iterator() : i(nullptr) {}
52  const_iterator(const_iterator const& it) : i(it.isValid() ? it.i->clone() : nullptr) {}
53  ~const_iterator() { delete i; }
55  if (isInvalid())
56  i = it.i;
57  else
58  i->assign(it.i);
59  return *this;
60  }
62  if (isInvalid())
64  "Trying to increment an inavlid RefToBaseVector<T>::const_iterator\n");
65  i->increase();
66  return *this;
67  }
69  if (isInvalid())
71  "Trying to postincrement an inavlid RefToBaseVector<T>::const_iterator\n");
72  const_iterator ci = *this;
73  i->increase();
74  return ci;
75  }
77  if (isInvalid())
79  "Trying to decrement an inavlid RefToBaseVector<T>::const_iterator\n");
80  i->decrease();
81  return *this;
82  }
84  if (isInvalid())
86  "Trying to postdecrement an inavlid RefToBaseVector<T>::const_iterator\n");
87  const_iterator ci = *this;
88  i->decrease();
89  return ci;
90  }
92  if (isInvalid() && o.isInvalid())
93  return 0;
94  if (isInvalid() || o.isInvalid())
96  "Trying to compute difference with an inavlid RefToBaseVector<T>::const_iterator\n");
97  return i->difference(o.i);
98  }
100  if (isInvalid())
102  "Trying to compute sum with an inavlid RefToBaseVector<T>::const_iterator\n");
104  ii->increase(n);
105  return const_iterator(ii);
106  }
108  if (isInvalid())
110  "Trying to compute difference with an inavlid RefToBaseVector<T>::const_iterator\n");
112  ii->decrease(n);
113  return const_iterator(ii);
114  }
115  bool operator<(const_iterator const& o) const {
116  if (isInvalid() && o.isInvalid())
117  return false;
118  if (isInvalid() || o.isInvalid())
120  "Trying to compute < operator with an inavlid RefToBaseVector<T>::const_iterator\n");
121  return i->less_than(o.i);
122  }
123  bool operator==(const const_iterator& ci) const {
124  if (isInvalid() && ci.isInvalid())
125  return true;
126  if (isInvalid() || ci.isInvalid())
127  return false;
128  return i->equal_to(ci.i);
129  }
130  bool operator!=(const const_iterator& ci) const {
131  if (isInvalid() && ci.isInvalid())
132  return false;
133  if (isInvalid() || ci.isInvalid())
134  return true;
135  return !i->equal_to(ci.i);
136  }
138  if (isInvalid())
140  "Trying to dereference an inavlid RefToBaseVector<T>::const_iterator\n");
141  return i->deref();
142  }
144  if (isInvalid())
146  "Trying to decrement an inavlid RefToBaseVector<T>::const_iterator\n");
147  i->decrease(d);
148  return *this;
149  }
151  if (isInvalid())
153  "Trying to increment an inavlid RefToBaseVector<T>::const_iterator\n");
154  i->increase(d);
155  return *this;
156  }
157  bool isValid() const { return i != nullptr; }
158  bool isInvalid() const { return i == nullptr; }
159 
160  private:
162  };
163 
164  virtual const_iterator begin() const = 0;
165  virtual const_iterator end() const = 0;
166  template <typename T>
167  RefToBase<T> getRef(size_t idx) const;
168  virtual size_t keyForIndex(size_t idx) const = 0;
169 
172  virtual bool isAvailable() const = 0;
173 
174  private:
175  virtual std::shared_ptr<reftobase::RefHolderBase> refBase(size_t idx) const = 0;
176  };
177 
178  template <typename T>
180  std::shared_ptr<reftobase::RefHolderBase> rb = refBase(idx);
181  return RefToBase<T>(rb);
182  }
183 
184  // Free swap function
185  inline void swap(RefVectorHolderBase& lhs, RefVectorHolderBase& rhs) { lhs.swap(rhs); }
186  } // namespace reftobase
187 } // namespace edm
188 
189 #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