Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-24 02:18:30

0001 #ifndef DataFormats_Common_IndirectHolder_h
0002 #define DataFormats_Common_IndirectHolder_h
0003 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
0004 #include "DataFormats/Common/interface/BaseHolder.h"
0005 #include "DataFormats/Common/interface/RefHolderBase.h"
0006 #include "DataFormats/Provenance/interface/ProductID.h"
0007 
0008 #include <memory>
0009 
0010 namespace edm {
0011   template <typename T>
0012   class RefToBase;
0013 
0014   namespace reftobase {
0015 
0016     template <typename T>
0017     class IndirectVectorHolder;
0018     class RefVectorHolderBase;
0019     class RefHolderBase;
0020 
0021     //------------------------------------------------------------------
0022     // Class template IndirectHolder<T>
0023     //------------------------------------------------------------------
0024 
0025     template <typename T>
0026     class IndirectHolder : public BaseHolder<T> {
0027     public:
0028       // It may be better to use unique_ptr<RefHolderBase> in
0029       // this constructor, so that the cloning can be avoided. I'm not
0030       // sure if use of unique_ptr here causes any troubles elsewhere.
0031       IndirectHolder() : BaseHolder<T>(), helper_(nullptr) {}
0032       IndirectHolder(std::shared_ptr<RefHolderBase> p);
0033       template <typename U>
0034       IndirectHolder(std::unique_ptr<U> p) : helper_(p.release()) {}
0035       IndirectHolder(IndirectHolder const& other);
0036       IndirectHolder& operator=(IndirectHolder const& rhs);
0037       void swap(IndirectHolder& other);
0038       ~IndirectHolder() override;
0039 
0040       BaseHolder<T>* clone() const override;
0041       T const* getPtr() const override;
0042       ProductID id() const override;
0043       size_t key() const override;
0044       bool isEqualTo(BaseHolder<T> const& rhs) const override;
0045 
0046       std::unique_ptr<RefHolderBase> holder() const override;
0047       std::unique_ptr<BaseVectorHolder<T> > makeVectorHolder() const override;
0048       EDProductGetter const* productGetter() const override;
0049 
0050       /// Checks if product collection is in memory or available
0051       /// in the Event. No type checking is done.
0052       bool isAvailable() const override { return helper_->isAvailable(); }
0053 
0054       bool isTransient() const override { return helper_->isTransient(); }
0055 
0056       //Used by ROOT storage
0057       CMS_CLASS_VERSION(10)
0058 
0059     private:
0060       friend class RefToBase<T>;
0061       friend class IndirectVectorHolder<T>;
0062       RefHolderBase* helper_;
0063     };
0064     //------------------------------------------------------------------
0065     // Implementation of IndirectHolder<T>
0066     //------------------------------------------------------------------
0067     template <typename T>
0068     inline IndirectHolder<T>::IndirectHolder(std::shared_ptr<RefHolderBase> p) : BaseHolder<T>(), helper_(p->clone()) {}
0069 
0070     template <typename T>
0071     inline IndirectHolder<T>::IndirectHolder(IndirectHolder const& other)
0072         : BaseHolder<T>(other), helper_(other.helper_->clone()) {}
0073 
0074     template <typename T>
0075     inline void IndirectHolder<T>::swap(IndirectHolder& other) {
0076       this->BaseHolder<T>::swap(other);
0077       std::swap(helper_, other.helper_);
0078     }
0079 
0080     template <typename T>
0081     inline IndirectHolder<T>& IndirectHolder<T>::operator=(IndirectHolder const& rhs) {
0082       IndirectHolder temp(rhs);
0083       swap(temp);
0084       return *this;
0085     }
0086 
0087     template <typename T>
0088     IndirectHolder<T>::~IndirectHolder() {
0089       delete helper_;
0090     }
0091 
0092     template <typename T>
0093     BaseHolder<T>* IndirectHolder<T>::clone() const {
0094       return new IndirectHolder<T>(*this);
0095     }
0096 
0097     template <typename T>
0098     T const* IndirectHolder<T>::getPtr() const {
0099       return helper_->template getPtr<T>();
0100     }
0101 
0102     template <typename T>
0103     ProductID IndirectHolder<T>::id() const {
0104       return helper_->id();
0105     }
0106 
0107     template <typename T>
0108     size_t IndirectHolder<T>::key() const {
0109       return helper_->key();
0110     }
0111 
0112     template <typename T>
0113     inline EDProductGetter const* IndirectHolder<T>::productGetter() const {
0114       return helper_->productGetter();
0115     }
0116 
0117     template <typename T>
0118     bool IndirectHolder<T>::isEqualTo(BaseHolder<T> const& rhs) const {
0119       IndirectHolder const* h = dynamic_cast<IndirectHolder const*>(&rhs);
0120       return h && helper_->isEqualTo(*h->helper_);
0121     }
0122 
0123     template <typename T>
0124     std::unique_ptr<RefHolderBase> IndirectHolder<T>::holder() const {
0125       return std::unique_ptr<RefHolderBase>(helper_->clone());
0126     }
0127 
0128     // Free swap function
0129     template <typename T>
0130     inline void swap(IndirectHolder<T>& lhs, IndirectHolder<T>& rhs) {
0131       lhs.swap(rhs);
0132     }
0133   }  // namespace reftobase
0134 
0135 }  // namespace edm
0136 
0137 #include "DataFormats/Common/interface/IndirectVectorHolder.h"
0138 #include "DataFormats/Common/interface/RefVectorHolderBase.h"
0139 
0140 namespace edm {
0141   namespace reftobase {
0142     template <typename T>
0143     std::unique_ptr<BaseVectorHolder<T> > IndirectHolder<T>::makeVectorHolder() const {
0144       std::unique_ptr<RefVectorHolderBase> p = helper_->makeVectorHolder();
0145       std::shared_ptr<RefVectorHolderBase> sp(p.release());
0146       return std::unique_ptr<BaseVectorHolder<T> >(new IndirectVectorHolder<T>(sp));
0147     }
0148   }  // namespace reftobase
0149 }  // namespace edm
0150 
0151 #endif