CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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  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  PtrVectorBase& operator=(const PtrVectorBase&) = delete;
45 
46  virtual ~PtrVectorBase();
47 
48  // ---------- const member functions ---------------------
50  bool isNull() const { return !isNonnull(); }
51 
53  //bool isNonnull() const {return id().isValid(); }
54  bool isNonnull() const { return core_.isNonnull(); }
55 
57  bool operator!() const { return isNull(); }
58 
60  ProductID id() const { return core_.id(); }
61 
63  EDProductGetter const* productGetter() const { return core_.productGetter(); }
64 
65  bool hasCache() const { return cachedItems_; }
66 
69  bool isAvailable() const;
70 
72  bool empty() const { return indicies_.empty(); }
73 
75  size_type size() const { return indicies_.size(); }
76 
78  size_type capacity() const { return indicies_.capacity(); }
79 
81  void clear() {
82  core_ = RefCore();
83  indicies_.clear();
84  if (cachedItems_) {
85  delete cachedItems_.load();
86  cachedItems_.store(nullptr);
87  }
88  }
89 
90  bool operator==(PtrVectorBase const& iRHS) const;
91  // ---------- static member functions --------------------
92 
93  // ---------- member functions ---------------------------
96  indicies_.reserve(n);
97  if (cachedItems_) {
98  (*cachedItems_).reserve(n);
99  }
100  }
101 
102  void setProductGetter(EDProductGetter* iGetter) const { core_.setProductGetter(iGetter); }
103 
104  bool isTransient() const { return core_.isTransient(); }
105 
106  void const* product() const { return nullptr; }
107 
108  protected:
109  PtrVectorBase();
110 
112  void swap(PtrVectorBase& other);
113 
114  void push_back_base(RefCore const& core, key_type iKey, void const* iData);
115 
116  std::vector<void const*>::const_iterator void_begin() const {
117  getProduct_();
118  if (not checkCachedItems()) {
119  return emptyCache().begin();
120  }
121  return (*cachedItems_).begin();
122  }
123  std::vector<void const*>::const_iterator void_end() const {
124  getProduct_();
125  if (not checkCachedItems()) {
126  return emptyCache().end();
127  }
128  return (*cachedItems_).end();
129  }
130 
131  template <typename TPtr>
132  TPtr makePtr(unsigned long iIndex) const {
133  if (isTransient()) {
134  return TPtr(reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]), indicies_[iIndex]);
135  }
136  if (hasCache() && ((*cachedItems_)[iIndex] != nullptr || productGetter() == nullptr)) {
137  return TPtr(
138  this->id(), reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]), indicies_[iIndex]);
139  }
140  return TPtr(this->id(), indicies_[iIndex], productGetter());
141  }
142 
143  template <typename TPtr>
144  TPtr makePtr(std::vector<void const*>::const_iterator const iIt) const {
145  if (isTransient()) {
146  return TPtr(reinterpret_cast<typename TPtr::value_type const*>(*iIt), indicies_[iIt - (*cachedItems_).begin()]);
147  }
148  if (hasCache() && (*iIt != nullptr || productGetter() == nullptr)) {
149  return TPtr(this->id(),
150  reinterpret_cast<typename TPtr::value_type const*>(*iIt),
151  indicies_[iIt - (*cachedItems_).begin()]);
152  }
153  return TPtr(this->id(), indicies_[iIt - (*cachedItems_).begin()], productGetter());
154  }
155 
156  private:
157  void getProduct_() const;
158  //virtual std::type_info const& typeInfo() const = 0;
159  virtual std::type_info const& typeInfo() const {
160  assert(false);
161  return typeid(void);
162  }
163 
164  //returns false if the cache is not yet set
165  bool checkCachedItems() const;
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
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:75
std::vector< void const * >::const_iterator void_begin() const
bool isNull() const
Checks for null.
Definition: PtrVectorBase.h:50
bool checkCachedItems() const
TPtr makePtr(unsigned long iIndex) const
PtrVectorBase & operator=(const PtrVectorBase &)=delete
bool empty() const
Is the RefVector empty.
Definition: PtrVectorBase.h:72
void swap(PtrVectorBase &other)
swap
bool isNonnull() const
Definition: RefCore.h:71
assert(be >=bs)
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:60
bool operator!() const
Checks for null.
Definition: PtrVectorBase.h:57
bool isNonnull() const
Checks for non-null.
Definition: PtrVectorBase.h:54
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
size_type capacity() const
Capacity of the RefVector.
Definition: PtrVectorBase.h:78
virtual ~PtrVectorBase()
std::vector< void const * >::const_iterator void_end() const
bool hasCache() const
Definition: PtrVectorBase.h:65
TPtr makePtr(std::vector< void const * >::const_iterator const iIt) const
EDProductGetter const * productGetter() const
Definition: RefCore.h:81
unsigned long key_type
Definition: PtrVectorBase.h:34
ProductID id() const
Definition: RefCore.h:48
void reserve(size_type n)
Reserve space for RefVector.
Definition: PtrVectorBase.h:95
bool isTransient() const
Definition: RefCore.h:105
void clear()
Clear the PtrVector.
Definition: PtrVectorBase.h:81
PtrVectorBase(ProductID const &productID, void const *prodPtr=nullptr, EDProductGetter const *prodGetter=nullptr)
Definition: PtrVectorBase.h:37
void setProductGetter(EDProductGetter const *prodGetter) const
Definition: RefCore.cc:156
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:63
void const * product() const