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  typedef size_t size_type;
18  typedef T element_type;
21  virtual ~BaseVectorHolder() {}
22  virtual BaseVectorHolder* clone() const = 0;
23  virtual BaseVectorHolder* cloneEmpty() const = 0;
24  virtual base_ref_type const at(size_type idx) const = 0;
25  virtual bool empty() const = 0;
26 
27  virtual size_type size() const = 0;
28  //virtual size_type capacity() const = 0;
29  //virtual void reserve(size_type n) = 0;
30  virtual void clear() = 0;
31  virtual ProductID id() const = 0;
32  virtual EDProductGetter const* productGetter() const = 0;
33  void swap(BaseVectorHolder&) {} // nothing to swap
34 
35  // the following structure is public
36  // to allow dictionary to compile
37  // protected:
39  typedef ptrdiff_t difference_type;
41  virtual ~const_iterator_imp() {}
42  virtual const_iterator_imp* clone() const = 0;
43  virtual void increase() = 0;
44  virtual void decrease() = 0;
45  virtual void increase(difference_type d) = 0;
46  virtual void decrease(difference_type d) = 0;
47  virtual bool equal_to(const_iterator_imp const*) const = 0;
48  virtual bool less_than(const_iterator_imp const*) const = 0;
49  virtual void assign(const_iterator_imp const*) = 0;
50  virtual base_ref_type deref() const = 0;
51  virtual difference_type difference(const_iterator_imp const*) const = 0;
52  };
53 
54  struct const_iterator : public std::iterator<std::random_access_iterator_tag, RefToBase<T> > {
56  typedef std::unique_ptr<value_type> pointer;
57  typedef std::ptrdiff_t difference_type;
58 
59  const_iterator() : i(nullptr) {}
61  const_iterator(const_iterator const& it) : i(it.isValid() ? it.i->clone() : nullptr) {}
62  ~const_iterator() { delete i; }
64  if (this == &it) {
65  return *this;
66  }
67  if (isInvalid())
68  i = it.i;
69  else
70  i->assign(it.i);
71  return *this;
72  }
74  throwInvalidReference(isInvalid(), "increment");
75  i->increase();
76  return *this;
77  }
79  throwInvalidReference(isInvalid(), "postincrement");
80  const_iterator ci = *this;
81  i->increase();
82  return ci;
83  }
85  throwInvalidReference(isInvalid(), "decrement");
86  i->decrease();
87  return *this;
88  }
90  throwInvalidReference(isInvalid(), "postdecrement");
91  const_iterator ci = *this;
92  i->decrease();
93  return ci;
94  }
96  if (isInvalid() && o.isInvalid())
97  return 0;
98  throwInvalidReference(isInvalid() || o.isInvalid(), "compute difference with");
99  return i->difference(o.i);
100  }
102  throwInvalidReference(isInvalid(), "compute sum with");
104  ii->increase(n);
105  return const_iterator(ii);
106  }
108  throwInvalidReference(isInvalid(), "compute difference with");
110  ii->decrease(n);
111  return const_iterator(ii);
112  }
113  bool operator<(const_iterator const& o) const {
114  if (isInvalid() && o.isInvalid())
115  return false;
116  throwInvalidReference(isInvalid() || o.isInvalid(), "compute < operator with");
117  return i->less_than(o.i);
118  }
119  bool operator==(const_iterator const& ci) const {
120  if (isInvalid() && ci.isInvalid())
121  return true;
122  if (isInvalid() || ci.isInvalid())
123  return false;
124  return i->equal_to(ci.i);
125  }
126  bool operator!=(const_iterator const& ci) const {
127  if (isInvalid() && ci.isInvalid())
128  return false;
129  if (isInvalid() || ci.isInvalid())
130  return true;
131  return !i->equal_to(ci.i);
132  }
134  throwInvalidReference(isInvalid(), "dereference");
135  return i->deref();
136  }
137  pointer operator->() const { return pointer(new value_type(operator*())); }
139  throwInvalidReference(isInvalid(), "increment");
140  i->increase(d);
141  return *this;
142  }
144  throwInvalidReference(isInvalid(), "decrement");
145  i->decrease(d);
146  return *this;
147  }
148  bool isValid() const { return i != nullptr; }
149  bool isInvalid() const { return i == nullptr; }
150 
151  void throwInvalidReference(bool iIsInvalid, char const* iWhy) const {
152  if (iIsInvalid) {
154  errors::InvalidReference, "Trying to ", iWhy, " an invalid RefToBaseVector<T>::const_iterator");
155  }
156  }
157 
158  private:
160  };
161 
162  virtual const_iterator begin() const = 0;
163  virtual const_iterator end() const = 0;
164  virtual void push_back(BaseHolder<T> const*) = 0;
165  virtual std::unique_ptr<RefVectorHolderBase> vectorHolder() const = 0;
166 
169  virtual bool isAvailable() const = 0;
170 
171  //Used by ROOT storage
173  };
174 
175  // Free swap function
176  template <typename T>
177  inline void swap(BaseVectorHolder<T>& lhs, BaseVectorHolder<T>& rhs) {
178  lhs.swap(rhs);
179  }
180  } // namespace reftobase
181 } // namespace edm
182 
183 #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
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