File indexing completed on 2023-03-17 10:49:24
0001 #ifndef DataFormats_Common_PtrVectorBase_h
0002 #define DataFormats_Common_PtrVectorBase_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "DataFormats/Common/interface/RefCore.h"
0023
0024
0025 #include <typeinfo>
0026 #include <vector>
0027 #include <cassert>
0028
0029
0030
0031 namespace edm {
0032 class PtrVectorBase {
0033 public:
0034 typedef unsigned long key_type;
0035 typedef key_type size_type;
0036
0037 explicit PtrVectorBase(ProductID const& productID,
0038 void const* prodPtr = nullptr,
0039 EDProductGetter const* prodGetter = nullptr)
0040 : core_(productID, prodPtr, prodGetter, false), indicies_(), cachedItems_(nullptr) {}
0041
0042 PtrVectorBase(const PtrVectorBase&);
0043
0044 PtrVectorBase& operator=(const PtrVectorBase&) = delete;
0045
0046 virtual ~PtrVectorBase();
0047
0048
0049
0050 bool isNull() const { return !isNonnull(); }
0051
0052
0053
0054 bool isNonnull() const { return core_.isNonnull(); }
0055
0056
0057 bool operator!() const { return isNull(); }
0058
0059
0060 ProductID id() const { return core_.id(); }
0061
0062
0063 EDProductGetter const* productGetter() const { return core_.productGetter(); }
0064
0065 bool hasCache() const { return cachedItems_; }
0066
0067
0068
0069 bool isAvailable() const;
0070
0071
0072 bool empty() const { return indicies_.empty(); }
0073
0074
0075 size_type size() const { return indicies_.size(); }
0076
0077
0078 size_type capacity() const { return indicies_.capacity(); }
0079
0080
0081 void clear() {
0082 core_ = RefCore();
0083 indicies_.clear();
0084 if (cachedItems_) {
0085 delete cachedItems_.load();
0086 cachedItems_.store(nullptr);
0087 }
0088 }
0089
0090 bool operator==(PtrVectorBase const& iRHS) const;
0091
0092
0093
0094
0095 void reserve(size_type n) {
0096 indicies_.reserve(n);
0097 if (cachedItems_) {
0098 (*cachedItems_).reserve(n);
0099 }
0100 }
0101
0102 void setProductGetter(EDProductGetter* iGetter) const { core_.setProductGetter(iGetter); }
0103
0104 bool isTransient() const { return core_.isTransient(); }
0105
0106 void const* product() const { return nullptr; }
0107
0108 protected:
0109 PtrVectorBase();
0110
0111
0112 void swap(PtrVectorBase& other);
0113
0114 void push_back_base(RefCore const& core, key_type iKey, void const* iData);
0115
0116 std::vector<void const*>::const_iterator void_begin() const {
0117 getProduct_();
0118 if (not checkCachedItems()) {
0119 return emptyCache().begin();
0120 }
0121 return (*cachedItems_).begin();
0122 }
0123 std::vector<void const*>::const_iterator void_end() const {
0124 getProduct_();
0125 if (not checkCachedItems()) {
0126 return emptyCache().end();
0127 }
0128 return (*cachedItems_).end();
0129 }
0130
0131 template <typename TPtr>
0132 TPtr makePtr(unsigned long iIndex) const {
0133 if (isTransient()) {
0134 return TPtr(reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]), indicies_[iIndex]);
0135 }
0136 if (hasCache() && ((*cachedItems_)[iIndex] != nullptr || productGetter() == nullptr)) {
0137 return TPtr(
0138 this->id(), reinterpret_cast<typename TPtr::value_type const*>((*cachedItems_)[iIndex]), indicies_[iIndex]);
0139 }
0140 return TPtr(this->id(), indicies_[iIndex], productGetter());
0141 }
0142
0143 template <typename TPtr>
0144 TPtr makePtr(std::vector<void const*>::const_iterator const iIt) const {
0145 if (isTransient()) {
0146 return TPtr(reinterpret_cast<typename TPtr::value_type const*>(*iIt), indicies_[iIt - (*cachedItems_).begin()]);
0147 }
0148 if (hasCache() && (*iIt != nullptr || productGetter() == nullptr)) {
0149 return TPtr(this->id(),
0150 reinterpret_cast<typename TPtr::value_type const*>(*iIt),
0151 indicies_[iIt - (*cachedItems_).begin()]);
0152 }
0153 return TPtr(this->id(), indicies_[iIt - (*cachedItems_).begin()], productGetter());
0154 }
0155
0156 private:
0157 void getProduct_() const;
0158
0159 virtual std::type_info const& typeInfo() const {
0160 assert(false);
0161 return typeid(void);
0162 }
0163
0164
0165 bool checkCachedItems() const;
0166
0167
0168 static const std::vector<void const*>& emptyCache();
0169
0170
0171 RefCore core_;
0172 std::vector<key_type> indicies_;
0173 mutable std::atomic<std::vector<void const*>*> cachedItems_;
0174 };
0175 }
0176
0177 #endif