CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/DataFormats/Common/interface/PtrVectorBase.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_PtrVectorBase_h
00002 #define DataFormats_Common_PtrVectorBase_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Common
00006 // Class  :     PtrVectorBase
00007 //
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Wed Oct 24 15:26:45 EDT 2007
00019 //
00020 
00021 // user include files
00022 #include "DataFormats/Common/interface/RefCore.h"
00023 
00024 // system include files
00025 #include <typeinfo>
00026 #include <vector>
00027 #include <cassert>
00028 
00029 // forward declarations
00030 
00031 namespace edm {
00032   class PtrVectorBase {
00033 
00034   public:
00035     typedef unsigned long key_type;
00036     typedef key_type size_type;
00037 
00038     explicit PtrVectorBase(ProductID const& productID, void const* prodPtr = 0,
00039                            EDProductGetter const* prodGetter = 0) :
00040       core_(productID, prodPtr, prodGetter, false), indicies_() {}
00041 
00042     virtual ~PtrVectorBase();
00043 
00044     // ---------- const member functions ---------------------
00046     bool isNull() const {return !isNonnull(); }
00047 
00049     //bool isNonnull() const {return id().isValid(); }
00050     bool isNonnull() const { return core_.isNonnull(); }
00051 
00053     bool operator!() const {return isNull();}
00054 
00056     ProductID id() const {return core_.id();}
00057 
00059     EDProductGetter const* productGetter() const {return core_.productGetter();}
00060 
00061     bool hasCache() const { return !cachedItems_.empty(); }
00062 
00063     bool hasProductCache() const { return 0 == core_.productPtr(); }
00064 
00067     bool isAvailable() const { return core_.isAvailable(); }
00068 
00070     bool empty() const {return indicies_.empty();}
00071 
00073     size_type size() const {return indicies_.size();}
00074 
00076     size_type capacity() const {return indicies_.capacity();}
00077 
00079     void clear() { core_ = RefCore(); indicies_.clear(); cachedItems_.clear(); }
00080 
00081     bool operator==(PtrVectorBase const& iRHS) const;
00082     // ---------- static member functions --------------------
00083 
00084     // ---------- member functions ---------------------------
00086     void reserve(size_type n) {indicies_.reserve(n); cachedItems_.reserve(n);}
00087 
00088     void setProductGetter(EDProductGetter* iGetter) const { core_.setProductGetter(iGetter); }
00089 
00090     bool isTransient() const {return core_.isTransient();}
00091 
00092     void const* product() const {
00093        return 0;
00094     }
00095 
00096   protected:
00097     PtrVectorBase();
00098 
00100     void swap(PtrVectorBase& other);
00101 
00102     void push_back_base(RefCore const& core, key_type iKey, void const* iData);
00103 
00104     std::vector<void const*>::const_iterator void_begin() const {
00105       getProduct_();
00106       return cachedItems_.begin();
00107     }
00108     std::vector<void const*>::const_iterator void_end() const {
00109       getProduct_();
00110       return cachedItems_.end();
00111     }
00112 
00113     template<typename TPtr>
00114     TPtr makePtr(unsigned long iIndex) const {
00115       if (isTransient()) {
00116         return TPtr(reinterpret_cast<typename TPtr::value_type const*>(cachedItems_[iIndex]),
00117                   indicies_[iIndex]);
00118       }
00119       if (hasCache()) {
00120         return TPtr(this->id(),
00121                   reinterpret_cast<typename TPtr::value_type const*>(cachedItems_[iIndex]),
00122                   indicies_[iIndex]);
00123       }
00124       return TPtr(this->id(), indicies_[iIndex], productGetter());
00125     }
00126 
00127     template<typename TPtr>
00128     TPtr makePtr(std::vector<void const*>::const_iterator const iIt) const {
00129       if (isTransient()) {
00130         return TPtr(reinterpret_cast<typename TPtr::value_type const*>(*iIt),
00131                   indicies_[iIt - cachedItems_.begin()]);
00132       }
00133       if (hasCache()) {
00134         return TPtr(this->id(),
00135                   reinterpret_cast<typename TPtr::value_type const*>(*iIt),
00136                   indicies_[iIt - cachedItems_.begin()]);
00137       }
00138       return TPtr(this->id(), indicies_[iIt - cachedItems_.begin()], productGetter());
00139     }
00140 
00141   private:
00142     void getProduct_() const;
00143     //virtual std::type_info const& typeInfo() const = 0;
00144     virtual std::type_info const& typeInfo() const {
00145       assert(false);
00146       return *reinterpret_cast<const std::type_info*>(0);
00147     }
00148     // ---------- member data --------------------------------
00149     RefCore core_;
00150     std::vector<key_type> indicies_;
00151     mutable std::vector<void const*> cachedItems_; 
00152   };
00153 }
00154 
00155 #endif