Correctly handle "torrent finished after move" event
[qBittorrent.git] / src / base / indexrange.h
blob31b5fefea3deb34d8425b47d379cdba0d3e6df61
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Eugene Shalygin
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #pragma once
31 #include <QtAssert>
33 // Interval is defined via [first;last]
34 template <typename Index>
35 class IndexInterval
37 public:
38 using IndexType = Index;
40 constexpr IndexInterval(const IndexType first, const IndexType last)
41 : m_first {first}
42 , m_last {last}
44 Q_ASSERT(first <= last);
47 constexpr IndexType first() const
49 return m_first;
52 constexpr IndexType last() const
54 return m_last;
57 private:
58 IndexType m_first;
59 IndexType m_last;
62 template <typename T>
63 constexpr IndexInterval<T> makeInterval(const T first, const T last)
65 return {first, last};
68 // range is defined via first index and size
69 template <typename Index, typename IndexDiff = Index>
70 class IndexRange
72 public:
73 using IndexType = Index;
74 using IndexDiffType = IndexDiff;
76 class Iterator
78 public:
79 explicit constexpr Iterator(const IndexType index)
80 : m_index {index}
84 constexpr Iterator(const Iterator &) = default;
86 constexpr IndexType operator*() const
88 return m_index;
91 constexpr Iterator &operator++()
93 ++m_index;
94 return *this;
97 constexpr Iterator operator++(int)
99 const Iterator iter {*this};
100 ++(*this);
101 return iter;
104 // comparing iterators from different containers is undefined behavior in C++ standard library
105 friend constexpr bool operator==(const Iterator &left, const Iterator &right)
107 return (*left == *right);
110 private:
111 IndexType m_index;
114 constexpr IndexRange()
115 : m_first {0}
116 , m_size {0}
120 constexpr IndexRange(const IndexType first, const IndexDiffType size)
121 : m_first {first}
122 , m_size {size}
126 constexpr IndexRange(const IndexInterval<IndexType> &interval)
127 : m_first {interval.first()}
128 , m_size {interval.last() - interval.first() + 1}
132 constexpr Iterator begin() const
134 return Iterator {m_first};
137 constexpr Iterator end() const
139 return Iterator {m_first + m_size};
142 constexpr IndexDiffType size() const
144 return m_size;
147 constexpr IndexType first() const
149 return m_first;
152 constexpr IndexType last() const
154 return (m_first + m_size - 1);
157 constexpr bool isEmpty() const
159 return (m_size == 0);
162 private:
163 IndexType m_first;
164 IndexDiffType m_size;