CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DataFormats/Common/interface/OrphanHandleBase.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_OrphanHandleBase_h
00002 #define DataFormats_Common_OrphanHandleBase_h
00003 
00004 /*----------------------------------------------------------------------
00005   
00006 OrphanHandle: Non-owning "smart pointer" for reference to EDProducts.
00007 
00008 This is a very preliminary version, and lacks safety features and
00009 elegance.
00010 
00011 If the pointed-to EDProduct is destroyed, use of the OrphanHandle
00012 becomes undefined. There is no way to query the OrphanHandle to
00013 discover if this has happened.
00014 
00015 OrphanHandles can have:
00016   -- Product pointer null and id == 0;
00017   -- Product pointer valid and id != 0;
00018 
00019 To check validity, one can use the isValid() function.
00020 
00021 ----------------------------------------------------------------------*/
00022 
00023 #include "DataFormats/Provenance/interface/ProductID.h"
00024 #include <cassert>
00025 #include <algorithm>
00026 
00027 namespace edm {
00028   class EDProduct;
00029 
00030   class OrphanHandleBase {
00031   public:
00032     OrphanHandleBase () :
00033       product_(), id_(ProductID()) {
00034     }
00035 
00036     OrphanHandleBase (void const* prod, ProductID const& id) :
00037       product_(prod), id_(id) {
00038       assert (prod);
00039     }
00040 
00041     ~OrphanHandleBase() {}
00042 
00043     void clear() {
00044       product_ = 0;
00045       id_ = ProductID();
00046     }
00047 
00048     void swap(OrphanHandleBase& other) {
00049       using std::swap;
00050       swap(product_, other.product_);
00051       std::swap(id_, other.id_);
00052     }
00053     
00054     OrphanHandleBase& operator=(OrphanHandleBase const& rhs) {
00055       OrphanHandleBase temp(rhs);
00056       this->swap(temp);
00057       return *this;
00058     }
00059 
00060     bool isValid() const {
00061       return product_ && id_ != ProductID();
00062     }
00063 
00064     ProductID id() const;
00065 
00066   protected:
00067     void const* productStorage() const;
00068 
00069   private:
00070     void const* product_;
00071     ProductID id_;
00072   };
00073 
00074   // Free swap function
00075   inline
00076   void
00077   swap(OrphanHandleBase& a, OrphanHandleBase& b) {
00078     a.swap(b);
00079   }
00080 }
00081 
00082 #endif