CGEM BOSS 6.6.5.g
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#include <cstddef>
25
26//FIXME: only works on Linux: class TrkHitOnTrkIter : public std::iterator_traits<TrkHitOnTrk *>
27//FIXME: only works on Linux: class TrkHitOnTrkIter : public std::random_access_iterator<const TrkHitOnTrk, ptrdiff_t>
28//FIXME: only works on SunOS58: class TrkHitOnTrkIter : public std::iterator<std::random_access_iterator_tag,TrkHitOnTrk>
29
30class TrkHitOnTrk;
31
32template <class T>
34{
35 // by using T::iterator_value_type as our value_type, we can re-use the same
36 // code for a non-const iterator as a const iterator -- all that is needed
37 // is a slightly different 'traits' class T to be passed here; The underlying
38 // real iterator (the iterator_implementation) can be the same regardless of
39 // const or non-const...
40
41public:
42 typedef std::random_access_iterator_tag iterator_category;
43 typedef typename T::iterator_value_type value_type;
44 typedef ptrdiff_t difference_type;
47
48 typedef typename T::iterator_implementation iterator_implementation;
49 TrkHitOnTrkIter<T>() :_i() {}; // create an invalid iter...
50 TrkHitOnTrkIter<T>(const TrkHitOnTrkIter<T>& i) : _i(i._i) { }
52
53 pointer get() const { return *_i; } // this function (together with * and ->) is one of the main
54 // reasons for this class:
55 // most (all?) underlying containers contain pointers to
56 // TrkHitOnTrk, and we need to double-dereference to
57 // create the illusion of something that iterates over
58 // (const) TrkHitOnTrk instead of (const) TrkHitOnTrk*
59
60 pointer operator->() const { return this->get(); }
61 reference operator*() const { return *this->get(); }
62
63
64 // next: forward all usual random access iterator operations
65 // to the underlying actual implementation...
66
67 TrkHitOnTrkIter<T>& operator-=(int i) { _i-=i; return *this; }
68 TrkHitOnTrkIter<T>& operator+=(int i) { _i+=i; return *this; }
69
70 TrkHitOnTrkIter<T> operator-(int i) { TrkHitOnTrkIter<T> x(_i); x-=i; return x; }
71 TrkHitOnTrkIter<T> operator+(int i) { TrkHitOnTrkIter<T> x(_i); x+=i; return x; }
72
73 TrkHitOnTrkIter<T>& operator--() { --_i; return *this; } // prefix --
74 TrkHitOnTrkIter<T>& operator++() { ++_i; return *this; } // prefix ++
75
76 TrkHitOnTrkIter<T> operator--(int) { TrkHitOnTrkIter<T> x(_i); --_i; return x; } // postfix --
77 TrkHitOnTrkIter<T> operator++(int) { TrkHitOnTrkIter<T> x(_i); ++_i; return x; } // postfix ++
78
79 ptrdiff_t operator-(const TrkHitOnTrkIter<T>& i) const { return _i - i._i; }
80 bool operator==(const TrkHitOnTrkIter<T>& i) const { return _i==i._i; }
81 bool operator!=(const TrkHitOnTrkIter<T>& i) const { return !operator==(i); }
82 bool operator< (const TrkHitOnTrkIter<T>& i) const { return _i<i._i;}
83 bool operator>=(const TrkHitOnTrkIter<T>& i) const { return !operator<(i);}
84 bool operator> (const TrkHitOnTrkIter<T>& i) const { return _i>i._i;}
85 bool operator<=(const TrkHitOnTrkIter<T>& i) const { return !operator>(i);}
86
87private:
89};
90#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)