CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PtrVectorBase.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_PtrVectorBase_h
2 #define DataFormats_Common_PtrVectorBase_h
3 // -*- C++ -*-
4 //
5 // Package: Common
6 // Class : PtrVectorBase
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Wed Oct 24 15:26:45 EDT 2007
19 //
20 
21 // user include files
23 
24 // system include files
25 #include <typeinfo>
26 #include <vector>
27 #include <cassert>
28 
29 // forward declarations
30 
31 namespace edm {
32  class PtrVectorBase {
33 
34  public:
35  typedef unsigned long key_type;
37 
38  explicit PtrVectorBase(ProductID const& productID, void const* prodPtr = 0,
39  EDProductGetter const* prodGetter = 0)
40 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
41  :
42  core_(productID, prodPtr, prodGetter, false), indicies_(), cachedItems_(nullptr) {}
43 #else
44  ;
45 #endif
46 
48 
49  virtual ~PtrVectorBase();
50 
51  // ---------- const member functions ---------------------
53  bool isNull() const {return !isNonnull(); }
54 
56  //bool isNonnull() const {return id().isValid(); }
57  bool isNonnull() const { return core_.isNonnull(); }
58 
60  bool operator!() const {return isNull();}
61 
63  ProductID id() const {return core_.id();}
64 
66  EDProductGetter const* productGetter() const {return core_.productGetter();}
67 
68  bool hasCache() const { return cachedItems_; }
69 
72  bool isAvailable() const;
73 
75  bool empty() const {return indicies_.empty();}
76 
78  size_type size() const {return indicies_.size();}
79 
81  size_type capacity() const {return indicies_.capacity();}
82 
84  void clear()
85 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
86  { core_ = RefCore(); indicies_.clear(); if(cachedItems_) { delete cachedItems_.load(); cachedItems_.store(nullptr); } }
87 #else
88  ;
89 #endif
90 
91  bool operator==(PtrVectorBase const& iRHS) const;
92  // ---------- static member functions --------------------
93 
94  // ---------- member functions ---------------------------
96  void reserve(size_type n) {indicies_.reserve(n);
97  if(cachedItems_) {(*cachedItems_).reserve(n);} }
98 
99  void setProductGetter(EDProductGetter* iGetter) const { core_.setProductGetter(iGetter); }
100 
101  bool isTransient() const {return core_.isTransient();}
102 
103  void const* product() const {
104  return 0;
105  }
106 
107  protected:
108  PtrVectorBase();
109 
111  void swap(PtrVectorBase& other);
112 
113  void push_back_base(RefCore const& core, key_type iKey, void const* iData);
114 
115  std::vector<void const*>::const_iterator void_begin() const {
116  getProduct_();
117  if(not checkCachedItems()) {
118  return emptyCache().begin();
119  }
120  return (*cachedItems_).begin();
121  }
122  std::vector<void const*>::const_iterator void_end() const {
123  getProduct_();
124  if(not checkCachedItems()) {
125  return emptyCache().end();
126  }
127  return (*cachedItems_).end();
128  }
129 
130  template<typename TPtr>
131  TPtr makePtr(unsigned long iIndex) const {
132  if (isTransient()) {
133  return TPtr(reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]),
134  indicies_[iIndex]);
135  }
136  if (hasCache() && ((*cachedItems_)[iIndex] != nullptr || productGetter() == nullptr)) {
137  return TPtr(this->id(),
138  reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]),
139  indicies_[iIndex]);
140  }
141  return TPtr(this->id(), indicies_[iIndex], productGetter());
142  }
143 
144  template<typename TPtr>
145  TPtr makePtr(std::vector<void const*>::const_iterator const iIt) const {
146  if (isTransient()) {
147  return TPtr(reinterpret_cast<typename TPtr::value_type const*>(*iIt),
148  indicies_[iIt - (*cachedItems_).begin()]);
149  }
150  if (hasCache() && (*iIt != nullptr || productGetter() == nullptr)) {
151  return TPtr(this->id(),
152  reinterpret_cast<typename TPtr::value_type const*>(*iIt),
153  indicies_[iIt - (*cachedItems_).begin()]);
154  }
155  return TPtr(this->id(), indicies_[iIt - (*cachedItems_).begin()], productGetter());
156  }
157 
158  private:
159  void getProduct_() const;
160  //virtual std::type_info const& typeInfo() const = 0;
161  virtual std::type_info const& typeInfo() const {
162  assert(false);
163  return *reinterpret_cast<const std::type_info*>(0);
164  }
165 
166  //returns false if the cache is not yet set
167  bool checkCachedItems() const;
168 
170 
171  //Used when we need an iterator but cache is not yet set
172  static const std::vector<void const*>& emptyCache();
173 
174  // ---------- member data --------------------------------
176  std::vector<key_type> indicies_;
177 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
178  mutable std::atomic<std::vector<void const*>*> cachedItems_;
179 #else
180  mutable std::vector<void const*>* cachedItems_;
181 #endif
182 
183  };
184 }
185 
186 #endif
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:78
std::vector< void const * >::const_iterator void_begin() const
PtrVectorBase(ProductID const &productID, void const *prodPtr=0, EDProductGetter const *prodGetter=0)
Definition: PtrVectorBase.h:38
bool isNull() const
Checks for null.
Definition: PtrVectorBase.h:53
bool checkCachedItems() const
TPtr makePtr(unsigned long iIndex) const
assert(m_qm.get())
PtrVectorBase & operator=(const PtrVectorBase &)
bool empty() const
Is the RefVector empty.
Definition: PtrVectorBase.h:75
void swap(PtrVectorBase &other)
swap
bool isNonnull() const
Definition: RefCore.h:66
virtual std::type_info const & typeInfo() const
static const std::vector< void const * > & emptyCache()
void getProduct_() const
bool isTransient() const
ProductID id() const
Accessor for product ID.
Definition: PtrVectorBase.h:63
bool operator!() const
Checks for null.
Definition: PtrVectorBase.h:60
bool isNonnull() const
Checks for non-null.
Definition: PtrVectorBase.h:57
bool isAvailable() const
std::vector< key_type > indicies_
std::atomic< std::vector< void const * > * > cachedItems_
bool operator==(PtrVectorBase const &iRHS) const
void setProductGetter(EDProductGetter *iGetter) const
Definition: PtrVectorBase.h:99
size_type capacity() const
Capacity of the RefVector.
Definition: PtrVectorBase.h:81
virtual ~PtrVectorBase()
std::vector< void const * >::const_iterator void_end() const
bool hasCache() const
Definition: PtrVectorBase.h:68
TPtr makePtr(std::vector< void const * >::const_iterator const iIt) const
EDProductGetter const * productGetter() const
Definition: RefCore.h:76
unsigned long key_type
Definition: PtrVectorBase.h:35
ProductID id() const
Definition: RefCore.h:41
void reserve(size_type n)
Reserve space for RefVector.
Definition: PtrVectorBase.h:96
bool isTransient() const
Definition: RefCore.h:98
void clear()
Clear the PtrVector.
Definition: PtrVectorBase.h:84
void setProductGetter(EDProductGetter const *prodGetter) const
Definition: RefCore.cc:179
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:66
void const * product() const