CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/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 OrphanHandleBase {
00029   public:
00030     OrphanHandleBase() :
00031       product_(), id_(ProductID()) {
00032     }
00033 
00034     OrphanHandleBase(void const* iProd, ProductID const& iId) :
00035       product_(iProd), id_(iId) {
00036       assert(iProd);
00037     }
00038 
00039     ~OrphanHandleBase() {}
00040 
00041     void clear() {
00042       product_ = 0;
00043       id_ = ProductID();
00044     }
00045 
00046     void swap(OrphanHandleBase& other) {
00047       using std::swap;
00048       swap(product_, other.product_);
00049       std::swap(id_, other.id_);
00050     }
00051     
00052     OrphanHandleBase& operator=(OrphanHandleBase const& rhs) {
00053       OrphanHandleBase temp(rhs);
00054       this->swap(temp);
00055       return *this;
00056     }
00057 
00058     bool isValid() const {
00059       return product_ && id_ != ProductID();
00060     }
00061 
00062     ProductID id() const;
00063 
00064   protected:
00065     void const* productStorage() const;
00066 
00067   private:
00068     void const* product_;
00069     ProductID id_;
00070   };
00071 
00072   // Free swap function
00073   inline
00074   void
00075   swap(OrphanHandleBase& a, OrphanHandleBase& b) {
00076     a.swap(b);
00077   }
00078 }
00079 
00080 #endif