Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-11 04:36:58

0001 #ifndef DataFormats_Common_RefProd_h
0002 #define DataFormats_Common_RefProd_h
0003 
0004 /*----------------------------------------------------------------------
0005 
0006 Ref: A template for an interproduct reference to a product.
0007 
0008 ----------------------------------------------------------------------*/
0009 
0010 /*----------------------------------------------------------------------
0011 //  This defines the public interface to the class RefProd<T>.
0012 //
0013 //  ProductID productID     is the product ID of the collection. (0 is invalid)
0014 //  RefProd<T> const& ref   is another RefProd<T>
0015 
0016 //  Constructors
0017     RefProd(); // Default constructor
0018     RefProd(RefProd<T> const& ref); // Copy constructor  (default, not explicitly specified)
0019 
0020     RefProd(Handle<T> const& handle);
0021     RefProd(ProductID pid, EDProductGetter const* prodGetter);
0022 
0023 //  Destructor
0024     virtual ~RefProd() {}
0025 
0026 // Operators and methods
0027     RefProd<T>& operator=(RefProd<T> const&);   // assignment (default, not explicitly specified)
0028     T const& operator*() const;         // dereference
0029     T const* operator->() const;        // member dereference
0030     bool operator==(RefProd<T> const& ref) const;   // equality
0031     bool operator!=(RefProd<T> const& ref) const;   // inequality
0032     bool operator<(RefProd<T> const& ref) const;    // ordering
0033     bool isNonnull() const;         // true if an object is referenced
0034     bool isNull() const;            // equivalent to !isNonnull()
0035     bool operator!() const;         // equivalent to !isNonnull()
0036 ----------------------------------------------------------------------*/
0037 
0038 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
0039 #include "DataFormats/Common/interface/Handle.h"
0040 #include "DataFormats/Common/interface/OrphanHandle.h"
0041 #include "DataFormats/Common/interface/RefCore.h"
0042 #include "DataFormats/Common/interface/TestHandle.h"
0043 #include "DataFormats/Provenance/interface/ProductID.h"
0044 
0045 // Not really needed but things depend on it being here ...
0046 #include "DataFormats/Common/interface/Ref.h"
0047 
0048 #include <utility>
0049 
0050 namespace edm {
0051 
0052   class EDProductGetter;
0053 
0054   template <typename C>
0055   class RefProd {
0056   public:
0057     typedef C product_type;
0058     typedef C value_type;
0059 
0060     /// Default constructor needed for reading from persistent store. Not for direct use.
0061     RefProd() : product_() {}
0062 
0063     /// General purpose constructor from handle.
0064     explicit RefProd(Handle<C> const& handle) : product_(handle.id(), handle.product(), nullptr, false) {
0065       checkTypeAtCompileTime(handle.product());
0066     }
0067 
0068     /// General purpose constructor from orphan handle.
0069     explicit RefProd(OrphanHandle<C> const& handle) : product_(handle.id(), handle.product(), nullptr, false) {
0070       checkTypeAtCompileTime(handle.product());
0071     }
0072 
0073     /// Constructor for ref to object that is not in an event.
0074     //  An exception will be thrown if an attempt is made to persistify
0075     //  any object containing this RefProd.  Also, in the future work will
0076     //  be done to throw an exception if an attempt is made to put any object
0077     //  containing this RefProd into an event(or run or lumi).
0078     RefProd(C const* iProduct) : product_(ProductID(), iProduct, nullptr, true) { checkTypeAtCompileTime(iProduct); }
0079 
0080     /// General purpose constructor from test handle.
0081     //  An exception will be thrown if an attempt is made to persistify
0082     //  any object containing this RefProd.  Also, in the future work will
0083     //  be done to throw an exception if an attempt is made to put any object
0084     //  containing this RefProd into an event(or run or lumi).
0085     explicit RefProd(TestHandle<C> const& handle) : product_(handle.id(), handle.product(), nullptr, true) {
0086       checkTypeAtCompileTime(handle.product());
0087     }
0088 
0089     // Constructor for those users who do not have a product handle,
0090     // but have a pointer to a product getter (such as the EventPrincipal).
0091     // prodGetter will ususally be a pointer to the event principal.
0092     RefProd(ProductID const& productID, EDProductGetter const* prodGetter)
0093         : product_(productID, nullptr, mustBeNonZero(prodGetter, "RefProd", productID), false) {}
0094 
0095     /// Destructor
0096     ~RefProd() {}
0097 
0098     /// Dereference operator
0099     product_type const& operator*() const;
0100 
0101     /// Member dereference operator
0102     product_type const* operator->() const;
0103 
0104     /// Returns C++ pointer to the product
0105     /// Will attempt to retrieve product
0106     product_type const* get() const { return isNull() ? nullptr : this->operator->(); }
0107 
0108     /// Returns C++ pointer to the product
0109     /// Will attempt to retrieve product
0110     product_type const* product() const { return isNull() ? nullptr : this->operator->(); }
0111 
0112     RefCore const& refCore() const { return product_; }
0113 
0114     /// Checks for null
0115     bool isNull() const { return !isNonnull(); }
0116 
0117     /// Checks for non-null
0118     bool isNonnull() const { return product_.isNonnull(); }
0119 
0120     /// Checks for null
0121     bool operator!() const { return isNull(); }
0122 
0123     /// Accessor for product ID.
0124     ProductID id() const { return product_.id(); }
0125 
0126     /// Accessor for product getter.
0127     EDProductGetter const* productGetter() const { return product_.productGetter(); }
0128 
0129     /// Checks if product is in memory.
0130     bool hasCache() const { return product_.productPtr() != nullptr; }
0131 
0132     /// Checks if product is in memory.
0133     bool hasProductCache() const { return hasCache(); }
0134 
0135     /// Checks if collection is in memory or available
0136     /// in the Event. No type checking is done.
0137     bool isAvailable() const { return product_.isAvailable(); }
0138 
0139     /// Checks if this RefProd is transient (i.e. not persistable).
0140     bool isTransient() const { return product_.isTransient(); }
0141 
0142     void swap(RefProd<C>&);
0143 
0144     //Needed for ROOT storage
0145     CMS_CLASS_VERSION(10)
0146 
0147   private:
0148     // Compile time check that the argument is a C* or C const*
0149     // or derived from it.
0150     void checkTypeAtCompileTime(C const* /*ptr*/) {}
0151 
0152     RefCore product_;
0153   };
0154 }  // namespace edm
0155 
0156 #include "DataFormats/Common/interface/RefCoreGet.h"
0157 
0158 namespace edm {
0159 
0160   /// Dereference operator
0161   template <typename C>
0162   inline C const& RefProd<C>::operator*() const {
0163     return *(edm::template getProduct<C>(product_));
0164   }
0165 
0166   /// Member dereference operator
0167   template <typename C>
0168   inline C const* RefProd<C>::operator->() const {
0169     return edm::template getProduct<C>(product_);
0170   }
0171 
0172   template <typename C>
0173   inline void RefProd<C>::swap(RefProd<C>& other) {
0174     edm::swap(product_, other.product_);
0175   }
0176 
0177   template <typename C>
0178   inline bool operator==(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0179     return lhs.refCore() == rhs.refCore();
0180   }
0181 
0182   template <typename C>
0183   inline bool operator!=(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0184     return !(lhs == rhs);
0185   }
0186 
0187   template <typename C>
0188   inline bool operator<(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0189     return (lhs.refCore() < rhs.refCore());
0190   }
0191 
0192   template <typename C>
0193   inline void swap(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0194     lhs.swap(rhs);
0195   }
0196 }  // namespace edm
0197 
0198 //Handle specialization here
0199 #include "DataFormats/Common/interface/HolderToVectorTrait_RefProd_specialization.h"
0200 
0201 #endif