CMS 3D CMS Logo

RefCore.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_RefCore_h
2 #define DataFormats_Common_RefCore_h
3 
4 /*----------------------------------------------------------------------
5 
6 RefCore: The component of edm::Ref containing the product ID and product getter.
7 
8 ----------------------------------------------------------------------*/
11 
12 #include <algorithm>
13 #include <typeinfo>
14 #include <atomic>
15 
16 namespace edm {
17  class RefCoreWithIndex;
18  class EDProductGetter;
19  class WrapperBase;
20 
21  class RefCore {
22  //RefCoreWithIndex is a specialization of RefCore done for performance
23  // Since we need to freely convert one to the other the friendship is used
24  friend class RefCoreWithIndex;
25  public:
27 
28  RefCore(ProductID const& theId, void const* prodPtr, EDProductGetter const* prodGetter, bool transient);
29 
30  RefCore( RefCore const&);
31 
32  RefCore& operator=(RefCore const&);
33 
34  RefCore( RefCore && iOther) noexcept :
37  cachePtr_.store(iOther.cachePtr_.load(std::memory_order_relaxed), std::memory_order_relaxed);
38  }
39 
41  cachePtr_.store(iOther.cachePtr_.load(std::memory_order_relaxed), std::memory_order_relaxed);
42  processIndex_ = iOther.processIndex_;
43  productIndex_ = iOther.productIndex_;
44  return *this;
45  }
46 
48 
49  ProductID id() const {ID_IMPL;}
50 
52  void const* productPtr() const {PRODUCTPTR_IMPL;}
53 
58  void setProductPtr(void const* prodPtr) const {
59  setCacheIsProductPtr(prodPtr);
60  }
61 
66  bool tryToSetProductPtrForFirstTime(void const* prodPtr) const {
68  }
69 
70  // Checks for null
71  bool isNull() const {return !isNonnull(); }
72 
73  // Checks for non-null
74  bool isNonnull() const {ISNONNULL_IMPL;}
75 
76  // Checks for null
77  bool operator!() const {return isNull();}
78 
79  // Checks if collection is in memory or available
80  // in the Event. No type checking is done.
81 
82  bool isAvailable() const;
83 
84  EDProductGetter const* productGetter() const {
86  }
87 
88  void setProductGetter(EDProductGetter const* prodGetter) const;
89 
90  WrapperBase const* getProductPtr(std::type_info const& type, EDProductGetter const* prodGetter) const;
91 
92  WrapperBase const* tryToGetProductPtr(std::type_info const& type, EDProductGetter const* prodGetter) const;
93 
94  WrapperBase const* getThinnedProductPtr(std::type_info const& type, unsigned int& thinnedKey, EDProductGetter const* prodGetter) const;
95 
96  bool isThinnedAvailable(unsigned int thinnedKey, EDProductGetter const* prodGetter) const;
97 
98  void productNotFoundException(std::type_info const& type) const;
99 
100  void wrongTypeException(std::type_info const& expectedType, std::type_info const& actualType) const;
101 
102  void nullPointerForTransientException(std::type_info const& type) const;
103 
104  void swap(RefCore &) noexcept;
105 
107 
108  int isTransientInt() const {return isTransient() ? 1 : 0;}
109 
110  void pushBackItem(RefCore const& productToBeInserted, bool checkPointer);
111 
112  void pushBackRefItem(RefCore const& productToBeInserted);
113 
114  private:
115  RefCore(void const* iCache, ProcessIndex iProcessIndex, ProductIndex iProductIndex):
116  cachePtr_(iCache), processIndex_(iProcessIndex), productIndex_(iProductIndex) {}
117  void setId(ProductID const& iId);
119  void setCacheIsProductPtr(const void* iItem) const {SETCACHEISPRODUCTPTR_IMPL(iItem);}
121  bool cachePtrIsInvalid() const { return 0 == (reinterpret_cast<std::uintptr_t>(cachePtr_.load()) & refcoreimpl::kCacheIsProductPtrMask); }
122 
123  //The low bit of the address is used to determine if the cachePtr_
124  // is storing the productPtr or the EDProductGetter. The bit is set if
125  // the address refers to the EDProductGetter.
126  mutable std::atomic<void const*> cachePtr_; // transient
127  //The following is what is stored in a ProductID
128  // the high bit of processIndex is used to store info on
129  // if this is transient.
130  //If the type or order of the member data is changes you MUST also update
131  // the custom streamer in RefCoreStreamer.cc and RefCoreWithIndex
134 
135  };
136 
137  inline
138  bool
139  operator==(RefCore const& lhs, RefCore const& rhs) {
140  return lhs.isTransient() == rhs.isTransient() && (lhs.isTransient() ? lhs.productPtr() == rhs.productPtr() : lhs.id() == rhs.id());
141  }
142 
143  inline
144  bool
145  operator!=(RefCore const& lhs, RefCore const& rhs) {
146  return !(lhs == rhs);
147  }
148 
149  inline
150  bool
151  operator<(RefCore const& lhs, RefCore const& rhs) {
152  return lhs.isTransient() ? (rhs.isTransient() ? lhs.productPtr() < rhs.productPtr() : false) : (rhs.isTransient() ? true : lhs.id() < rhs.id());
153  }
154 
155  inline
156  void
158  std::swap(processIndex_, other.processIndex_);
159  std::swap(productIndex_, other.productIndex_);
160  other.cachePtr_.store(cachePtr_.exchange(other.cachePtr_.load()));
161  }
162 
163  inline void swap(edm::RefCore & lhs, edm::RefCore & rhs) {
164  lhs.swap(rhs);
165  }
166 }
167 
168 #endif
void pushBackItem(RefCore const &productToBeInserted, bool checkPointer)
Definition: RefCore.cc:194
bool operator<(DetSet< T > const &x, DetSet< T > const &y)
Definition: DetSet.h:92
type
Definition: HCALResponse.h:21
RefCore(void const *iCache, ProcessIndex iProcessIndex, ProductIndex iProductIndex)
Definition: RefCore.h:115
void setProductPtr(void const *prodPtr) const
Definition: RefCore.h:58
int isTransientInt() const
Definition: RefCore.h:108
#define ID_IMPL
bool isThinnedAvailable(unsigned int thinnedKey, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:130
bool tryToSetProductPtrForFirstTime(void const *prodPtr) const
Definition: RefCore.h:66
void swap(RefCore &) noexcept
Definition: RefCore.h:157
#define nullptr
WrapperBase const * getProductPtr(std::type_info const &type, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:61
bool isNonnull() const
Definition: RefCore.h:74
bool tryToSetCacheItemForFirstTime(std::atomic< void const * > &iCache, void const *iNewValue)
#define PRODUCTPTR_IMPL
WrapperBase const * tryToGetProductPtr(std::type_info const &type, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:97
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
ProductIndex productIndex_
Definition: RefCore.h:133
bool cachePtrIsInvalid() const
Definition: RefCore.h:121
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
WrapperBase const * getThinnedProductPtr(std::type_info const &type, unsigned int &thinnedKey, EDProductGetter const *prodGetter) const
Definition: RefCore.cc:115
#define SETCACHEISPRODUCTPTR_IMPL(_item_)
void const * productPtr() const
Definition: RefCore.h:52
bool isAvailable() const
Definition: RefCore.cc:169
~RefCore() noexcept
Definition: RefCore.h:47
void productNotFoundException(std::type_info const &type) const
Definition: RefCore.cc:141
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
ProcessIndex processIndex_
Definition: RefCore.h:132
bool isNull() const
Definition: RefCore.h:71
const std::uintptr_t kCacheIsProductPtrMask
unsigned short ProcessIndex
Definition: ProductID.h:25
#define noexcept
#define ISNONNULL_IMPL
RefCore & operator=(RefCore const &)
Definition: RefCore.cc:53
#define ISTRANSIENT_IMPL
void setId(ProductID const &iId)
Definition: RefCore.cc:185
RefCore & operator=(RefCore &&iOther) noexcept
Definition: RefCore.h:40
RefCore(RefCore &&iOther) noexcept
Definition: RefCore.h:34
void setCacheIsProductPtr(const void *iItem) const
Definition: RefCore.h:119
EDProductGetter const * productGetter() const
Definition: RefCore.h:84
ProductID id() const
Definition: RefCore.h:49
bool operator!() const
Definition: RefCore.h:77
bool isTransient() const
Definition: RefCore.h:106
std::atomic< void const * > cachePtr_
Definition: RefCore.h:126
HLT enums.
#define PRODUCTGETTER_IMPL
unsigned short ProductIndex
Definition: ProductID.h:26
void setProductGetter(EDProductGetter const *prodGetter) const
Definition: RefCore.cc:180
void pushBackRefItem(RefCore const &productToBeInserted)
Definition: RefCore.cc:243
void setCacheIsProductGetter(EDProductGetter const *iGetter) const
Definition: RefCore.h:120
#define SETCACHEISPRODUCTGETTER_IMPL(_getter_)
void setTransient()
Definition: RefCore.h:118
void wrongTypeException(std::type_info const &expectedType, std::type_info const &actualType) const
Definition: RefCore.cc:151
void nullPointerForTransientException(std::type_info const &type) const
Definition: RefCore.cc:161
#define SETTRANSIENT_IMPL