BOSS 6.6.4.p01
BESIII Offline Software System
Loading...
Searching...
No Matches
TrkHitOnTrkIter.h
Go to the documentation of this file.
1// "Adaptor" class which takes a class T, and presents a _guaranteed_ interface to T
2// This is done with templates instead of inheritance to avoid (virtual) function
3// call overhead, as these functions are going to be called from lots of loops,
4// and this way the interface gets inlined and (if everything goes well) no
5// overhead is incurred for using this iterator class compared to using
6// a 'raw' T instead... (but then you couldn't deal with different T's...)
7//
8// There is a similar example in Stroustroup, which turns an iterator into a 'checked'
9// iterator
10//
11// For the above to work, an implementation specific to 'T' has to be provided.
12// In order to allow 'T' to specify which implementation should be used, it should
13// provide a typename T::iterator_implementation that we can use here...
14//
15// To allow iterators over const TrkHitOnTrk and TrkHitOnTrk, 'T' should provide
16// a T::value_type which is either a const TrkHitOnTrk, or a TrkHitOnTrk
17//
18// BTW, the implementations tend to iterate over pointers to the valuetype,
19// and this class derefences the implementation to iterator over the valuetype instead...
20//
21#ifndef TRKHITONTRKITER_H
22#define TRKHITONTRKITER_H
23#include <iterator>
24
25//FIXME: only works on Linux: class TrkHitOnTrkIter : public std::iterator_traits<TrkHitOnTrk *>
26//FIXME: only works on Linux: class TrkHitOnTrkIter : public std::random_access_iterator<const TrkHitOnTrk, ptrdiff_t>
27//FIXME: only works on SunOS58: class TrkHitOnTrkIter : public std::iterator<std::random_access_iterator_tag,TrkHitOnTrk>
28
29class TrkHitOnTrk;
30
31template <class T>
33{
34 // by using T::iterator_value_type as our value_type, we can re-use the same
35 // code for a non-const iterator as a const iterator -- all that is needed
36 // is a slightly different 'traits' class T to be passed here; The underlying
37 // real iterator (the iterator_implementation) can be the same regardless of
38 // const or non-const...
39
40public:
41 typedef std::random_access_iterator_tag iterator_category;
42 typedef typename T::iterator_value_type value_type;
43 typedef ptrdiff_t difference_type;
46
47 typedef typename T::iterator_implementation iterator_implementation;
48 TrkHitOnTrkIter<T>() :_i() {}; // create an invalid iter...
49 TrkHitOnTrkIter<T>(const TrkHitOnTrkIter<T>& i) : _i(i._i) { }
51
52 pointer get() const { return *_i; } // this function (together with * and ->) is one of the main
53 // reasons for this class:
54 // most (all?) underlying containers contain pointers to
55 // TrkHitOnTrk, and we need to double-dereference to
56 // create the illusion of something that iterates over
57 // (const) TrkHitOnTrk instead of (const) TrkHitOnTrk*
58
59 pointer operator->() const { return this->get(); }
60 reference operator*() const { return *this->get(); }
61
62
63 // next: forward all usual random access iterator operations
64 // to the underlying actual implementation...
65
66 TrkHitOnTrkIter<T>& operator-=(int i) { _i-=i; return *this; }
67 TrkHitOnTrkIter<T>& operator+=(int i) { _i+=i; return *this; }
68
69 TrkHitOnTrkIter<T> operator-(int i) { TrkHitOnTrkIter<T> x(_i); x-=i; return x; }
70 TrkHitOnTrkIter<T> operator+(int i) { TrkHitOnTrkIter<T> x(_i); x+=i; return x; }
71
72 TrkHitOnTrkIter<T>& operator--() { --_i; return *this; } // prefix --
73 TrkHitOnTrkIter<T>& operator++() { ++_i; return *this; } // prefix ++
74
75 TrkHitOnTrkIter<T> operator--(int) { TrkHitOnTrkIter<T> x(_i); --_i; return x; } // postfix --
76 TrkHitOnTrkIter<T> operator++(int) { TrkHitOnTrkIter<T> x(_i); ++_i; return x; } // postfix ++
77
78 ptrdiff_t operator-(const TrkHitOnTrkIter<T>& i) const { return _i - i._i; }
79 bool operator==(const TrkHitOnTrkIter<T>& i) const { return _i==i._i; }
80 bool operator!=(const TrkHitOnTrkIter<T>& i) const { return !operator==(i); }
81 bool operator< (const TrkHitOnTrkIter<T>& i) const { return _i<i._i;}
82 bool operator>=(const TrkHitOnTrkIter<T>& i) const { return !operator<(i);}
83 bool operator> (const TrkHitOnTrkIter<T>& i) const { return _i>i._i;}
84 bool operator<=(const TrkHitOnTrkIter<T>& i) const { return !operator>(i);}
85
86private:
88};
89#endif
Double_t x[10]
bool operator>(const TrkHitOnTrkIter< T > &i) const
TrkHitOnTrkIter< T > & operator-=(int i)
TrkHitOnTrkIter< T > operator++(int)
value_type & reference
bool operator>=(const TrkHitOnTrkIter< T > &i) const
std::random_access_iterator_tag iterator_category
bool operator==(const TrkHitOnTrkIter< T > &i) const
T::iterator_value_type value_type
pointer operator->() const
T::iterator_implementation iterator_implementation
ptrdiff_t operator-(const TrkHitOnTrkIter< T > &i) const
TrkHitOnTrkIter< T > & operator--()
ptrdiff_t difference_type
bool operator!=(const TrkHitOnTrkIter< T > &i) const
pointer get() const
TrkHitOnTrkIter< T > operator-(int i)
bool operator<=(const TrkHitOnTrkIter< T > &i) const
TrkHitOnTrkIter< T > & operator++()
bool operator<(const TrkHitOnTrkIter< T > &i) const
value_type * pointer
reference operator*() const
TrkHitOnTrkIter< T > & operator+=(int i)
TrkHitOnTrkIter< T > operator+(int i)
TrkHitOnTrkIter< T > operator--(int)