Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

PHObjectHandleVector.h

Go to the documentation of this file.
00001 #ifndef __PHOBJECTHANDLEVECTOR_H__
00002 #define __PHOBJECTHANDLEVECTOR_H__
00003 
00004 #include "PHObject.h"
00005 #include "phool.h"
00006 #include <iterator>
00007 #include <cassert>
00008 #include "PHObjectHandle.h"
00009 
00010 template<typename BASE> class PHObjectHandle;
00011 template<typename BASE> class PHObjectHandleVectorIterator;
00012 template<typename BASE> class PHObjectHandleVector;
00013 
00017 template<typename BASE>
00018 class PHObjectHandleVectorIterator
00019 {
00020 public:
00021 
00022   typedef PHObjectHandleVectorIterator<BASE> iterator;
00023   typedef std::random_access_iterator_tag iterator_category;
00024   typedef PHObjectHandle<BASE> value_type;
00025   typedef PHObjectHandleVector<BASE> container;
00026   typedef int difference_type;
00027   typedef value_type& reference;
00028   typedef const value_type& const_reference;
00029   typedef value_type* pointer;
00030 
00031   PHObjectHandleVectorIterator() : fCont(0), fPos(0) {}
00032 
00033   PHObjectHandleVectorIterator(container* cont, unsigned int pos) 
00034     : fCont(cont), fPos(pos) 
00035   {
00036   }
00037 
00038   PHObjectHandleVectorIterator(const iterator& other)
00039   {
00040     fPos = other.fPos;
00041     fCont = other.fCont;
00042   }
00043 
00044   virtual ~PHObjectHandleVectorIterator() {}
00045 
00046   reference operator*();
00047 
00048   void increment() { ++fPos; }
00049   void decrement() { --fPos; }
00050 
00051   iterator& operator++() 
00052   { 
00053     increment(); return *this; 
00054   }
00055   iterator operator++(int)
00056   {
00057     iterator old = *this;
00058     increment();
00059     return old;
00060   }
00061 
00062   iterator& operator--() 
00063   {
00064     decrement(); return *this; 
00065   }
00066   iterator operator--(int) 
00067   { 
00068     iterator old = *this;
00069     decrement(); 
00070     return old; 
00071   }
00072 
00073   iterator& operator+=(difference_type n)
00074   { 
00075     fPos+=n; return *this; 
00076   }
00077   iterator operator+(difference_type n) const
00078   { 
00079     iterator newone = *this;
00080     newone += n;
00081     return newone;
00082   }
00083 
00084   iterator& operator-=(difference_type n)
00085   { 
00086     fPos-=n; return *this; 
00087   }
00088   iterator operator-(difference_type n) const
00089   { 
00090     iterator newone = *this;
00091     newone -= n;
00092     return newone;
00093   }
00094 
00095   bool operator==(const iterator& rhs) const
00096   {
00097     return (fCont==rhs.fCont && fPos==rhs.fPos); 
00098   }
00099 
00100   bool operator!=(const iterator& rhs) const
00101   {
00102     return (fCont!=rhs.fCont || fPos!=rhs.fPos); 
00103   }
00104   
00105   difference_type
00106   operator-(const iterator& other) const
00107   {
00108     return fPos - other.fPos;
00109   }
00110 
00111   difference_type
00112   operator+(const iterator& other) const
00113   {
00114     return fPos + other.fPos;
00115   }
00116 
00117   iterator
00118   operator[](difference_type n) const
00119   {
00120     return iterator(fCont,n);
00121   }
00122 
00123   bool operator < (const iterator& other) const
00124   {
00125     return ( *this - other < 0 );
00126   }
00127 
00128   bool operator <= (const iterator& other) const
00129   {
00130     return ( *this - other <= 0 );
00131   }
00132 
00133   bool operator > (const iterator& other)
00134   {
00135     return ( *this - other > 0 );
00136   }
00137  
00138   bool operator >= (const iterator& other)
00139   {
00140     return ( *this - other >= 0);
00141   }
00142 
00143 private:
00144   container* fCont;
00145   unsigned int fPos;
00146 
00147   ClassDefT(PHObjectHandleVectorIterator,1)
00148 };
00149 
00150 ClassDefT2(PHObjectHandleVectorIterator,BASE)
00151 
00152 ClassImpT(PHObjectHandleVectorIterator,BASE)
00153 
00154 
00157 template<typename BASE>
00158 class PHObjectHandleVectorConstIterator
00159 {
00160 public:
00161 
00162   typedef PHObjectHandleVectorConstIterator<BASE> iterator;
00163   typedef std::random_access_iterator_tag iterator_category;
00164   typedef const PHObjectHandle<BASE> value_type;
00165   typedef const PHObjectHandleVector<BASE> container;
00166   typedef int difference_type;
00167   typedef value_type& reference;
00168   typedef const value_type& const_reference;
00169   typedef value_type* pointer;
00170 
00171   PHObjectHandleVectorConstIterator() : fCont(0), fPos(0) {}
00172 
00173   PHObjectHandleVectorConstIterator(const container* cont, unsigned int pos) 
00174     : fCont(cont), fPos(pos) 
00175   {
00176   }
00177 
00178   PHObjectHandleVectorConstIterator(const iterator& other)
00179   {
00180     fPos = other.fPos;
00181     fCont = other.fCont;
00182   }
00183 
00184   virtual ~PHObjectHandleVectorConstIterator() {}
00185 
00186   const_reference operator*();
00187 
00188   void increment() { ++fPos; }
00189   void decrement() { --fPos; }
00190 
00191   iterator& operator++() 
00192   { 
00193     increment(); return *this; 
00194   }
00195   iterator operator++(int)
00196   {
00197     iterator old = *this;
00198     increment();
00199     return old;
00200   }
00201 
00202   iterator& operator--() 
00203   {
00204     decrement(); return *this; 
00205   }
00206   iterator operator--(int) 
00207   { 
00208     iterator old = *this;
00209     decrement(); 
00210     return old; 
00211   }
00212 
00213   iterator& operator+=(difference_type n)
00214   { 
00215     fPos+=n; return *this; 
00216   }
00217   iterator operator+(difference_type n) const
00218   { 
00219     iterator newone = *this;
00220     newone += n;
00221     return newone;
00222   }
00223 
00224   iterator& operator-=(difference_type n)
00225   { 
00226     fPos-=n; return *this; 
00227   }
00228   iterator operator-(difference_type n) const
00229   { 
00230     iterator newone = *this;
00231     newone -= n;
00232     return newone;
00233   }
00234 
00235   bool operator==(const iterator& rhs) const
00236   {
00237     return (fCont==rhs.fCont && fPos==rhs.fPos); 
00238   }
00239 
00240   bool operator!=(const iterator& rhs) const
00241   {
00242     return (fCont!=rhs.fCont || fPos!=rhs.fPos); 
00243   }
00244   
00245   difference_type
00246   operator-(const iterator& other) const
00247   {
00248     return fPos - other.fPos;
00249   }
00250 
00251   difference_type
00252   operator+(const iterator& other) const
00253   {
00254     return fPos + other.fPos;
00255   }
00256 
00257   iterator
00258   operator[](difference_type n) const
00259   {
00260     return iterator(fCont,n);
00261   }
00262 
00263   bool operator < (const iterator& other) const
00264   {
00265     return ( *this - other < 0 );
00266   }
00267 
00268   bool operator <= (const iterator& other) const
00269   {
00270     return ( *this - other <= 0 );
00271   }
00272 
00273   bool operator > (const iterator& other)
00274   {
00275     return ( *this - other > 0 );
00276   }
00277  
00278   bool operator >= (const iterator& other)
00279   {
00280     return ( *this - other >= 0);
00281   }
00282 
00283 private:
00284   container* fCont;
00285   unsigned int fPos;
00286 
00287   ClassDefT(PHObjectHandleVectorConstIterator,1)
00288 };
00289 
00290 ClassDefT2(PHObjectHandleVectorConstIterator,BASE)
00291 
00292 ClassImpT(PHObjectHandleVectorConstIterator,BASE)
00293 
00294 template<typename BASE>
00295 PHObjectHandle<BASE>* 
00296 make_dummy()
00297 { 
00298   // this is just to pacify the compiler w.r.t. the
00299   // PHObjectHandleVector::get methods (which have to return
00300   // something, even if dummy -remember that anyway
00301   // PHObjectHandleVector<BASE> ought to be pure virtual so you
00302   // won't use it directly).
00303   return new PHObjectHandle<BASE>;
00304 }
00305 
00306 
00310 //_____________________________________________________________________________
00311 template<typename BASE>
00312 class PHObjectHandleVector : public PHObject
00313 {
00314 public:
00315 
00316   typedef PHObjectHandle<BASE> value_type;
00317   typedef const value_type& const_reference;
00318   typedef value_type& reference;
00319   typedef unsigned int size_type;
00320   typedef PHObjectHandleVectorIterator<BASE> iterator;
00321   typedef PHObjectHandleVectorConstIterator<BASE> const_iterator;
00322 
00323   virtual ~PHObjectHandleVector() {}
00324 
00326   virtual value_type& add(const size_type& index) = 0;
00327 
00329   virtual value_type& add(const size_type& index, const value_type& value) = 0;
00330 
00332   iterator begin()
00333   { return iterator(this,0); }
00334 
00335   const_iterator begin() const
00336   { return const_iterator(this,0); }
00337 
00338   virtual size_type capacity() const
00339   { PHOOL_VIRTUAL_WARNING; return 0; }
00340 
00341   virtual PHObjectHandleVector<BASE>* clone() const
00342   { PHOOL_VIRTUAL_WARNING; return 0; }
00343 
00344   virtual PHObjectHandleVector<BASE>* create() const
00345   { PHOOL_VIRTUAL_WARNING; return 0; }
00346 
00347   virtual bool empty() const
00348   { PHOOL_VIRTUAL_WARNING; return true; }
00349 
00350   iterator end()
00351   { return iterator(this,size()); }
00352 
00353   const_iterator end() const
00354   { return const_iterator(this,size()); }
00355 
00356   virtual iterator erase(iterator first, iterator last)
00357   { PHOOL_VIRTUAL_WARNING; return first; }
00358 
00359   virtual iterator erase(iterator pos)
00360   { PHOOL_VIRTUAL_WARNING; return pos; }
00361 
00362   virtual const_reference get(const size_type& index) const
00363   { PHOOL_VIRTUAL_WARNING; return *make_dummy<BASE>(); }
00364 
00365   virtual reference get(const size_type& index)
00366   { PHOOL_VIRTUAL_WARNING; return *make_dummy<BASE>(); }
00367 
00368   virtual void identify(std::ostream& os = std::cout) const
00369   { PHOOL_VIRTUAL_WARNING; }
00370 
00371   virtual iterator insert(iterator pos, size_t n, const value_type& value)
00372   { PHOOL_VIRTUAL_WARNING; return pos; }
00373 
00374   virtual iterator insert(iterator pos, const value_type& value)
00375   { PHOOL_VIRTUAL_WARNING; return pos; }
00376 
00377   virtual int isValid() const
00378   { PHOOL_VIRTUAL_WARNING; return 0; }
00379 
00380   virtual bool remove(const size_type& index)
00381   { PHOOL_VIRTUAL_WARNING; return false; }
00382 
00383   virtual void reserve(const size_type& thesize)
00384   { PHOOL_VIRTUAL_WARNING; }
00385 
00386   virtual void Reset()
00387   { PHOOL_VIRTUAL_WARNING; }
00388 
00389   virtual bool resize(const size_type& newsize, const value_type& value)
00390   { PHOOL_VIRTUAL_WARNING; return false; }
00391 
00392   virtual bool resize(const size_type& newsize)
00393   { PHOOL_VIRTUAL_WARNING; return false;}
00394 
00395   virtual size_type size() const
00396   { PHOOL_VIRTUAL_WARNING; return 0; }
00397 
00398 protected:
00399 
00400   PHObjectHandleVector<BASE>& operator=(const PHObjectHandleVector<BASE>&);
00401 
00402   ClassDefT(PHObjectHandleVector,1)
00403 };
00404 
00405 ClassDefT2(PHObjectHandleVector,BASE)
00406   ClassImpT(PHObjectHandleVector,BASE)
00407 
00408 #ifndef __CINT__
00409 
00410 //_____________________________________________________________________________
00411 //_____________________________________________________________________________
00412   
00413 template<typename BASE>
00414 typename PHObjectHandleVectorIterator<BASE>::reference
00415 PHObjectHandleVectorIterator<BASE>::operator*()
00416 {
00417   return fCont->get(fPos);
00418 }
00419 
00420 template<typename BASE>
00421 typename PHObjectHandleVectorConstIterator<BASE>::const_reference
00422 PHObjectHandleVectorConstIterator<BASE>::operator*()
00423 {
00424   return fCont->get(fPos);
00425 }
00426 
00427 #endif
00428 
00429 #endif