CMS 3D CMS Logo

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  public:
34  typedef unsigned long key_type;
36 
37  explicit PtrVectorBase(ProductID const& productID,
38  void const* prodPtr = nullptr,
39  EDProductGetter const* prodGetter = nullptr)
40  : core_(productID, prodPtr, prodGetter, false), indicies_(), cachedItems_(nullptr) {}
41 
43 
44  virtual ~PtrVectorBase();
45 
46  // ---------- const member functions ---------------------
48  bool isNull() const { return !isNonnull(); }
49 
51  //bool isNonnull() const {return id().isValid(); }
52  bool isNonnull() const { return core_.isNonnull(); }
53 
55  bool operator!() const { return isNull(); }
56 
58  ProductID id() const { return core_.id(); }
59 
61  EDProductGetter const* productGetter() const { return core_.productGetter(); }
62 
63  bool hasCache() const { return cachedItems_; }
64 
67  bool isAvailable() const;
68 
70  bool empty() const { return indicies_.empty(); }
71 
73  size_type size() const { return indicies_.size(); }
74 
76  size_type capacity() const { return indicies_.capacity(); }
77 
79  void clear() {
80  core_ = RefCore();
81  indicies_.clear();
82  if (cachedItems_) {
83  delete cachedItems_.load();
84  cachedItems_.store(nullptr);
85  }
86  }
87 
88  bool operator==(PtrVectorBase const& iRHS) const;
89  // ---------- static member functions --------------------
90 
91  // ---------- member functions ---------------------------
94  indicies_.reserve(n);
95  if (cachedItems_) {
96  (*cachedItems_).reserve(n);
97  }
98  }
99 
100  void setProductGetter(EDProductGetter* iGetter) const { core_.setProductGetter(iGetter); }
101 
102  bool isTransient() const { return core_.isTransient(); }
103 
104  void const* product() const { return nullptr; }
105 
106  protected:
107  PtrVectorBase();
108 
110  void swap(PtrVectorBase& other);
111 
112  void push_back_base(RefCore const& core, key_type iKey, void const* iData);
113 
114  std::vector<void const*>::const_iterator void_begin() const {
115  getProduct_();
116  if (not checkCachedItems()) {
117  return emptyCache().begin();
118  }
119  return (*cachedItems_).begin();
120  }
121  std::vector<void const*>::const_iterator void_end() const {
122  getProduct_();
123  if (not checkCachedItems()) {
124  return emptyCache().end();
125  }
126  return (*cachedItems_).end();
127  }
128 
129  template <typename TPtr>
130  TPtr makePtr(unsigned long iIndex) const {
131  if (isTransient()) {
132  return TPtr(reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]), indicies_[iIndex]);
133  }
134  if (hasCache() && ((*cachedItems_)[iIndex] != nullptr || productGetter() == nullptr)) {
135  return TPtr(
136  this->id(), reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]), indicies_[iIndex]);
137  }
138  return TPtr(this->id(), indicies_[iIndex], productGetter());
139  }
140 
141  template <typename TPtr>
142  TPtr makePtr(std::vector<void const*>::const_iterator const iIt) const {
143  if (isTransient()) {
144  return TPtr(reinterpret_cast<typename TPtr::value_type const*>(*iIt), indicies_[iIt - (*cachedItems_).begin()]);
145  }
146  if (hasCache() && (*iIt != nullptr || productGetter() == nullptr)) {
147  return TPtr(this->id(),
148  reinterpret_cast<typename TPtr::value_type const*>(*iIt),
149  indicies_[iIt - (*cachedItems_).begin()]);
150  }
151  return TPtr(this->id(), indicies_[iIt - (*cachedItems_).begin()], productGetter());
152  }
153 
154  private:
155  void getProduct_() const;
156  //virtual std::type_info const& typeInfo() const = 0;
157  virtual std::type_info const& typeInfo() const {
158  assert(false);
159  return typeid(void);
160  }
161 
162  //returns false if the cache is not yet set
163  bool checkCachedItems() const;
164 
165  PtrVectorBase& operator=(const PtrVectorBase&) = delete;
166 
167  //Used when we need an iterator but cache is not yet set
168  static const std::vector<void const*>& emptyCache();
169 
170  // ---------- member data --------------------------------
172  std::vector<key_type> indicies_;
173  mutable std::atomic<std::vector<void const*>*> cachedItems_;
174  };
175 } // namespace edm
176 
177 #endif
edm::PtrVectorBase::PtrVectorBase
PtrVectorBase(ProductID const &productID, void const *prodPtr=nullptr, EDProductGetter const *prodGetter=nullptr)
Definition: PtrVectorBase.h:37
edm::RefCore
Definition: RefCore.h:21
edm::RefCore::setProductGetter
void setProductGetter(EDProductGetter const *prodGetter) const
Definition: RefCore.cc:156
edm::PtrVectorBase::size
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:73
edm::PtrVectorBase::getProduct_
void getProduct_() const
Definition: PtrVectorBase.cc:105
funct::false
false
Definition: Factorize.h:29
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
edm::PtrVectorBase::isNull
bool isNull() const
Checks for null.
Definition: PtrVectorBase.h:48
edm::PtrVectorBase::push_back_base
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
Definition: PtrVectorBase.cc:62
edm
HLT enums.
Definition: AlignableModifier.h:19
cms::cuda::assert
assert(be >=bs)
edm::PtrVectorBase::productGetter
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:61
edm::PtrVectorBase::product
void const * product() const
Definition: PtrVectorBase.h:104
edm::PtrVectorBase::capacity
size_type capacity() const
Capacity of the RefVector.
Definition: PtrVectorBase.h:76
edm::PtrVectorBase::checkCachedItems
bool checkCachedItems() const
Definition: PtrVectorBase.cc:156
edm::PtrVectorBase::isTransient
bool isTransient() const
Definition: PtrVectorBase.h:102
edm::RefCore::productGetter
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
edm::PtrVectorBase::setProductGetter
void setProductGetter(EDProductGetter *iGetter) const
Definition: PtrVectorBase.h:100
edm::EDProductGetter
Definition: EDProductGetter.h:41
edm::PtrVectorBase::indicies_
std::vector< key_type > indicies_
Definition: PtrVectorBase.h:172
edm::PtrVectorBase::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: PtrVectorBase.h:52
edm::PtrVectorBase::operator!
bool operator!() const
Checks for null.
Definition: PtrVectorBase.h:55
trackingPlots.other
other
Definition: trackingPlots.py:1464
edm::PtrVectorBase::~PtrVectorBase
virtual ~PtrVectorBase()
Definition: PtrVectorBase.cc:37
edm::PtrVectorBase::core_
RefCore core_
Definition: PtrVectorBase.h:171
edm::PtrVectorBase::size_type
key_type size_type
Definition: PtrVectorBase.h:35
edm::RefCore::isTransient
bool isTransient() const
Definition: RefCore.h:105
core
Definition: __init__.py:1
edm::PtrVectorBase::operator==
bool operator==(PtrVectorBase const &iRHS) const
Definition: PtrVectorBase.cc:170
edm::PtrVectorBase::empty
bool empty() const
Is the RefVector empty.
Definition: PtrVectorBase.h:70
edm::PtrVectorBase::cachedItems_
std::atomic< std::vector< void const * > * > cachedItems_
Definition: PtrVectorBase.h:173
edm::PtrVectorBase::emptyCache
static const std::vector< void const * > & emptyCache()
Definition: PtrVectorBase.cc:186
edm::PtrVectorBase::isAvailable
bool isAvailable() const
Definition: PtrVectorBase.cc:83
edm::RefCore::id
ProductID id() const
Definition: RefCore.h:48
edm::PtrVectorBase::id
ProductID id() const
Accessor for product ID.
Definition: PtrVectorBase.h:58
edm::PtrVectorBase::clear
void clear()
Clear the PtrVector.
Definition: PtrVectorBase.h:79
edm::PtrVectorBase::PtrVectorBase
PtrVectorBase()
Definition: PtrVectorBase.cc:35
edm::PtrVectorBase::void_end
std::vector< void const * >::const_iterator void_end() const
Definition: PtrVectorBase.h:121
edm::PtrVectorBase::key_type
unsigned long key_type
Definition: PtrVectorBase.h:34
edm::PtrVectorBase::typeInfo
virtual std::type_info const & typeInfo() const
Definition: PtrVectorBase.h:157
edm::PtrVectorBase::void_begin
std::vector< void const * >::const_iterator void_begin() const
Definition: PtrVectorBase.h:114
funct::void
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
edm::PtrVectorBase::hasCache
bool hasCache() const
Definition: PtrVectorBase.h:63
RefCore.h
edm::PtrVectorBase
Definition: PtrVectorBase.h:32
edm::PtrVectorBase::makePtr
TPtr makePtr(unsigned long iIndex) const
Definition: PtrVectorBase.h:130
edm::PtrVectorBase::operator=
PtrVectorBase & operator=(const PtrVectorBase &)=delete
edm::ProductID
Definition: ProductID.h:27
edm::PtrVectorBase::makePtr
TPtr makePtr(std::vector< void const * >::const_iterator const iIt) const
Definition: PtrVectorBase.h:142
edm::PtrVectorBase::swap
void swap(PtrVectorBase &other)
swap
Definition: PtrVectorBase.cc:56
edm::PtrVectorBase::reserve
void reserve(size_type n)
Reserve space for RefVector.
Definition: PtrVectorBase.h:93
edm::RefCore::isNonnull
bool isNonnull() const
Definition: RefCore.h:71