CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DataFormats/Common/interface/RefProd.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_RefProd_h
00002 #define DataFormats_Common_RefProd_h
00003 
00004 /*----------------------------------------------------------------------
00005   
00006 Ref: A template for an interproduct reference to a product.
00007 
00008 $Id: RefProd.h,v 1.21 2011/03/03 22:04:46 chrjones Exp $
00009 
00010 ----------------------------------------------------------------------*/
00011 
00012 /*----------------------------------------------------------------------
00013 //  This defines the public interface to the class RefProd<T>.
00014 //
00015 //  ProductID productID         is the product ID of the collection. (0 is invalid)
00016 //  RefProd<T> const& ref       is another RefProd<T>
00017 
00018 //  Constructors
00019     RefProd(); // Default constructor
00020     RefProd(RefProd<T> const& ref);     // Copy constructor  (default, not explicitly specified)
00021 
00022     RefProd(Handle<T> const& handle);
00023     RefProd(ProductID pid, EDProductGetter const* prodGetter);
00024 
00025 //  Destructor
00026     virtual ~RefProd() {}
00027 
00028 // Operators and methods
00029     RefProd<T>& operator=(RefProd<T> const&);   // assignment (default, not explicitly specified)
00030     T const& operator*() const;                 // dereference
00031     T const* operator->() const;                // member dereference
00032     bool operator==(RefProd<T> const& ref) const;       // equality
00033     bool operator!=(RefProd<T> const& ref) const;       // inequality
00034     bool operator<(RefProd<T> const& ref) const;        // ordering
00035     bool isNonnull() const;                     // true if an object is referenced
00036     bool isNull() const;                        // equivalent to !isNonnull()
00037     bool operator!() const;                     // equivalent to !isNonnull()
00038 ----------------------------------------------------------------------*/ 
00039 
00040 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
00041 #include "DataFormats/Common/interface/EDProductfwd.h"
00042 #include "DataFormats/Common/interface/EDProductGetter.h"
00043 #include "DataFormats/Common/interface/RefCore.h"
00044 #include "DataFormats/Provenance/interface/ProductID.h"
00045 #include "DataFormats/Common/interface/Handle.h"
00046 #include "DataFormats/Common/interface/OrphanHandle.h"
00047 #include "DataFormats/Common/interface/TestHandle.h"
00048 
00049 namespace edm {
00050 
00051   template <typename C>
00052   class RefProd {
00053   public:
00054     typedef C product_type;
00055     typedef C value_type;
00056 
00058     RefProd() : product_() {}
00059 
00061     explicit RefProd(Handle<C> const& handle) :
00062     product_(handle.id(), handle.product(), 0, false) {
00063       checkTypeAtCompileTime(handle.product());
00064     }
00065 
00067     explicit RefProd(OrphanHandle<C> const& handle) :
00068     product_(handle.id(), handle.product(), 0, false) {
00069       checkTypeAtCompileTime(handle.product());
00070     }
00071 
00073     //  An exception will be thrown if an attempt is made to persistify
00074     //  any object containing this RefProd.  Also, in the future work will
00075     //  be done to throw an exception if an attempt is made to put any object
00076     //  containing this RefProd into an event(or run or lumi).
00077     RefProd(C const* product) :
00078       product_(ProductID(), product, 0, true) {
00079       checkTypeAtCompileTime(product);
00080     }
00081 
00083     //  An exception will be thrown if an attempt is made to persistify
00084     //  any object containing this RefProd.  Also, in the future work will
00085     //  be done to throw an exception if an attempt is made to put any object
00086     //  containing this RefProd into an event(or run or lumi).
00087     explicit RefProd(TestHandle<C> const& handle) :
00088     product_(handle.id(), handle.product(), 0, true) {
00089       checkTypeAtCompileTime(handle.product());
00090     }
00091 
00093     template <typename T, typename F>
00094     explicit RefProd(Ref<C, T, F> const& ref);
00095 
00097     template <typename T, typename F>
00098     explicit RefProd(RefVector<C, T, F> const& ref);
00099 
00100     // Constructor for those users who do not have a product handle,
00101     // but have a pointer to a product getter (such as the EventPrincipal).
00102     // prodGetter will ususally be a pointer to the event principal.
00103     RefProd(ProductID const& productID, EDProductGetter const* prodGetter) :
00104       product_(productID, 0, mustBeNonZero(prodGetter, "RefProd", productID), false) {
00105     }
00106 
00108     ~RefProd() {}
00109 
00111     product_type const& operator*() const;
00112 
00114     product_type const* operator->() const;
00115 
00118     product_type const* get() const {
00119       return isNull() ? 0 : this->operator->();
00120     }
00121 
00124     product_type const* product() const {
00125       return isNull() ? 0 : this->operator->();
00126     }
00127 
00128     RefCore const& refCore() const {
00129       return product_;
00130     }
00131 
00133     bool isNull() const {return !isNonnull();}
00134 
00136     bool isNonnull() const {return product_.isNonnull();}
00137 
00139     bool operator!() const {return isNull();}
00140 
00142     ProductID id() const {return product_.id();}
00143 
00145     EDProductGetter const* productGetter() const {return product_.productGetter();}
00146 
00148     bool hasCache() const {return product_.productPtr() != 0;}
00149 
00151     bool hasProductCache() const {return hasCache();}
00152 
00155     bool isAvailable() const {return product_.isAvailable();}
00156 
00158     bool isTransient() const {return product_.isTransient();}
00159 
00160     void swap(RefProd<C> &);
00161 
00162     //Needed for ROOT storage
00163     CMS_CLASS_VERSION(10)
00164 
00165   private:
00166     // Compile time check that the argument is a C* or C const*
00167     // or derived from it.
00168     void checkTypeAtCompileTime(C const* ptr) {}
00169 
00170     RefCore product_;
00171   };
00172 }
00173 
00174 #include "DataFormats/Common/interface/Ref.h"
00175 #include "DataFormats/Common/interface/RefCoreGet.h"
00176 
00177 namespace edm {
00178   template<typename C, typename T, typename F>
00179   class RefVector;
00180 
00182   template <typename C>
00183   template <typename T, typename F>
00184   inline
00185   RefProd<C>::RefProd(Ref<C, T, F> const& ref) :
00186       product_(ref.id(), ref.hasProductCache() ?  ref.product() : 0, ref.productGetter(), ref.isTransient()) 
00187   {  }
00188 
00190   template <typename C>
00191   template <typename T, typename F>
00192   inline
00193   RefProd<C>::RefProd(RefVector<C, T, F> const& ref) :
00194       product_(ref.id(), ref.hasProductCache() ?  ref.product() : 0, ref.productGetter(), ref.isTransient()) 
00195   {  }
00196 
00198   template <typename C>
00199   inline
00200   C const& RefProd<C>::operator*() const {
00201     return *(edm::template getProduct<C>(product_));
00202   }
00203 
00205   template <typename C>
00206   inline
00207   C const* RefProd<C>::operator->() const {
00208     return edm::template getProduct<C>(product_);
00209   } 
00210 
00211 
00212   template<typename C>
00213   inline
00214   void RefProd<C>::swap(RefProd<C> & other) {
00215     std::swap(product_, other.product_);
00216   }
00217 
00218   template <typename C>
00219   inline
00220   bool
00221   operator== (RefProd<C> const& lhs, RefProd<C> const& rhs) {
00222     return lhs.refCore() == rhs.refCore();
00223   }
00224 
00225   template <typename C>
00226   inline
00227   bool
00228   operator!= (RefProd<C> const& lhs, RefProd<C> const& rhs) {
00229     return !(lhs == rhs);
00230   }
00231 
00232   template <typename C>
00233   inline
00234   bool
00235   operator< (RefProd<C> const& lhs, RefProd<C> const& rhs) {
00236     return (lhs.refCore() < rhs.refCore());
00237   }
00238 
00239   template<typename C>
00240   inline
00241   void swap(edm::RefProd<C> const& lhs, edm::RefProd<C> const& rhs ) {
00242     lhs.swap(rhs);
00243   }
00244 }
00245 
00246 #include "DataFormats/Common/interface/HolderToVectorTrait.h"
00247 
00248 namespace edm {
00249   namespace reftobase {
00250 
00251     template <typename T>
00252     struct RefProdHolderToVector {
00253       static  std::auto_ptr<BaseVectorHolder<T> > makeVectorHolder() {
00254         Exception::throwThis(errors::InvalidReference, "attempting to make a BaseVectorHolder<T> from a RefProd<C>.\n");
00255         return std::auto_ptr<BaseVectorHolder<T> >();
00256       }
00257       static std::auto_ptr<RefVectorHolderBase> makeVectorBaseHolder() {
00258         Exception::throwThis(errors::InvalidReference, "attempting to make a RefVectorHolderBase from a RefProd<C>.\n");
00259         return std::auto_ptr<RefVectorHolderBase>();
00260       }
00261     };
00262 
00263     template<typename C, typename T>
00264     struct HolderToVectorTrait<T, RefProd<C> > {
00265       typedef RefProdHolderToVector<T> type;
00266     };
00267 
00268     struct RefProdRefHolderToRefVector {
00269       static  std::auto_ptr<RefVectorHolderBase> makeVectorHolder() {
00270         Exception::throwThis(errors::InvalidReference, "attempting to make a BaseVectorHolder<T> from a RefProd<C>.\n");
00271         return std::auto_ptr<RefVectorHolderBase>();
00272       }
00273       static  std::auto_ptr<RefVectorHolderBase> makeVectorBaseHolder() {
00274         Exception::throwThis(errors::InvalidReference, "attempting to make a RefVectorHolderBase from a RefProd<C>.\n");
00275         return std::auto_ptr<RefVectorHolderBase>();
00276       }
00277     };
00278 
00279     template<typename C>
00280     struct RefHolderToRefVectorTrait<RefProd<C> > {
00281       typedef RefProdRefHolderToRefVector type;
00282     };
00283 
00284   }
00285 }
00286 
00287 #endif