CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/DataFormats/Common/interface/Holder.h

Go to the documentation of this file.
00001 
00002 #ifndef DataFormats_Common_Holder_h
00003 #define DataFormats_Common_Holder_h
00004 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
00005 #include "DataFormats/Common/interface/BaseHolder.h"
00006 #include "DataFormats/Common/interface/RefHolder.h"
00007 #include <memory>
00008 
00009 namespace edm {
00010   namespace reftobase {
00011    //------------------------------------------------------------------
00012     // Class template Holder<T,REF>
00013     //------------------------------------------------------------------
00014 
00015     template <class T, class REF>
00016     class Holder : public BaseHolder<T> {
00017     public:
00018       Holder();
00019       Holder(Holder const& other);
00020       explicit Holder(REF const& iRef);
00021       Holder& operator= (Holder const& rhs);
00022       void swap(Holder& other);
00023       virtual ~Holder();
00024       virtual BaseHolder<T>* clone() const;
00025 
00026       virtual T const* getPtr() const;
00027       virtual ProductID id() const;
00028       virtual size_t key() const;
00029       virtual bool isEqualTo(BaseHolder<T> const& rhs) const;
00030       REF const& getRef() const;
00031 
00032       virtual bool fillRefIfMyTypeMatches(RefHolderBase& fillme,
00033                                           std::string& msg) const;
00034 
00035       virtual std::auto_ptr<RefHolderBase> holder() const {
00036         return std::auto_ptr<RefHolderBase>( new RefHolder<REF>( ref_ ) );
00037       }
00038       virtual std::auto_ptr<BaseVectorHolder<T> > makeVectorHolder() const;
00039       virtual std::auto_ptr<RefVectorHolderBase> makeVectorBaseHolder() const;
00040       virtual EDProductGetter const* productGetter() const;
00041       virtual bool hasProductCache() const;
00042       virtual void const * product() const;
00043 
00046       virtual bool isAvailable() const { return ref_.isAvailable(); }
00047 
00048       //Used by ROOT storage
00049       CMS_CLASS_VERSION(10)
00050 
00051     private:
00052       REF ref_;
00053     };
00054 
00055     //------------------------------------------------------------------
00056     // Implementation of Holder<T,REF>
00057     //------------------------------------------------------------------
00058 
00059     template <class T, class REF>
00060     inline
00061     Holder<T,REF>::Holder() : BaseHolder<T>(),
00062       ref_()
00063     {  }
00064 
00065     template <class T, class REF>
00066     inline
00067     Holder<T,REF>::Holder(Holder const& other) : BaseHolder<T>(other),
00068       ref_(other.ref_)
00069     { }
00070 
00071     template <class T, class REF>
00072     inline
00073     Holder<T,REF>::Holder(REF const& r) : BaseHolder<T>(),
00074       ref_(r)
00075     { }
00076 
00077     template <class T, class REF>
00078     inline
00079     Holder<T,REF> &
00080     Holder<T,REF>::operator=(Holder const& rhs)
00081     {
00082       Holder temp(rhs);
00083       swap(temp);
00084       return *this;
00085     }
00086 
00087     template <class T, class REF>
00088     inline
00089     void
00090     Holder<T,REF>::swap(Holder& other)
00091     {
00092       std::swap(ref_, other.ref_);
00093     }
00094 
00095     template <class T, class REF>
00096     inline
00097     Holder<T,REF>::~Holder()
00098     { }
00099 
00100     template <class T, class REF>
00101     inline
00102     BaseHolder<T>*
00103     Holder<T,REF>::clone() const 
00104     {
00105       return new Holder(*this);
00106     }
00107 
00108     template <class T, class REF>
00109     inline
00110     T const*
00111     Holder<T,REF>::getPtr() const
00112     {
00113       return ref_.operator->();
00114     }
00115 
00116     template <class T, class REF>
00117     inline
00118     ProductID
00119     Holder<T,REF>::id() const
00120     {
00121       return ref_.id();
00122     }
00123 
00124     template <class T, class REF>
00125     inline
00126     bool
00127     Holder<T,REF>::isEqualTo(BaseHolder<T> const& rhs) const
00128     {
00129       Holder const* h = dynamic_cast<Holder const*>(&rhs);
00130       return h && (getRef() == h->getRef());
00131       //       if (h == 0) return false;
00132       //       return getRef() == h->getRef();
00133     }
00134 
00135     template <class T, class REF>
00136     inline
00137     REF const&
00138     Holder<T,REF>::getRef() const
00139     {
00140       return ref_;
00141     }
00142 
00143     template <class T, class REF>
00144     inline
00145     EDProductGetter const* Holder<T,REF>::productGetter() const {
00146       return ref_.productGetter();
00147     }
00148 
00149     template <class T, class REF>
00150     inline
00151     bool Holder<T,REF>::hasProductCache() const {
00152       return ref_.hasProductCache();
00153     }
00154 
00155     template <class T, class REF>
00156     inline
00157     void const * Holder<T,REF>::product() const {
00158       return ref_.product();
00159     }
00160 
00161     template <class T, class REF>
00162     bool
00163     Holder<T,REF>::fillRefIfMyTypeMatches(RefHolderBase& fillme,
00164                                           std::string& msg) const
00165     {
00166       RefHolder<REF>* h = dynamic_cast<RefHolder<REF>*>(&fillme);
00167       bool conversion_worked = (h != 0);
00168 
00169       if (conversion_worked)
00170         h->setRef(ref_);
00171       else
00172         msg = typeid(REF).name();
00173 
00174       return conversion_worked;
00175     }
00176 
00177   }
00178 }
00179 
00180 #include "DataFormats/Common/interface/HolderToVectorTrait.h"
00181 #include "DataFormats/Common/interface/Ref.h"
00182 
00183 namespace edm {
00184   namespace reftobase {
00185 
00186     template <typename T, typename REF>
00187     std::auto_ptr<BaseVectorHolder<T> > Holder<T,REF>::makeVectorHolder() const {
00188       typedef typename HolderToVectorTrait<T, REF>::type helper;
00189       return helper::makeVectorHolder();
00190     }
00191 
00192     template <typename T, typename REF>
00193     std::auto_ptr<RefVectorHolderBase> Holder<T,REF>::makeVectorBaseHolder() const {
00194       typedef typename HolderToVectorTrait<T, REF>::type helper;
00195       return helper::makeVectorBaseHolder();
00196     }
00197 
00198   }
00199 }
00200 
00201 #include "DataFormats/Common/interface/RefKeyTrait.h"
00202 
00203 namespace edm {
00204   namespace reftobase {
00205 
00206     template <class T, class REF>
00207     inline
00208     size_t
00209     Holder<T,REF>::key() const
00210     {
00211       typedef typename RefKeyTrait<REF>::type helper;
00212       return helper::key( ref_ );
00213     }
00214     
00215   }
00216 }
00217 #endif