CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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 // system include files
00022 #include "boost/type_traits/is_base_of.hpp"
00023 #include "boost/utility/enable_if.hpp"
00024 
00025 // user include files
00026 #include "DataFormats/Common/interface/RefCore.h"
00027 #include "DataFormats/Common/interface/Ptr.h"
00028 #include "DataFormats/Common/interface/traits.h"
00029 #include "DataFormats/Common/interface/GetProduct.h"
00030 #include "DataFormats/Common/interface/EDProduct.h"
00031 #include "DataFormats/Common/interface/EDProductGetter.h"
00032 #include "DataFormats/Common/interface/Handle.h"
00033 #include "DataFormats/Common/interface/OrphanHandle.h"
00034 #include "DataFormats/Common/interface/TestHandle.h"
00035 
00036 #include "FWCore/Utilities/interface/EDMException.h"
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   FwdPtr(FwdPtr<T> const& iOther):
00059     ptr_(iOther.ptr_), backPtr_(iOther.backPtr_)
00060       {}
00061     
00062     /*     template<typename U> */
00063     /*     FwdPtr(FwdPtr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<T, U>::value>::type * t = 0): */
00064     /*     ptr_(iOther.ptr_, t), backPtr_(iOther.backPtr_,t) */
00065     /*     { */
00066     /*     } */
00067     
00068     /*     template<typename U> */
00069     /*     explicit */
00070     /*     FwdPtr(FwdPtr<U> const& iOther, typename boost::enable_if_c<boost::is_base_of<U, T>::value>::type * t = 0): */
00071     /*     ptr_(iOther.ptr_,t), backPtr_(iOther.backPtr_,t){} */
00072     
00073     
00075     ~FwdPtr() {}
00076     
00078     T const&
00079     operator*() const { return ptr_.isNonnull() ? ptr_.operator*() : backPtr_.operator*();}
00080 
00082     T const*
00083     operator->() const{ return ptr_.isNonnull() ? ptr_.operator->() : backPtr_.operator->();}
00084 
00086     T const* get() const { return ptr_.isNonnull() ? ptr_.get() : backPtr_.get();}
00087 
00088 
00090     bool isNull() const {return !isNonnull(); }
00091     
00093     //bool isNonnull() const {return id().isValid(); }
00094     bool isNonnull() const {return ptr_.isNonnull() || backPtr_.isNonnull(); }
00096     bool operator!() const {return isNull();}
00097     
00100     bool isAvailable() const {return ptr_.isAvailable() || backPtr_.isAvailable();}
00101     
00103     bool isTransient() const {return ptr_.isTransient();}
00104     
00106     ProductID id() const {return ptr_.isNonnull() ? ptr_.id() : backPtr_.id();}
00107 
00109     EDProductGetter const* productGetter() const {
00110       if ( ptr_.productGetter() ) return ptr_.productGetter();
00111       else return backPtr_.productGetter();
00112     }
00113 
00114     key_type key() const {return ptr_.isNonnull() ? ptr_.key() : backPtr_.key() ;}
00115     
00116     bool hasProductCache() const { return ptr_.hasProductCache() || backPtr_.hasProductCache();} 
00117     
00118     RefCore const& refCore() const {return ptr_.isNonnull() ? ptr_.refCore() : backPtr_.refCore() ;}
00119     // ---------- member functions ---------------------------
00120     
00121     void const* product() const {return 0;}
00122     
00123     Ptr<value_type> const & ptr() const { return ptr_;}
00124     Ptr<value_type> const & backPtr() const { return backPtr_;}
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 
00159 #endif