File indexing completed on 2024-04-06 12:03:53
0001 #ifndef DataFormats_Common_RefCoreWithIndex_h
0002 #define DataFormats_Common_RefCoreWithIndex_h
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "DataFormats/Provenance/interface/ProductID.h"
0012 #include "DataFormats/Common/interface/traits.h"
0013 #include "DataFormats/Common/interface/refcore_implementation.h"
0014 #include "DataFormats/Common/interface/RefCore.h"
0015
0016 #include <algorithm>
0017 #include <typeinfo>
0018
0019 #include <atomic>
0020
0021 namespace edm {
0022 class EDProductGetter;
0023 class WrapperBase;
0024
0025 class RefCoreWithIndex {
0026 public:
0027 RefCoreWithIndex()
0028 : cachePtr_(nullptr), processIndex_(0), productIndex_(0), elementIndex_(edm::key_traits<unsigned int>::value) {}
0029
0030 RefCoreWithIndex(ProductID const& theId,
0031 void const* prodPtr,
0032 EDProductGetter const* prodGetter,
0033 bool transient,
0034 unsigned int elementIndex);
0035
0036 RefCoreWithIndex(RefCore const& iCore, unsigned int);
0037
0038 RefCoreWithIndex(RefCoreWithIndex const&);
0039
0040 RefCoreWithIndex& operator=(RefCoreWithIndex const&);
0041
0042 RefCoreWithIndex(RefCoreWithIndex&& iOther) noexcept
0043 : processIndex_(iOther.processIndex_),
0044 productIndex_(iOther.productIndex_),
0045 elementIndex_(iOther.elementIndex_) {
0046 cachePtr_.store(iOther.cachePtr_.load(std::memory_order_relaxed), std::memory_order_relaxed);
0047 }
0048
0049 RefCoreWithIndex& operator=(RefCoreWithIndex&& iOther) noexcept {
0050 cachePtr_.store(iOther.cachePtr_.load(std::memory_order_relaxed), std::memory_order_relaxed);
0051 processIndex_ = iOther.processIndex_;
0052 productIndex_ = iOther.productIndex_;
0053 elementIndex_ = iOther.elementIndex_;
0054 return *this;
0055 }
0056
0057 ~RefCoreWithIndex() noexcept {}
0058
0059 ProductID id() const { ID_IMPL; }
0060
0061
0062 void const* productPtr() const { PRODUCTPTR_IMPL; }
0063
0064
0065
0066
0067
0068 void setProductPtr(void const* prodPtr) const { setCacheIsProductPtr(prodPtr); }
0069
0070
0071
0072
0073
0074 bool tryToSetProductPtrForFirstTime(void const* prodPtr) const {
0075 return refcoreimpl::tryToSetCacheItemForFirstTime(cachePtr_, prodPtr);
0076 }
0077
0078 unsigned int index() const { return elementIndex_; }
0079
0080
0081 bool isNull() const { return !isNonnull(); }
0082
0083
0084 bool isNonnull() const { ISNONNULL_IMPL; }
0085
0086
0087 bool operator!() const { return isNull(); }
0088
0089
0090
0091
0092
0093 bool isAvailable() const { return toRefCore().isAvailable(); }
0094
0095
0096 RefCore const& toRefCore() const { return *reinterpret_cast<const RefCore*>(this); }
0097
0098 EDProductGetter const* productGetter() const { PRODUCTGETTER_IMPL; }
0099
0100 void setProductGetter(EDProductGetter const* prodGetter) const { toRefCore().setProductGetter(prodGetter); }
0101
0102 WrapperBase const* getProductPtr(std::type_info const& type, EDProductGetter const* prodGetter) const {
0103 return toRefCore().getProductPtr(type, prodGetter);
0104 }
0105
0106 void productNotFoundException(std::type_info const& type) const { toRefCore().productNotFoundException(type); }
0107
0108 void wrongTypeException(std::type_info const& expectedType, std::type_info const& actualType) const {
0109 toRefCore().wrongTypeException(expectedType, actualType);
0110 }
0111
0112 void nullPointerForTransientException(std::type_info const& type) const {
0113 toRefCore().nullPointerForTransientException(type);
0114 }
0115
0116 void swap(RefCoreWithIndex&);
0117
0118 bool isTransient() const { ISTRANSIENT_IMPL; }
0119
0120 int isTransientInt() const { return isTransient() ? 1 : 0; }
0121
0122 void pushBackItem(RefCoreWithIndex const& productToBeInserted, bool checkPointer) {
0123 toUnConstRefCore().pushBackItem(productToBeInserted.toRefCore(), checkPointer);
0124 }
0125
0126 private:
0127 RefCore& toUnConstRefCore() { return *reinterpret_cast<RefCore*>(this); }
0128
0129 void setId(ProductID const& iId) { toUnConstRefCore().setId(iId); }
0130 void setTransient() { SETTRANSIENT_IMPL; }
0131 void setCacheIsProductPtr(void const* iItem) const { SETCACHEISPRODUCTPTR_IMPL(iItem); }
0132 void setCacheIsProductGetter(EDProductGetter const* iGetter) const { SETCACHEISPRODUCTGETTER_IMPL(iGetter); }
0133
0134
0135
0136
0137
0138
0139
0140
0141 mutable std::atomic<void const*> cachePtr_;
0142
0143
0144
0145 ProcessIndex processIndex_;
0146 ProductIndex productIndex_;
0147 unsigned int elementIndex_;
0148 };
0149
0150 inline void RefCoreWithIndex::swap(RefCoreWithIndex& other) {
0151 std::swap(processIndex_, other.processIndex_);
0152 std::swap(productIndex_, other.productIndex_);
0153 other.cachePtr_.store(cachePtr_.exchange(other.cachePtr_.load()));
0154 std::swap(elementIndex_, other.elementIndex_);
0155 }
0156
0157 inline void swap(edm::RefCoreWithIndex& lhs, edm::RefCoreWithIndex& rhs) { lhs.swap(rhs); }
0158 }
0159
0160 #endif