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