Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:53

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     /// This function is potentially costly as it might cause a disk
0138     /// read (note that it does not cause the data to be cached locally)
0139     bool isAvailable() const { return product_.isAvailable(); }
0140 
0141     /// Checks if this RefProd is transient (i.e. not persistable).
0142     bool isTransient() const { return product_.isTransient(); }
0143 
0144     void swap(RefProd<C>&);
0145 
0146     //Needed for ROOT storage
0147     CMS_CLASS_VERSION(10)
0148 
0149   private:
0150     // Compile time check that the argument is a C* or C const*
0151     // or derived from it.
0152     void checkTypeAtCompileTime(C const* /*ptr*/) {}
0153 
0154     RefCore product_;
0155   };
0156 }  // namespace edm
0157 
0158 #include "DataFormats/Common/interface/RefCoreGet.h"
0159 
0160 namespace edm {
0161 
0162   /// Dereference operator
0163   template <typename C>
0164   inline C const& RefProd<C>::operator*() const {
0165     return *(edm::template getProduct<C>(product_));
0166   }
0167 
0168   /// Member dereference operator
0169   template <typename C>
0170   inline C const* RefProd<C>::operator->() const {
0171     return edm::template getProduct<C>(product_);
0172   }
0173 
0174   template <typename C>
0175   inline void RefProd<C>::swap(RefProd<C>& other) {
0176     edm::swap(product_, other.product_);
0177   }
0178 
0179   template <typename C>
0180   inline bool operator==(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0181     return lhs.refCore() == rhs.refCore();
0182   }
0183 
0184   template <typename C>
0185   inline bool operator!=(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0186     return !(lhs == rhs);
0187   }
0188 
0189   template <typename C>
0190   inline bool operator<(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0191     return (lhs.refCore() < rhs.refCore());
0192   }
0193 
0194   template <typename C>
0195   inline void swap(RefProd<C> const& lhs, RefProd<C> const& rhs) {
0196     lhs.swap(rhs);
0197   }
0198 }  // namespace edm
0199 
0200 //Handle specialization here
0201 #include "DataFormats/Common/interface/HolderToVectorTrait_RefProd_specialization.h"
0202 
0203 #endif