Chess Engine
A Chess Engine project written in C++.
Loading...
Searching...
No Matches
iterator.hpp
1#pragma once
2
3#include <cstdint>
4#include <iterator>
5template <typename Derived>
6struct Iterable {
7 [[nodiscard]] static constexpr Derived first() { return Derived::FIRST; }
8 [[nodiscard]] static constexpr Derived last() { return Derived::LAST; }
9
10 class Iterator {
11 private:
12 uint8_t m_current;
13
14 public:
15 using iterator_category = std::forward_iterator_tag;
16 using value_type = Derived;
17 using difference_type = std::ptrdiff_t;
18 using pointer = const Derived*;
19 using reference = const Derived&;
20
21 constexpr Iterator(uint8_t value) : m_current(value) {}
22
23 constexpr Derived operator*() const { return m_current; }
24
25 constexpr Iterator& operator++() {
26 ++m_current;
27 return *this;
28 }
29
30 constexpr Iterator operator++(int) {
31 Iterator tmp = *this;
32 ++m_current;
33 return tmp;
34 }
35
36 constexpr bool operator==(const Iterator& other) const { return m_current == other.m_current; }
37
38 constexpr bool operator!=(const Iterator& other) const { return m_current != other.m_current; }
39 };
40
41 class Range {
42 public:
43 [[nodiscard]] constexpr Iterator begin() const { return Derived::FIRST; }
44 [[nodiscard]] constexpr Iterator end() const { return Derived::LAST + 1; }
45 };
46
47 [[nodiscard]] static constexpr Range all() { return Range{}; }
48};
Definition iterator.hpp:10
Definition iterator.hpp:41
Definition iterator.hpp:6