CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_FwdPtr_h
00002 #define DataFormats_Common_FwdPtr_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Common
00006 // Class  :     FwdPtr
00007 //
00016 //
00017 // Original Author:  Salvatore Rappoccio
00018 //         Created:  Fri Feb  5 14:58:49 CST 2010
00019 //
00020 
00021 // user include files
00022 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
00023 #include "DataFormats/Common/interface/EDProductGetter.h"
00024 #include "DataFormats/Common/interface/GetProduct.h"
00025 #include "DataFormats/Common/interface/Handle.h"
00026 #include "DataFormats/Common/interface/OrphanHandle.h"
00027 #include "DataFormats/Common/interface/Ptr.h"
00028 #include "DataFormats/Common/interface/RefCore.h"
00029 #include "DataFormats/Common/interface/TestHandle.h"
00030 #include "DataFormats/Common/interface/traits.h"
00031 
00032 #include "FWCore/Utilities/interface/EDMException.h"
00033 
00034 // system include files
00035 #include "boost/type_traits/is_base_of.hpp"
00036 #include "boost/utility/enable_if.hpp"
00037 
00038 // forward declarations
00039 namespace edm {
00040   template<typename T>
00041   class FwdPtr {
00042     friend class FwdPtrVectorBase;
00043   public:
00044     
00045     typedef unsigned long key_type;
00046     typedef T   value_type;
00047     
00048     // General purpose constructor from two Ptrs.
00049     template<typename C>
00050     FwdPtr(Ptr<C> f, Ptr<C> b):
00051     ptr_(f), backPtr_(b)
00052     {}
00053     
00054   FwdPtr() :
00055     ptr_(), backPtr_()
00056       {}
00057     
00058     /*     template<typename U> */
00059     /*     FwdPtr(FwdPtr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<T, U>::value>::type * t = 0): */
00060     /*     ptr_(iOther.ptr_, t), backPtr_(iOther.backPtr_,t) */
00061     /*     { */
00062     /*     } */
00063     
00064     /*     template<typename U> */
00065     /*     explicit */
00066     /*     FwdPtr(FwdPtr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<U, T>::value>::type * t = 0): */
00067     /*     ptr_(iOther.ptr_,t), backPtr_(iOther.backPtr_,t){} */
00068     
00069     
00071     ~FwdPtr() {}
00072     
00074     T const&
00075     operator*() const { return ptr_.isNonnull() ? ptr_.operator*() : backPtr_.operator*();}
00076 
00078     T const*
00079     operator->() const{ return ptr_.isNonnull() ? ptr_.operator->() : backPtr_.operator->();}
00080 
00082     T const* get() const { return ptr_.isNonnull() ? ptr_.get() : backPtr_.get();}
00083 
00084 
00086     bool isNull() const {return !isNonnull(); }
00087     
00089     //bool isNonnull() const {return id().isValid(); }
00090     bool isNonnull() const {return ptr_.isNonnull() || backPtr_.isNonnull(); }
00092     bool operator!() const {return isNull();}
00093     
00096     bool isAvailable() const {return ptr_.isAvailable() || backPtr_.isAvailable();}
00097     
00099     bool isTransient() const {return ptr_.isTransient();}
00100     
00102     ProductID id() const {return ptr_.isNonnull() ? ptr_.id() : backPtr_.id();}
00103 
00105     EDProductGetter const* productGetter() const {
00106       if (ptr_.productGetter()) return ptr_.productGetter();
00107       else return backPtr_.productGetter();
00108     }
00109 
00110     key_type key() const {return ptr_.isNonnull() ? ptr_.key() : backPtr_.key() ;}
00111     
00112     bool hasProductCache() const { return ptr_.hasProductCache() || backPtr_.hasProductCache();} 
00113     
00114     RefCore const& refCore() const {return ptr_.isNonnull() ? ptr_.refCore() : backPtr_.refCore() ;}
00115     // ---------- member functions ---------------------------
00116     
00117     void const* product() const {return 0;}
00118     
00119     Ptr<value_type> const& ptr() const { return ptr_;}
00120     Ptr<value_type> const& backPtr() const { return backPtr_;}
00121       
00122     //Used by ROOT storage
00123     CMS_CLASS_VERSION(10)
00124 
00125   private:
00126     Ptr<value_type> ptr_;
00127     Ptr<value_type> backPtr_;
00128   };
00129   
00130 
00131   template<typename T>
00132   inline
00133   bool
00134   operator==(FwdPtr<T> const& lhs, FwdPtr<T> const& rhs) {
00135     return (lhs.ptr() == rhs.ptr() ||
00136              lhs.backPtr() == rhs.ptr() ||
00137              lhs.ptr() == rhs.backPtr() ||
00138              lhs.backPtr() == rhs.backPtr());
00139   }
00140 
00141   template<typename T>
00142   inline
00143   bool
00144   operator!=(FwdPtr<T> const& lhs, FwdPtr<T> const& rhs) {
00145     return !(lhs == rhs);
00146   }
00147 
00148   template<typename T>
00149   inline
00150   bool
00151   operator<(FwdPtr<T> const& lhs, FwdPtr<T> const& rhs) {
00154     return (lhs.ptr() < rhs.ptr());
00155   }
00156 
00157 }
00158 #endif