summaryrefslogtreecommitdiffhomepage
path: root/src/tile-iterator.hpp
blob: ce4b645e970e080399488da5a62af929b4df4673 (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
#pragma once

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

namespace Magnum::Examples {

struct tile;
template<typename T> class basic_tile_iterator;
template<typename T> struct tile_tuple;

} // namespace Magnum::Examples

namespace std {

template<typename T> struct tuple_size<Magnum::Examples::tile_tuple<T>> : std::integral_constant<std::size_t, 3> {};

template<> struct tuple_element<0, Magnum::Examples::tile_tuple<Magnum::Examples::tile>> { using type = Magnum::Examples::tile&; };
template<> struct tuple_element<0, Magnum::Examples::tile_tuple<const Magnum::Examples::tile>> { using type = const Magnum::Examples::tile&; };
template<> struct tuple_element<0, const Magnum::Examples::tile_tuple<Magnum::Examples::tile>> { using type = const Magnum::Examples::tile&; };

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

} // namespace std

namespace Magnum::Examples {

template<typename T>
struct tile_tuple {
    tile_tuple(T* ptr, std::size_t pos) : data{ptr, pos} {}
    tile_tuple(const tile_tuple<T>&) = default;
    tile_tuple<T>& operator=(const tile_tuple<T>&) = default;

    template <std::size_t N>
    typename std::tuple_element<N, tile_tuple<T>>::type get()
    {
        auto& [ptr, pos] = data;
        if constexpr(N == 0)
            return 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 <std::size_t N>
    typename std::tuple_element<N, const tile_tuple<const T>>::type get() const {
        return const_cast<tile_tuple<T>&>(*this).get<N>();
    }

    auto operator<=>(const tile_tuple<T>&) const noexcept = default;

protected:
    std::tuple<T*, std::size_t> data = {nullptr, 0};
};

} // namespace Magnum::Examples

namespace Magnum::Examples {

template<typename T>
class basic_tile_iterator final : private tile_tuple<T> {
public:
    explicit basic_tile_iterator(T* ptr, std::size_t pos) noexcept : tile_tuple<T>{ptr, pos} {}
    basic_tile_iterator(const basic_tile_iterator&) = default;
    basic_tile_iterator<T>& operator=(const basic_tile_iterator<T>&) = default;

    auto operator<=>(const basic_tile_iterator<T>&) const noexcept = default;
    void swap(basic_tile_iterator<T>& other) { std::swap(this->data, other.data); }

    basic_tile_iterator<T>& operator++() { auto& [ptr, pos] = this->data; pos++; return *this; }
    basic_tile_iterator<T>  operator++(int) { auto tmp = *this; operator++(); return tmp; }
    tile_tuple<T>* operator->() { return this; }
    tile_tuple<T>& operator*() { return *this; }
};

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