OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_wrapper.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef TILEAREA_WRAPPER_H
11#define TILEAREA_WRAPPER_H
12
13#include "tile_type.h"
14
19template <typename T>
20concept TileIterator = requires(const T iter) {
21 { *iter } -> std::same_as<TileIndex>;
22};
23
28template <TileIterator... Titer>
30public:
32 using difference_type = std::ptrdiff_t;
33 using iterator_category = std::forward_iterator_tag;
34 using pointer = void;
35 using reference = void;
36
42
48
54 bool operator==(const TileIteratorWrapper &other) const
55 {
56 return this->iter == other.iter;
57 }
58
63 bool operator==(const std::default_sentinel_t &) const
64 {
65 return **this == INVALID_TILE;
66 }
67
72 inline TileIndex operator*() const
73 {
74 return std::visit(dereference_visitor{}, this->iter);
75 }
76
82 {
83 std::visit(increment_visitor{}, this->iter);
84 return *this;
85 }
86
87private:
88 std::variant<Titer...> iter;
89
98 {
99 return *iter;
100 }
101 };
102
109 void operator()(auto &iter)
110 {
111 ++iter;
112 }
113 };
114};
115
121template <TileIterator Titer, typename... Tarea>
123public:
124 using iterator = Titer;
125
130 template <typename T>
132 {
133 }
134
139 template <typename T>
140 TileAreaWrapper(T &&area) : area(std::move(area))
141 {
142 }
143
148 inline iterator begin() const
149 {
150 return std::visit(begin_visitor{}, this->area);
151 }
152
157 inline std::default_sentinel_t end() const
158 {
159 return std::default_sentinel_t();
160 }
161
167 inline bool Contains(TileIndex tile) const
168 {
169 return std::visit(contains_visitor{tile}, this->area);
170 }
171
172private:
173 std::variant<Tarea...> area;
174
183 {
184 return iterator{area.begin()};
185 };
186 };
187
191
197 bool operator()(const auto &area)
198 {
199 return area.Contains(this->tile);
200 }
201 };
202};
203
204#endif /* TILEAREA_WRAPPER_H */
iterator begin() const
Returns an iterator to the start of the tile area.
std::default_sentinel_t end() const
Returns an iterator to the end of the tile area.
TileAreaWrapper(T &&area)
Construct a new tile area wrapper.
bool Contains(TileIndex tile) const
Test if a tile is part of the tile area.
TileAreaWrapper(const T &area)
Construct a new tile area wrapper.
TileIndex operator*() const
Get the current tile.
TileIteratorWrapper & operator++()
Increment the iterator and set it to the next position.
bool operator==(const TileIteratorWrapper &other) const
Compare with other iterator.
bool operator==(const std::default_sentinel_t &) const
Compare with end sentinel.
TileIteratorWrapper(TileIterator auto &&iter)
Construct the iterator wrapper.
TileIteratorWrapper(const TileIterator auto &iter)
Construct the iterator wrapper.
A type is considered a TileIterator when it has a dereference operator that returns as TileIndex.
#define T
Climate temperate.
Definition engines.h:91
Visitor to call the underlying tile area's begin method.
iterator operator()(const auto &area)
Get the iterator for the underlying tile area.
Visitor to call the underlying tile area's contains method.
bool operator()(const auto &area)
Test if the underlying tile area contains a tile.
TileIndex tile
The tile to test.
Visitor to call the underlying tile iterator's dereference operator.
TileIndex operator()(const auto &iter)
Call the underlying iterator's dereference operator.
Visitor to call the underlying tile iterator's increment operator.
void operator()(auto &iter)
Call the underlying iterator's increment operator.
Types related to tiles.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100