summaryrefslogtreecommitdiffhomepage
path: root/src/tile-iterator.hpp
blob: 0fa5049d2c9ba7b86d65494a64d1289367c07a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once

#include "local-coords.hpp"
#include <iterator>
#include <utility>
#include <type_traits>

namespace Magnum::Examples {

template<typename T> class basic_tile_iterator;
struct tile;

} // namespace Magnum::Examples

namespace std {

template<typename T>
void swap(Magnum::Examples::basic_tile_iterator<T>& lhs,
          Magnum::Examples::basic_tile_iterator<T>& rhs) noexcept;

template<typename T> struct tuple_element<0, Magnum::Examples::basic_tile_iterator<T>> { using type = T&; };
template<typename T> struct tuple_element<0, const Magnum::Examples::basic_tile_iterator<T>> { using type = const T&; };

template<typename T> struct tuple_element<1, Magnum::Examples::basic_tile_iterator<T>> { using type = std::size_t; };
template<typename T> struct tuple_element<2, Magnum::Examples::basic_tile_iterator<T>> { using type = Magnum::Examples::local_coords; };
template<typename T> struct tuple_size<Magnum::Examples::basic_tile_iterator<T>> : std::integral_constant<std::size_t, 3> {};

} // namespace std

namespace Magnum::Examples {

template<typename T>
class basic_tile_iterator final {
    T* ptr;
    std::size_t pos = 0;

public:
    using value_type = std::tuple<T&, std::size_t, local_coords>;

    explicit basic_tile_iterator(T* ptr, std::size_t pos) noexcept : ptr(ptr), pos(pos) {}
    ~basic_tile_iterator() noexcept = default;

    basic_tile_iterator<T>& operator=(const basic_tile_iterator<T>&) = default;
    basic_tile_iterator<T>& operator++() { pos++; return *this; }
    basic_tile_iterator<T>  operator++(int) { auto tmp = *this; operator++(); return tmp; }
    basic_tile_iterator<T>* operator->() & { return this; }
    basic_tile_iterator<T>& operator*() & { return *this; }
    basic_tile_iterator<T> const* operator->() const& { return this; }
    basic_tile_iterator<T> const& operator*() const& { return *this; }
    auto operator<=>(const basic_tile_iterator<T>&) const noexcept = default;
    void swap(basic_tile_iterator<T>& other);
    template<std::size_t N> typename std::tuple_element<N, basic_tile_iterator<T>>::type get() &;
    template<std::size_t N> typename std::tuple_element<N, basic_tile_iterator<T>>::type get() const&;
};

template <typename T>
void basic_tile_iterator<T>::swap(basic_tile_iterator<T>& other)
{
    using std::swap;
    swap(ptr, other.ptr);
    swap(pos, other.pos);
}

template <typename T>
template <std::size_t N>
typename std::tuple_element<N, basic_tile_iterator<T>>::type basic_tile_iterator<T>::get() &
{
    if constexpr(N == 0)
        return pos == TILE_COUNT ? ptr[0] : ptr[pos];
    else if constexpr(N == 1)
        return pos;
    else if constexpr(N == 2)
        return local_coords{pos};
    else
        return std::void_t<std::integral_constant<int, N>>();
}

template <typename T>
template <std::size_t N>
typename std::tuple_element<N, basic_tile_iterator<T>>::type basic_tile_iterator<T>::get() const&
{
    return const_cast<basic_tile_iterator<T>&>(*this).get<N>();
}

extern template class basic_tile_iterator<tile>;
extern template class basic_tile_iterator<const tile>;

} // namespace Magnum::Examples

namespace std {

template<typename Tile>
class iterator_traits<Magnum::Examples::basic_tile_iterator<Tile>> {
    using T = Magnum::Examples::basic_tile_iterator<Tile>;
public:
    using difference_type = std::ptrdiff_t;
    using value_type = T;
    using reference = T&;
    using pointer = T*;
    using iterator_category = std::input_iterator_tag;  //usually std::forward_iterator_tag or similar
};

} // namespace std