CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RefVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefVector_h
2 #define DataFormats_Common_RefVector_h
3 
4 /*----------------------------------------------------------------------
5 
6 RefVector: A template for a vector of interproduct references.
7  Each vector element is a reference to a member of the same product.
8 
9 $Id: RefVector.h,v 1.40 2011/02/24 20:20:48 wmtan Exp $
10 
11 ----------------------------------------------------------------------*/
12 
13 #include <vector>
14 #include <stdexcept>
26 
27 namespace edm {
28 
29  template <typename T>
30  T const* getProduct(RefCore const& ref);
31 
32  template <typename C,
33  typename T = typename refhelper::ValueTrait<C>::value,
34  typename F = typename refhelper::FindTrait<C, T>::value>
35  class RefVector {
36  public:
37  typedef C collection_type;
38  typedef T member_type;
39  typedef F finder_type;
43  typedef value_type const const_reference; // better this than the default 'const value_type &'
44  typedef const_reference reference; // as operator[] returns 'const R' and not 'R &'
45 
46  // key_type is the type of the key into the collection
47  typedef typename value_type::key_type key_type;
48  typedef std::vector<key_type> KeyVec;
49 
50  // size_type is the type of the index into the RefVector
51  typedef typename KeyVec::size_type size_type;
53 
57 
58  RefVector(ProductID const& id) : refVector_(id) {}
60  void push_back(value_type const& ref)
61  {refVector_.pushBack(ref.refCore(), ref.key());}
62 
64  value_type const operator[](size_type idx) const {
65  key_type const& key = refVector_.keys()[idx];
66  RefCore const& prod = refVector_.refCore();
67  return value_type(prod, key);
68  }
69 
71  value_type const at(size_type idx) const {
72  key_type const& key = refVector_.keys().at(idx);
73  RefCore const& prod = refVector_.refCore();
74  return value_type(prod, key);
75  }
76 
78  contents_type const& refVector() const {return refVector_;}
79 
81  bool empty() const {return refVector_.empty();}
82 
84  size_type size() const {return refVector_.size();}
85 
87  size_type capacity() const {return refVector_.capacity();}
88 
91 
93  const_iterator begin() const;
94 
96  const_iterator end() const;
97 
99  ProductID id() const {return refVector_.refCore().id();}
100 
103 
105  bool isNull() const {return !id().isValid();}
106 
108  bool isNonnull() const {return !isNull();}
109 
111  bool operator!() const {return isNull();}
112 
114  // Accessor must get the product if necessary
115  C const* product() const;
116 
119  bool isAvailable() const {return refVector_.refCore().isAvailable();}
120 
122  bool isTransient() const {return refVector_.refCore().isTransient();}
123 
125  iterator erase(iterator const& pos);
126 
128  void clear() {refVector_.clear();}
129 
131  void swap(RefVector<C, T, F> & other);
132 
134  RefVector& operator=(RefVector const& rhs);
135 
137  bool hasProductCache() const {return refVector_.refCore().productPtr() != 0;}
138 
139  void fillView(ProductID const& id,
140  std::vector<void const*>& pointers,
141  helper_vector& helpers) const;
142 
143  //Needed for ROOT storage
145  private:
147  };
148 
149  template <typename C, typename T, typename F>
150  inline
151  void
152  RefVector<C, T, F>::swap(RefVector<C, T, F> & other) {
153  refVector_.swap(other.refVector_);
154  }
155 
156  template <typename C, typename T, typename F>
157  inline
161  this->swap(temp);
162  return *this;
163  }
164 
165  template <typename C, typename T, typename F>
166  inline
167  void
169  a.swap(b);
170  }
171 
172  template <typename C, typename T, typename F>
173  void
175  std::vector<void const*>& pointers,
176  helper_vector& helpers) const
177  {
178  typedef Ref<C,T,F> ref_type;
179  typedef reftobase::RefHolder<ref_type> holder_type;
180 
181  pointers.reserve(this->size());
182  helpers.reserve(this->size());
183 
184  size_type key = 0;
185  for (const_iterator i=begin(), e=end(); i!=e; ++i, ++key) {
186  member_type const* address = i->isNull() ? 0 : &**i;
187  pointers.push_back(address);
188  holder_type h(ref_type(i->id(), address, i->key(), product() ));
189  helpers.push_back( & h );
190  }
191  }
192 
193 
194  template <typename C, typename T, typename F>
195  inline
196  void
198  ProductID const& id,
199  std::vector<void const*>& pointers,
200  helper_vector& helpers)
201  {
202  obj.fillView(id, pointers, helpers);
203  }
204 
205  template <typename C, typename T, typename F>
206  struct has_fillView<edm::RefVector<C,T,F> >
207  {
208  static bool const value = true;
209  };
210 
211  template <typename C, typename T, typename F>
212  inline
213  bool
215  return lhs.refVector() == rhs.refVector();
216  }
217 
218  template <typename C, typename T, typename F>
219  inline
220  bool
222  return !(lhs == rhs);
223  }
224 
225  template <typename C, typename T, typename F>
226  inline
227  typename RefVector<C, T, F>::iterator
230  typename contents_type::keys_type::iterator newPos =
231  refVector_.eraseAtIndex(index);
232  RefCore const& prod = refVector_.refCore();
233  //return typename RefVector<C, T, F>::iterator(prod, newPos);
234  return iterator(prod, newPos);
235  }
236 
237  template <typename C, typename T, typename F>
239  return iterator(refVector_.refCore(), refVector_.keys().begin());
240  }
241 
242  template <typename C, typename T, typename F>
244  return iterator(refVector_.refCore(), refVector_.keys().end());
245  }
246 
247  template <typename C, typename T, typename F>
248  std::ostream&
249  operator<< (std::ostream& os, RefVector<C,T,F> const& r)
250  {
252  i = r.begin(),
253  e = r.end();
254  i != e;
255  ++i)
256  {
257  os << *i << '\n';
258  }
259  return os;
260  }
261 
262 }
263 
265 
266 namespace edm {
267 
268  template <typename C, typename T, typename F>
269  C const* RefVector<C,T,F>::product() const {
270  return isNull() ? 0 : edm::template getProduct<C>(refVector_.refCore());
271  }
272 
273 }
274 
276 namespace edm {
277  namespace detail {
278 
279  template<typename C, typename T, typename F>
280  struct GetProduct<RefVector<C, T, F> > {
281  typedef T element_type;
283  static const element_type * address( const iter & i ) {
284  return &**i;
285  }
286  static const C * product( const RefVector<C, T, F> & coll ) {
287  return coll.product();
288  }
289  };
290  }
291 }
292 #endif
int i
Definition: DBlmapReader.cc:9
bool isNonnull() const
Checks for non-null.
Definition: RefVector.h:108
char * address
Definition: mlp_lapack.h:14
C const * product() const
Accessor for product collection.
Definition: RefVector.h:269
value_type const const_reference
Definition: RefVector.h:43
value_type::key_type key_type
Definition: RefVector.h:47
void fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers) const
Definition: RefVector.h:174
size_type capacity() const
Capacity of the RefVector.
Definition: RefVector.h:87
bool empty() const
Is vector empty?
Definition: RefVectorBase.h:43
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
const_reference reference
Definition: RefVector.h:44
contents_type refVector_
Definition: RefVector.h:146
keys_type::iterator eraseAtIndex(size_type index)
erase an element from the vector
Definition: RefVectorBase.h:60
#define CMS_CLASS_VERSION(_version_)
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:243
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:81
uint16_t size_type
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:238
void pushBack(RefCore const &product, KEY const &key)
Definition: RefVectorBase.h:48
KeyVec::size_type size_type
Definition: RefVector.h:51
bool isNull() const
Checks for null.
Definition: RefVector.h:105
void swap(RefVectorBase< KEY > &other)
swap two vectors
Definition: RefVectorBase.h:71
tuple obj
Example code starts here #.
Definition: VarParsing.py:655
size_type capacity() const
Capacity of vector.
Definition: RefVectorBase.h:54
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:99
std::vector< key_type > KeyVec
Definition: RefVector.h:48
RefCore const & refCore() const
Accessor for product ID and product getter.
Definition: RefVectorBase.h:37
static const element_type * address(const iter &i)
Definition: RefVector.h:283
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool isTransient() const
Checks if product collection is tansient (i.e. non persistable)
Definition: RefVector.h:122
void const * productPtr() const
Definition: RefCore.h:26
bool isAvailable() const
Definition: RefCore.cc:103
refhelper::RefVectorTrait< C, T, F >::iterator_type iterator
Definition: RefVector.h:40
void clear()
clear the vector
Definition: RefVectorBase.h:65
void swap(RefVector< C, T, F > &other)
Swap two vectors.
Definition: RefVector.h:152
keys_type const & keys() const
Accessor for vector of keys and pointers.
Definition: RefVectorBase.h:40
T const * getProduct(RefCore const &ref)
Definition: RefCoreGet.h:37
RefCore const & refCore() const
Definition: Ref.h:280
void reserve(size_type n)
Reserve space for RefVector.
Definition: RefVector.h:90
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: RefVector.h:102
iterator const_iterator
Definition: RefVector.h:41
RefVectorBase< key_type > contents_type
Definition: RefVector.h:52
virtual void push_back(const RefHolderBase *r)=0
JetCorrectorParametersCollection coll
Definition: classes.h:14
void clear()
Clear the vector.
Definition: RefVector.h:128
static const C * product(const RefVector< C, T, F > &coll)
Definition: RefVector.h:286
bool hasProductCache() const
Checks if product is in memory.
Definition: RefVector.h:137
key_type key() const
Accessor for product key.
Definition: Ref.h:265
RefVector< C, T, F >::const_iterator iter
Definition: RefVector.h:282
double b
Definition: hdecay.h:120
virtual void reserve(size_type n)=0
RefVector & operator=(RefVector const &rhs)
Copy assignment.
Definition: RefVector.h:159
EDProductGetter const * productGetter() const
Definition: RefCore.h:47
ProductID id() const
Definition: RefCore.h:23
value_type const operator[](size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:64
#define private
Definition: FWFileEntry.h:18
bool isTransient() const
Definition: RefCore.h:64
value_type const at(size_type idx) const
Retrieve an element of the RefVector.
Definition: RefVector.h:71
bool isAvailable() const
Definition: RefVector.h:119
list key
Definition: combine.py:13
double a
Definition: hdecay.h:121
void push_back(value_type const &ref)
Add a Ref&lt;C, T&gt; to the RefVector.
Definition: RefVector.h:60
keys_type::size_type size_type
Definition: RefVectorBase.h:25
size_type size() const
Size of the RefVector.
Definition: RefVector.h:84
refhelper::RefVectorTrait< C, T, F >::ref_type value_type
Definition: RefVector.h:42
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:169
bool isValid() const
Definition: ProductID.h:36
contents_type const & refVector() const
Accessor for all data.
Definition: RefVector.h:78
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
size_type size() const
Size of vector.
Definition: RefVectorBase.h:46
long double T
bool operator!() const
Checks for null.
Definition: RefVector.h:111
edm::RefVector< Container > RefVector
RefVector(ProductID const &id)
Definition: RefVector.h:58
def template
Definition: svgfig.py:520
void reserve(size_type n)
Reserve space for vector.
Definition: RefVectorBase.h:57
iterator erase(iterator const &pos)
Erase an element from the vector.
Definition: RefVector.h:228