CMS 3D CMS Logo

RefVectorBase.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefVectorBase_h
2 #define DataFormats_Common_RefVectorBase_h
3 
4 /*----------------------------------------------------------------------
5 
6 RefVectorBase: Base class for a vector of interproduct references.
7 
8 
9 ----------------------------------------------------------------------*/
10 
14 #include <vector>
15 
16 namespace edm {
17 
18  class EDProductGetter;
19 
21  public:
23  std::vector<void const*> const& memberPointers() const { return memberPointers_; }
24  std::vector<void const*>& memberPointers() { return memberPointers_; }
25  private:
26  std::vector<void const*> memberPointers_;
27  };
28 
29  template <typename KEY>
30  class RefVectorBase {
31  public:
32  typedef std::vector<KEY > keys_type;
33  typedef KEY key_type;
34  typedef typename keys_type::size_type size_type;
36  RefVectorBase() : product_(), keys_() {}
37  RefVectorBase( RefVectorBase const & rhs) : product_(rhs.product_), keys_(rhs.keys_),
38  memberPointersHolder_(rhs.memberPointersHolder_) {}
39  RefVectorBase( RefVectorBase && rhs) noexcept : product_(std::move(rhs.product_)), keys_(std::move(rhs.keys_)),
40  memberPointersHolder_(std::move(rhs.memberPointersHolder_)) {}
41 
43  RefVectorBase temp(rhs);
44  this->swap(temp);
45  return *this;
46  }
48  product_ = std::move(rhs.product_);
49  keys_ =std::move(rhs.keys_);
50  memberPointersHolder_ = std::move(rhs.memberPointersHolder_);
51  return *this;
52  }
53 
54 
55 
56  explicit RefVectorBase(ProductID const& productID, void const* prodPtr = nullptr,
57  EDProductGetter const* prodGetter = nullptr) :
58  product_(productID, prodPtr, prodGetter, false), keys_() {}
59 
60 
63 
65  RefCore const& refCore() const {return product_;}
66 
67  void const* cachedMemberPointer(size_type idx) const {
68  return memberPointers().empty() ? nullptr : memberPointers()[idx];
69  }
70 
72  keys_type const& keys() const {return keys_;}
73 
75  bool empty() const {return keys_.empty();}
76 
78  size_type size() const {return keys_.size();}
79 
80  void pushBack(RefCore const& product, KEY const& key) {
81  product_.pushBackRefItem(product);
82  if(product.productPtr() != nullptr) {
83  if(memberPointers().empty()) {
84  memberPointersHolder_.memberPointers().resize(keys_.size(), nullptr);
85  }
86  memberPointersHolder_.memberPointers().push_back(product.productPtr());
87  keys_.push_back(key);
88  return;
89  } else {
90  if(!memberPointers().empty()) {
91  memberPointersHolder_.memberPointers().push_back(nullptr);
92  }
93  keys_.push_back(key);
94  }
95  }
96 
98  size_type capacity() const {return keys_.capacity();}
99 
101  void reserve(size_type n) {keys_.reserve(n);}
102 
104  typename keys_type::iterator eraseAtIndex(size_type index) {
105  memberPointersHolder_.memberPointers().erase(memberPointersHolder_.memberPointers().begin() + index);
106  return keys_.erase(keys_.begin() + index);
107  }
108 
110  void clear() {
111  keys_.clear();
112  memberPointersHolder_.memberPointers().clear();
113  product_ = RefCore();
114  }
115 
118  product_.swap(other.product_);
119  keys_.swap(other.keys_);
120  memberPointersHolder_.memberPointers().swap(other.memberPointersHolder_.memberPointers());
121  }
122 
123 
124  //Needed for ROOT storage
126 
127  private:
128 
129  std::vector<void const*> const& memberPointers() const { return memberPointersHolder_.memberPointers(); }
130 
132  keys_type keys_;
134  };
135 
137  template<typename KEY>
138  bool
140  return lhs.refCore() == rhs.refCore() && lhs.keys() == rhs.keys();
141  }
142 
144  template<typename KEY>
145  bool
147  return !(lhs == rhs);
148  }
149 
151  template<typename KEY>
152  inline
153  void
155  a.swap(b);
156  }
157 
158 }
159 #endif
void const * cachedMemberPointer(size_type idx) const
Definition: RefVectorBase.h:67
size_type size() const
Size of vector.
Definition: RefVectorBase.h:78
void swap(RefVectorBase< KEY > &other) noexcept
swap two vectors
#define noexcept
void clear()
clear the vector
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
uint16_t size_type
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
std::vector< void const * > const & memberPointers() const
Definition: RefVectorBase.h:23
RefVectorBase(RefVectorBase &&rhs) noexcept
Definition: RefVectorBase.h:39
~RefVectorBase() noexcept
Destructor.
Definition: RefVectorBase.h:62
RefCore const & refCore() const
Accessor for product ID and product getter.
Definition: RefVectorBase.h:65
std::vector< void const * > & memberPointers()
Definition: RefVectorBase.h:24
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
bool empty() const
Is vector empty?
Definition: RefVectorBase.h:75
void const * productPtr() const
Definition: RefCore.h:52
keys_type::size_type size_type
Definition: RefVectorBase.h:34
RefVectorBase()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: RefVectorBase.h:36
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
RefVectorMemberPointersHolder memberPointersHolder_
void pushBack(RefCore const &product, KEY const &key)
Definition: RefVectorBase.h:80
std::vector< void const * > memberPointers_
Definition: RefVectorBase.h:26
RefVectorBase & operator=(RefVectorBase const &rhs)
Definition: RefVectorBase.h:42
RefVectorBase(RefVectorBase const &rhs)
Definition: RefVectorBase.h:37
double b
Definition: hdecay.h:120
RefVectorBase(ProductID const &productID, void const *prodPtr=nullptr, EDProductGetter const *prodGetter=nullptr)
Definition: RefVectorBase.h:56
HLT enums.
double a
Definition: hdecay.h:121
keys_type const & keys() const
Accessor for vector of keys and pointers.
Definition: RefVectorBase.h:72
RefVectorBase & operator=(RefVectorBase &&rhs) noexcept
Definition: RefVectorBase.h:47
std::vector< KEY > keys_type
Definition: RefVectorBase.h:32
keys_type::iterator eraseAtIndex(size_type index)
erase an element from the vector
void reserve(size_type n)
Reserve space for vector.
size_type capacity() const
Capacity of vector.
Definition: RefVectorBase.h:98
def move(src, dest)
Definition: eostools.py:511