CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/DataFormats/Common/interface/VectorHolder.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_VectorHolder_h
00002 #define DataFormats_Common_VectorHolder_h
00003 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
00004 #include "DataFormats/Common/interface/BaseVectorHolder.h"
00005 #include "DataFormats/Common/interface/Holder.h"
00006 #include <memory>
00007 
00008 namespace edm {
00009   namespace reftobase {
00010     
00011     class RefVectorHolderBase;
00012     
00013     template <class T, class REFV>
00014     class VectorHolder : public BaseVectorHolder<T> {
00015     public:
00016       typedef BaseVectorHolder<T>                base_type;
00017       typedef typename base_type::size_type      size_type;
00018       typedef typename base_type::element_type   element_type;
00019       typedef typename base_type::base_ref_type  base_ref_type;
00020       typedef typename base_type::const_iterator const_iterator;
00021       typedef REFV                               ref_vector_type;
00022       
00023       VectorHolder() : base_type() {}
00024       VectorHolder(VectorHolder const & rh) :  base_type(rh),  refVector_(rh.refVector_){}
00025 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
00026       VectorHolder(VectorHolder && rh) :  base_type(std::forward(rh)),  refVector_(std::move(rh.refVector_)){}
00027  #endif
00028 
00029       explicit VectorHolder(const ref_vector_type& iRefVector) : base_type(), refVector_(iRefVector) {}
00030       explicit VectorHolder(const ProductID& iId) : base_type(), refVector_(iId) {}
00031       virtual ~VectorHolder() {}
00032       virtual base_type* clone() const { return new VectorHolder(*this); }
00033       virtual base_type* cloneEmpty() const { return new VectorHolder(refVector_.id()); }
00034       base_ref_type const at(size_type idx) const { return base_ref_type( refVector_.at( idx ) ); }
00035       bool empty() const { return refVector_.empty(); }
00036       size_type size() const { return refVector_.size(); }
00037       //size_type capacity() const { return refVector_.capacity(); }
00038       //void reserve(size_type n) { refVector_.reserve(n); }
00039       void clear() { refVector_.clear(); }
00040       ProductID id() const { return refVector_.id(); }
00041       EDProductGetter const* productGetter() const { return refVector_.productGetter(); }
00042       void swap(VectorHolder& other) {
00043         this->BaseVectorHolder<T>::swap(other);
00044         refVector_.swap(other.refVector_);
00045       }
00046       VectorHolder& operator=(VectorHolder const& rhs) {
00047         VectorHolder temp(rhs);
00048         this->swap(temp);
00049         return *this;
00050       }
00051 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
00052      VectorHolder& operator=(VectorHolder && rhs) {
00053        base_type::operator=(std::forward(rhs));
00054        refVector_ = std::move(rhs.refVector_);
00055        return *this;
00056      }
00057 #endif
00058       
00059       const_iterator begin() const {
00060         return const_iterator( new const_iterator_imp_specific( refVector_.begin() ) );
00061       }
00062       const_iterator end() const {
00063         return const_iterator( new const_iterator_imp_specific( refVector_.end() ) );
00064       }
00065       virtual void push_back( const BaseHolder<T> * r ) {
00066         typedef Holder<T, typename REFV::value_type > holder_type;
00067         const holder_type * h = dynamic_cast<const holder_type *>( r );
00068         if( h == 0 )
00069           Exception::throwThis( errors::InvalidReference,
00070                                "In VectorHolder<T, REFV> trying to push_back wrong reference type");
00071         refVector_.push_back( h->getRef() );
00072       }
00073       virtual std::auto_ptr<RefVectorHolderBase> vectorHolder() const {
00074         return std::auto_ptr<RefVectorHolderBase>( new RefVectorHolder<REFV>( refVector_ ) );
00075       }
00076       virtual const void * product() const {
00077         return refVector_.product();
00078       }
00079       
00082       virtual bool isAvailable() const { return refVector_.isAvailable(); }
00083       
00084       //Used by ROOT storage
00085       CMS_CLASS_VERSION(10)
00086 
00087     private:
00088       typedef typename base_type::const_iterator_imp const_iterator_imp;
00089       
00090       ref_vector_type refVector_;
00091       
00092       // the following structure is public
00093       // to allow reflex dictionary to compile
00094     public:
00095       struct const_iterator_imp_specific : public const_iterator_imp {
00096         typedef ptrdiff_t difference_type;
00097         const_iterator_imp_specific() { }
00098         explicit const_iterator_imp_specific( const typename REFV::const_iterator & it ) : i ( it ) { }
00099         ~const_iterator_imp_specific() { }
00100         const_iterator_imp_specific * clone() const { return new const_iterator_imp_specific( i ); }
00101         void increase() { ++i; }
00102         void decrease() { --i; }
00103         void increase( difference_type d ) { i += d; }
00104         void decrease( difference_type d ) { i -= d; }
00105         bool equal_to( const const_iterator_imp * o ) const { return i == dc( o ); }
00106         bool less_than( const const_iterator_imp * o ) const { return i < dc( o ); }
00107         void assign( const const_iterator_imp * o ) { i = dc( o ); }
00108         base_ref_type deref() const { return base_ref_type( * i ); }
00109         difference_type difference( const const_iterator_imp * o ) const { return i - dc( o ); }
00110       private:
00111         const typename ref_vector_type::const_iterator & dc( const const_iterator_imp * o ) const {
00112           if ( o == 0 )
00113             Exception::throwThis( errors::InvalidReference,
00114                                  "In RefToBaseVector<T> trying to dereference a null pointer");
00115           const const_iterator_imp_specific * oo = dynamic_cast<const const_iterator_imp_specific *>( o );
00116           if ( oo == 0 )
00117             Exception::throwThis( errors::InvalidReference,
00118                                  "In RefToBaseVector<T> trying to cast iterator to wrong type ");
00119           return oo->i;
00120         }
00121         typename ref_vector_type::const_iterator i;
00122       };
00123     };
00124     
00125     // Free swap function
00126     template <typename T, typename REFV>
00127     inline
00128     void
00129     swap(VectorHolder<T, REFV>& lhs, VectorHolder<T, REFV>& rhs) {
00130       lhs.swap(rhs);
00131     }
00132   }
00133 }
00134 
00135 #endif