| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# SmartRefVector.h

Go to the documentation of this file.

    00001 //      ====================================================================
    00002 //      SmartRefVector.h
    00003 //      --------------------------------------------------------------------
    00004 //
    00005 //      Package   : Kernel
    00006 //
    00007 //      Author    : Markus Frank
    00008 //
    00009 //      ====================================================================
    00010 #ifndef KERNEL_SMARTREFVECTOR_H
    00011 #define KERNEL_SMARTREFVECTOR_H 1
    00012
    00013 // STL include files
    00014 #include <vector>
    00015
    00016 // Include files
    00017 #include "GaudiKernel/SmartRef.h"
    00018
    00054 template <class TYPE> class SmartRefVector : public std::vector < SmartRef<TYPE> >   {
    00055 protected:
    00057   typedef SmartRef<TYPE>          _Entry;
    00059   typedef std::vector <_Entry>    _Base;
    00060   typedef typename std::vector<_Entry>::const_iterator    _BaseConstIter;
    00061   typedef typename std::vector<_Entry>::value_type        _BaseValueType;
    00062
    00064   mutable const DataObject*      m_data;
    00066   mutable const ContainedObject* m_contd;
    00067
    00069   void _setEnvironment(const DataObject* pObj, const ContainedObject* pContd)  const  {
    00070     m_data  = pObj;
    00071     m_contd = pContd;
    00072     for ( _BaseConstIter i = _Base::begin(); i != _Base::end(); i++ )   {
    00073       (*i)._setEnvironment(pObj, pContd);
    00074     }
    00075   }
    00076 public:
    00078   SmartRefVector()  {
    00079     m_contd  = 0;
    00080     m_data   = 0;
    00081   }
    00083   template <class ITERATOR>
    00084   SmartRefVector( ITERATOR first , ITERATOR last )
    00085     :  std::vector< SmartRef<TYPE> >( first , last )
    00086     , m_data  ( 0 )
    00087     , m_contd ( 0 )
    00088   {
    00089
    00090   }
    00092   SmartRefVector(const SmartRefVector& copy)
    00093     :  std::vector< SmartRef<TYPE> >(copy) {
    00094     *this = copy;
    00095   }
    00097   //virtual ~SmartRefVector()                  {
    00098   //}
    00099
    00101   SmartRefVector<TYPE>& operator() (ContainedObject* pObj)    {
    00102     _setEnvironment((0==pObj) ? 0 : pObj->parent(), pObj);
    00103     return *this;
    00104   }
    00106   const SmartRefVector<TYPE>& operator() (const ContainedObject* pObj)  const  {
    00107     _setEnvironment((0==pObj) ? 0 : pObj->parent(), pObj);
    00108     return *this;
    00109   }
    00111   SmartRefVector<TYPE>& operator() (DataObject* pObj)    {
    00112     _setEnvironment(pObj,0);
    00113     return *this;
    00114   }
    00116   const SmartRefVector<TYPE>& operator() (const DataObject* pObj)  const  {
    00117     _setEnvironment(pObj,0);
    00118     return *this;
    00119   }
    00121   SmartRefVector<TYPE>& operator=(const SmartRefVector<TYPE>& copy)  {
    00122     _Base::operator=(copy);
    00123     // Harms.... MF
    00124     // on copy we MUST make a 1 to 1 copy
    00125     // _setEnvironment( copy.m_data, copy.m_contd );
    00126     // use instead:
    00127     m_data  = copy.m_data;
    00128     m_contd = copy.m_contd;
    00129     return *this;
    00130   }
    00132   const std::type_info* type()  const   {
    00133     return &typeid(TYPE);
    00134   }
    00136   StreamBuffer& readRefs(StreamBuffer& s);
    00138   StreamBuffer& writeRefs(StreamBuffer& s)  const;
    00140   // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
    00141   friend StreamBuffer& operator<< (StreamBuffer& _s, const SmartRefVector<TYPE>& ptr)   {
    00142     return ptr.writeRefs(_s);
    00143   }
    00145   // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
    00146   friend StreamBuffer& operator>> (StreamBuffer& _s, SmartRefVector<TYPE>& ptr)     {
    00147     return ptr.readRefs(_s);
    00148   }
    00149 };
    00150
    00151 template <class TYPE> inline
    00152 StreamBuffer& SmartRefVector<TYPE>::writeRefs(StreamBuffer& s)  const  {
    00153   long len = _Base::size();
    00154   s << len;
    00155   for ( _BaseConstIter i = _Base::begin(); i != _Base::end(); i++ )   {
    00156     (*i)._setEnvironment(m_data, m_contd);
    00157     (*i).writeRef(s);
    00158   }
    00159   return s;
    00160 }
    00161
    00162 template <class TYPE> inline
    00163 StreamBuffer& SmartRefVector<TYPE>::readRefs(StreamBuffer& s)    {
    00164   long len;
    00165   _Base::erase( _Base::begin(), _Base::end() );
    00166   s >> len;
    00167   for ( long i = 0; i < len; i++ )    {
    00168     _BaseValueType entry;
    00169     entry._setEnvironment(m_data, m_contd);
    00170     entry.readRef(s);
    00171     _Base::push_back( entry );
    00172   }
    00173   return s;
    00174 }
    00175
    00176 #endif // KERNEL_SMARTREFVECTOR_H