CMS 3D CMS Logo

PtrVectorBase.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Common
4 // Class : PtrVectorBase
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Wed Oct 24 15:49:27 EDT 2007
11 //
12 
13 // user include files
20 
21 // system include files
22 #include <ostream>
23 
24 //
25 // constants, enums and typedefs
26 //
27 namespace edm {
28 //
29 // static data member definitions
30 //
31 
32 //
33 // constructor and destructor
34 //
36  }
37 
39  delete cachedItems_.load();
40  }
41 
43  core_(iOther.core_),
44  indicies_(iOther.indicies_),
46  {
47  auto cache = iOther.cachedItems_.load();
48  if(cache) {
49  cachedItems_.store( new std::vector<void const*>(*cache));
50  }
51  }
52 
53 //
54 // assignment operators
55 //
56 
57 //
58 // member functions
59 //
60 
62  void
64  core_.swap(other.core_);
65  indicies_.swap(other.indicies_);
66  other.cachedItems_.store(cachedItems_.exchange(other.cachedItems_.load()));
67  }
68 
69  void
70  PtrVectorBase::push_back_base(RefCore const& core, key_type iKey, void const* iData) {
71  core_.pushBackItem(core, false);
72  //Did we already push a 'non-cached' Ptr into the container or is this a 'non-cached' Ptr?
73  if(not cachedItems_ and indicies_.empty()) {
74  cachedItems_.store( new std::vector<void const*>());
75  (*cachedItems_).reserve(indicies_.capacity());
76  }
77  auto tmpCachedItems = cachedItems_.load();
78  if(tmpCachedItems and (indicies_.size() == (*tmpCachedItems).size())) {
79  if(iData) {
80  tmpCachedItems->push_back(iData);
81  } else if(key_traits<key_type>::value == iKey) {
82  tmpCachedItems->push_back(nullptr);
83  } else {
84  delete tmpCachedItems;
85  cachedItems_.store(nullptr);
86  }
87  }
88  indicies_.push_back(iKey);
89  }
90 
91  bool
93  if(indicies_.empty()) {
94  return core_.isAvailable();
95  }
96  if(!hasCache()) {
97  if(!id().isValid() || productGetter() == nullptr) {
98  return false;
99  }
100  getProduct_();
101  }
102  auto tmpCachedItems = cachedItems_.load();
103  for(auto ptr : *tmpCachedItems) {
104  if(ptr == nullptr) {
105  return false;
106  }
107  }
108  return true;
109  }
110 
111 //
112 // const member functions
113 //
114  void
116  if(hasCache()) {
117  return;
118  }
119  if(indicies_.size() == 0) {
120  return;
121  }
122  //NOTE: Another thread could be getting the data
123  auto tmpProductGetter = productGetter();
124  if(nullptr == tmpProductGetter) {
125  throw Exception(errors::LogicError) << "Tried to get data for a PtrVector which has no EDProductGetter\n";
126  }
127  WrapperBase const* product = tmpProductGetter->getIt(id());
128 
129  auto tmpCachedItems = std::make_unique<std::vector<void const*>>();
130 
131  if(product != nullptr) {
132  product->fillPtrVector(typeInfo(), indicies_, *tmpCachedItems);
133 
134  std::vector<void const*>* expected = nullptr;
135  if(cachedItems_.compare_exchange_strong(expected, tmpCachedItems.get())) {
136  //we were the first thread to change the value
137  tmpCachedItems.release();
138  }
139 
140  return;
141  }
142 
143  tmpCachedItems->resize(indicies_.size(), nullptr);
144 
145  std::vector<unsigned int> thinnedKeys;
146  thinnedKeys.assign(indicies_.begin(), indicies_.end());
147  std::vector<WrapperBase const*> wrappers(indicies_.size(), nullptr);
148  tmpProductGetter->getThinnedProducts(id(), wrappers, thinnedKeys);
149  unsigned int nWrappers = wrappers.size();
150  assert(wrappers.size() == indicies_.size());
151  assert(wrappers.size() == tmpCachedItems->size());
152  for(unsigned k = 0; k < nWrappers; ++k) {
153  if (wrappers[k] != nullptr) {
154  wrappers[k]->setPtr(typeInfo(), thinnedKeys[k], (*tmpCachedItems)[k]);
155  }
156  }
157  {
158  std::vector<void const*>* expected = nullptr;
159  if(cachedItems_.compare_exchange_strong(expected, tmpCachedItems.get())) {
160  //we were the first thread to change the value
161  tmpCachedItems.release();
162  }
163  }
164  }
165 
166  bool
168  auto tmp = cachedItems_.load();
169  if(not tmp) { return false;}
170  for(auto item : *tmp) {
171  if(item == nullptr) {
172  throw Exception(errors::InvalidReference) << "Asked for data from a PtrVector which refers to a non-existent product with ProductID "
173  << id() << "\n";
174  }
175  }
176  return true;
177  }
178 
179  bool
181  if(core_ != iRHS.core_) {
182  return false;
183  }
184  if(indicies_.size() != iRHS.indicies_.size()){
185  return false;
186  }
187  return std::equal(indicies_.begin(),
188  indicies_.end(),
189  iRHS.indicies_.begin());
190  }
191 
192 //
193 // static member functions
194 //
195 
196  static const std::vector<void const*> s_emptyCache{};
197 
198  const std::vector<void const*>& PtrVectorBase::emptyCache() {
199  return s_emptyCache;
200  }
201 
202 }
void push_back_base(RefCore const &core, key_type iKey, void const *iData)
void pushBackItem(RefCore const &productToBeInserted, bool checkPointer)
Definition: RefCore.cc:194
bool checkCachedItems() const
void swap(RefCore &) noexcept
Definition: RefCore.h:157
Definition: __init__.py:1
void swap(PtrVectorBase &other)
swap
static const std::vector< void const * > s_emptyCache
#define nullptr
virtual std::type_info const & typeInfo() const
bool equal(const T &first, const T &second)
Definition: Equal.h:34
static const std::vector< void const * > & emptyCache()
void getProduct_() const
ProductID id() const
Accessor for product ID.
Definition: PtrVectorBase.h:59
bool isAvailable() const
std::vector< key_type > indicies_
std::atomic< std::vector< void const * > * > cachedItems_
bool isAvailable() const
Definition: RefCore.cc:169
bool operator==(PtrVectorBase const &iRHS) const
int k[5][pyjets_maxn]
void fillPtrVector(std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr) const
Definition: WrapperBase.cc:34
virtual ~PtrVectorBase()
bool hasCache() const
Definition: PtrVectorBase.h:64
unsigned long key_type
Definition: PtrVectorBase.h:35
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
HLT enums.
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:62
void const * product() const
Definition: PtrVectorBase.h:95