CMS 3D CMS Logo

BaseVectorHolder.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_BaseVectorHolder_h
2 #define DataFormats_Common_BaseVectorHolder_h
6 #include <cstddef>
7 #include <memory>
8 
9 namespace edm {
10  class ProductID;
11  template <typename T>
12  class RefToBase;
13  namespace reftobase {
14  template <typename T>
15  class BaseVectorHolder {
16  public:
17  using size_type = size_t;
18  using element_type = T;
20 
22  virtual ~BaseVectorHolder() {}
23  virtual BaseVectorHolder* clone() const = 0;
24  virtual BaseVectorHolder* cloneEmpty() const = 0;
25  virtual base_ref_type const at(size_type idx) const = 0;
26  virtual bool empty() const = 0;
27 
28  virtual size_type size() const = 0;
29  //virtual size_type capacity() const = 0;
30  //virtual void reserve(size_type n) = 0;
31  virtual void clear() = 0;
32  virtual ProductID id() const = 0;
33  virtual EDProductGetter const* productGetter() const = 0;
34  void swap(BaseVectorHolder&) {} // nothing to swap
35 
36  // the following structure is public
37  // to allow dictionary to compile
38  // protected:
40  using difference_type = ptrdiff_t;
42  virtual ~const_iterator_imp() {}
43  virtual const_iterator_imp* clone() const = 0;
44  virtual void increase() = 0;
45  virtual void decrease() = 0;
46  virtual void increase(difference_type d) = 0;
47  virtual void decrease(difference_type d) = 0;
48  virtual bool equal_to(const_iterator_imp const*) const = 0;
49  virtual bool less_than(const_iterator_imp const*) const = 0;
50  virtual void assign(const_iterator_imp const*) = 0;
51  virtual base_ref_type deref() const = 0;
52  virtual difference_type difference(const_iterator_imp const*) const = 0;
53  };
54 
55  struct const_iterator {
56  using iterator_category = std::random_access_iterator_tag;
58  using pointer = std::unique_ptr<value_type>;
59  using difference_type = std::ptrdiff_t;
61 
62  const_iterator() : i(nullptr) {}
64  const_iterator(const_iterator const& it) : i(it.isValid() ? it.i->clone() : nullptr) {}
65  ~const_iterator() { delete i; }
67  if (this == &it) {
68  return *this;
69  }
70  if (isInvalid())
71  i = it.i;
72  else
73  i->assign(it.i);
74  return *this;
75  }
77  throwInvalidReference(isInvalid(), "increment");
78  i->increase();
79  return *this;
80  }
82  throwInvalidReference(isInvalid(), "postincrement");
83  const_iterator ci = *this;
84  i->increase();
85  return ci;
86  }
88  throwInvalidReference(isInvalid(), "decrement");
89  i->decrease();
90  return *this;
91  }
93  throwInvalidReference(isInvalid(), "postdecrement");
94  const_iterator ci = *this;
95  i->decrease();
96  return ci;
97  }
99  if (isInvalid() && o.isInvalid())
100  return 0;
101  throwInvalidReference(isInvalid() || o.isInvalid(), "compute difference with");
102  return i->difference(o.i);
103  }
105  throwInvalidReference(isInvalid(), "compute sum with");
107  ii->increase(n);
108  return const_iterator(ii);
109  }
111  throwInvalidReference(isInvalid(), "compute difference with");
113  ii->decrease(n);
114  return const_iterator(ii);
115  }
116  bool operator<(const_iterator const& o) const {
117  if (isInvalid() && o.isInvalid())
118  return false;
119  throwInvalidReference(isInvalid() || o.isInvalid(), "compute < operator with");
120  return i->less_than(o.i);
121  }
122  bool operator==(const_iterator const& ci) const {
123  if (isInvalid() && ci.isInvalid())
124  return true;
125  if (isInvalid() || ci.isInvalid())
126  return false;
127  return i->equal_to(ci.i);
128  }
129  bool operator!=(const_iterator const& ci) const {
130  if (isInvalid() && ci.isInvalid())
131  return false;
132  if (isInvalid() || ci.isInvalid())
133  return true;
134  return !i->equal_to(ci.i);
135  }
137  throwInvalidReference(isInvalid(), "dereference");
138  return i->deref();
139  }
140  pointer operator->() const { return pointer(new value_type(operator*())); }
142  throwInvalidReference(isInvalid(), "increment");
143  i->increase(d);
144  return *this;
145  }
147  throwInvalidReference(isInvalid(), "decrement");
148  i->decrease(d);
149  return *this;
150  }
151  bool isValid() const { return i != nullptr; }
152  bool isInvalid() const { return i == nullptr; }
153 
154  void throwInvalidReference(bool iIsInvalid, char const* iWhy) const {
155  if (iIsInvalid) {
157  errors::InvalidReference, "Trying to ", iWhy, " an invalid RefToBaseVector<T>::const_iterator");
158  }
159  }
160 
161  private:
163  };
164 
165  virtual const_iterator begin() const = 0;
166  virtual const_iterator end() const = 0;
167  virtual void push_back(BaseHolder<T> const*) = 0;
168  virtual std::unique_ptr<RefVectorHolderBase> vectorHolder() const = 0;
169 
172  virtual bool isAvailable() const = 0;
173 
174  //Used by ROOT storage
176  };
177 
178  // Free swap function
179  template <typename T>
180  inline void swap(BaseVectorHolder<T>& lhs, BaseVectorHolder<T>& rhs) {
181  lhs.swap(rhs);
182  }
183  } // namespace reftobase
184 } // namespace edm
185 
186 #endif
virtual ProductID id() const =0
virtual void push_back(BaseHolder< T > const *)=0
virtual BaseVectorHolder * cloneEmpty() const =0
virtual const_iterator begin() const =0
bool operator<(const_iterator const &o) const
const_iterator & operator+=(difference_type d)
virtual bool equal_to(const_iterator_imp const *) const =0
virtual difference_type difference(const_iterator_imp const *) const =0
virtual size_type size() const =0
#define CMS_CLASS_VERSION(_version_)
virtual EDProductGetter const * productGetter() 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 bool isAvailable() const =0
const_iterator operator+(difference_type n) const
bool operator==(const_iterator const &ci) const
const_iterator & operator=(const_iterator const &it)
virtual bool less_than(const_iterator_imp const *) const =0
virtual BaseVectorHolder * clone() const =0
d
Definition: ztail.py:151
ii
Definition: cuy.py:589
virtual const_iterator end() const =0
std::random_access_iterator_tag iterator_category
void throwInvalidReference(bool iIsInvalid, char const *iWhy) const
difference_type operator-(const_iterator const &o) const
HLT enums.
virtual std::unique_ptr< RefVectorHolderBase > vectorHolder() const =0
virtual const_iterator_imp * clone() const =0
void swap(BaseVectorHolder &)
const_iterator & operator-=(difference_type d)
virtual bool empty() const =0
const_iterator operator-(difference_type n) const
virtual base_ref_type const at(size_type idx) const =0
long double T
bool operator!=(const_iterator const &ci) const
void swap(BaseHolder< T > &lhs, BaseHolder< T > &rhs)
Definition: BaseHolder.h:105
virtual void assign(const_iterator_imp const *)=0