Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
qx-traverser.h
1#ifndef QX_STRINGTRAVERSER_H
2#define QX_STRINGTRAVERSER_H
3
4// Qt Includes
5#include <QString>
6
7// Extra-component Includes
9
10namespace Qx
11{
12
13template<typename T>
14 requires traverseable<T>
16{
17//-Instance Members----------------------------------------------------------------------------------------------------
18private:
19 typename T::const_iterator mIterator;
20 typename T::const_iterator mStart;
21 typename T::const_iterator mEnd;
22 qsizetype mIndex;
23 qsizetype mLastIndex;
24
25//-Constructor-------------------------------------------------------------------------------------------------------
26public:
28 mIterator(traverseable.cbegin()),
29 mStart(traverseable.cbegin()),
30 mEnd(traverseable.cend()),
31 mIndex(mIterator == mEnd ? -1 : 0), // -1 for empty traverseable
32 mLastIndex(traverseable.size() - 1)
33 {}
34
35//-Instance Functions--------------------------------------------------------------------------------------------------
36public:
37 void advance(quint32 count = 1)
38 {
39 // Set to end if over end, otherwise advance to target
40 if(mIndex + count > mLastIndex)
41 mIterator = mEnd;
42 else
43 {
44 mIterator += count;
45 mIndex += count;
46 }
47 }
48
49 void retreat(quint32 count = 1)
50 {
51 // Set to beginning if before start, otherwise retreat to target
52 if(mIndex - count < 0)
53 mIterator = mStart;
54 else
55 {
56 mIterator -= count;
57 mIndex -= count;
58 }
59 }
60
61 bool atEnd() const { return mIterator == mEnd; }
62
63 std::iter_value_t<typename T::const_iterator> currentValue() const { return *mIterator; }
64 quint32 currentIndex() const { return mIndex; }
65
66 std::iter_value_t<typename T::const_iterator> lookAhead(quint32 count = 1) const
67 {
68 // Return default constructed object if over end, otherwise return target
69 return (mIndex + count > mLastIndex) ? std::iter_value_t<T>() : *(mIterator + count);
70 }
71
72 std::iter_value_t<typename T::const_iterator> lookBehind(quint32 count = 1) const
73 {
74 // Return default constructed object if before start, otherwise return target
75 return (mIndex - count < mLastIndex) ? std::iter_value_t<T>() : *(mIterator - count);
76 }
77};
78
80
81}
82
83#endif // QX_STRINGTRAVERSER_H
The Traverser template class provides a const_iterator wrapper for an alternate approach to iterating...
Definition qx-traverser.h:16
std::iter_value_t< typename T::const_iterator > lookBehind(quint32 count=1) const
Definition qx-traverser.h:72
void advance(quint32 count=1)
Definition qx-traverser.h:37
bool atEnd() const
Definition qx-traverser.h:61
quint32 currentIndex() const
Definition qx-traverser.h:64
void retreat(quint32 count=1)
Definition qx-traverser.h:49
Traverser(const T &traverseable)
Definition qx-traverser.h:27
std::iter_value_t< typename T::const_iterator > currentValue() const
Definition qx-traverser.h:63
std::iter_value_t< typename T::const_iterator > lookAhead(quint32 count=1) const
Definition qx-traverser.h:66
Specifies that a type is a valid type for Qx::Traverser.
Definition qx-concepts.h:503
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
Traverser< QString > StringTraverser
Definition qx-traverser.h:79
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...