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 <stddef.h>
7 #include <memory>
8 
9 namespace edm {
10  class ProductID;
11  template<typename T> class RefToBase;
12  namespace reftobase {
13  template<typename T>
14  class BaseVectorHolder {
15  public:
16  typedef size_t size_type;
17  typedef T element_type;
20  virtual ~BaseVectorHolder() {}
21  virtual BaseVectorHolder* clone() const = 0;
22  virtual BaseVectorHolder* cloneEmpty() const = 0;
23  virtual base_ref_type const at(size_type idx) const = 0;
24  virtual bool empty() const = 0;
25 
26  virtual size_type size() const = 0;
27  //virtual size_type capacity() const = 0;
28  //virtual void reserve(size_type n) = 0;
29  virtual void clear() = 0;
30  virtual ProductID id() const = 0;
31  virtual EDProductGetter const* productGetter() const = 0;
32  void swap(BaseVectorHolder&) {} // nothing to swap
33 
34  // the following structure is public
35  // to allow dictionary to compile
36  // protected:
38  typedef ptrdiff_t difference_type;
40  virtual ~const_iterator_imp() { }
41  virtual const_iterator_imp* clone() const = 0;
42  virtual void increase() = 0;
43  virtual void decrease() = 0;
44  virtual void increase(difference_type d) = 0;
45  virtual void decrease(difference_type d) = 0;
46  virtual bool equal_to(const_iterator_imp const*) const = 0;
47  virtual bool less_than(const_iterator_imp const*) const = 0;
48  virtual void assign(const_iterator_imp const*) = 0;
49  virtual base_ref_type deref() const = 0;
50  virtual difference_type difference(const_iterator_imp const*) const = 0;
51  };
52 
53  struct const_iterator : public std::iterator <std::random_access_iterator_tag, RefToBase<T> >{
54  typedef base_ref_type value_type;
55  typedef std::unique_ptr<value_type> pointer;
56  typedef std::ptrdiff_t difference_type;
57 
60  const_iterator(const_iterator const& it) : i(it.isValid() ? it.i->clone() : nullptr) { }
61  ~const_iterator() { delete i; }
63  if(this == &it) {
64  return *this;
65  }
66  if (isInvalid()) i = it.i;
67  else i->assign(it.i);
68  return *this;
69  }
71  throwInvalidReference(isInvalid(), "increment");
72  i->increase();
73  return *this;
74  }
76  throwInvalidReference(isInvalid(), "postincrement");
77  const_iterator ci = *this;
78  i->increase();
79  return ci;
80  }
82  throwInvalidReference(isInvalid(), "decrement");
83  i->decrease();
84  return *this;
85  }
87  throwInvalidReference(isInvalid(), "postdecrement");
88  const_iterator ci = *this;
89  i->decrease();
90  return ci;
91  }
92  difference_type operator-(const_iterator const& o) const {
93  if (isInvalid() && o.isInvalid()) return 0;
94  throwInvalidReference(isInvalid() || o.isInvalid(), "compute difference with");
95  return i->difference(o.i);
96  }
97  const_iterator operator+(difference_type n) const {
98  throwInvalidReference(isInvalid(), "compute sum with");
99  const_iterator_imp* ii = i->clone();
100  ii->increase(n);
101  return const_iterator(ii);
102  }
103  const_iterator operator-(difference_type n) const {
104  throwInvalidReference(isInvalid(), "compute difference with");
105  const_iterator_imp* ii = i->clone();
106  ii->decrease(n);
107  return const_iterator(ii);
108  }
109  bool operator<(const_iterator const& o) const {
110  if (isInvalid() && o.isInvalid()) return false;
111  throwInvalidReference(isInvalid() || o.isInvalid(), "compute < operator with");
112  return i->less_than(o.i);
113  }
114  bool operator==(const_iterator const& ci) const {
115  if (isInvalid() && ci.isInvalid()) return true;
116  if (isInvalid() || ci.isInvalid()) return false;
117  return i->equal_to(ci.i);
118  }
119  bool operator!=(const_iterator const& ci) const {
120  if (isInvalid() && ci.isInvalid()) return false;
121  if (isInvalid() || ci.isInvalid()) return true;
122  return ! i->equal_to(ci.i);
123  }
124  value_type operator*() const {
125  throwInvalidReference(isInvalid(), "dereference");
126  return i->deref();
127  }
128  pointer operator->()const {
129  return pointer(new value_type(operator*()));
130  }
131  const_iterator& operator+=(difference_type d) {
132  throwInvalidReference(isInvalid(), "increment");
133  i->increase(d);
134  return *this;
135  }
136  const_iterator& operator-=(difference_type d) {
137  throwInvalidReference(isInvalid(), "decrement");
138  i->decrease(d);
139  return *this;
140  }
141  bool isValid() const { return i != nullptr; }
142  bool isInvalid() const { return i == nullptr; }
143 
144  void throwInvalidReference(bool iIsInvalid, char const* iWhy) const {
145  if (iIsInvalid) {
146  Exception::throwThis(errors::InvalidReference, "Trying to ", iWhy, " an invalid RefToBaseVector<T>::const_iterator");
147  }
148  }
149 
150  private:
152  };
153 
154  virtual const_iterator begin() const = 0;
155  virtual const_iterator end() const = 0;
156  virtual void push_back(BaseHolder<T> const*) = 0;
157  virtual std::unique_ptr<RefVectorHolderBase> vectorHolder() const = 0;
158 
161  virtual bool isAvailable() const = 0;
162 
163  //Used by ROOT storage
165  };
166 
167  // Free swap function
168  template<typename T>
169  inline
170  void
172  lhs.swap(rhs);
173  }
174  }
175 }
176 
177 #endif
virtual void push_back(BaseHolder< T > const *)=0
const_iterator & operator+=(difference_type d)
const_iterator operator+(difference_type n) const
#define nullptr
bool operator<(const_iterator const &o) const
virtual bool less_than(const_iterator_imp const *) const =0
bool operator!=(const_iterator const &ci) const
virtual BaseVectorHolder * cloneEmpty() const =0
#define CMS_CLASS_VERSION(_version_)
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:83
virtual difference_type difference(const_iterator_imp const *) const =0
difference_type operator-(const_iterator const &o) const
const_iterator & operator=(const_iterator const &it)
bool operator==(const_iterator const &ci) const
virtual base_ref_type const at(size_type idx) const =0
const_iterator operator-(difference_type n) const
ii
Definition: cuy.py:590
virtual bool equal_to(const_iterator_imp const *) const =0
virtual EDProductGetter const * productGetter() const =0
virtual size_type size() const =0
virtual std::unique_ptr< RefVectorHolderBase > vectorHolder() const =0
void throwInvalidReference(bool iIsInvalid, char const *iWhy) const
virtual const_iterator end() const =0
HLT enums.
virtual const_iterator begin() const =0
virtual ProductID id() const =0
void swap(BaseVectorHolder &)
const_iterator & operator-=(difference_type d)
long double T
virtual bool isAvailable() const =0
virtual bool empty() const =0
virtual const_iterator_imp * clone() const =0
virtual BaseVectorHolder * clone() const =0
virtual void assign(const_iterator_imp const *)=0