Maze Builder Docs 7.5.6
Loading...
Searching...
No Matches
configurator.h
Go to the documentation of this file.
1#ifndef CONFIGURATOR_H
2#define CONFIGURATOR_H
3
4#include <limits>
5#include <memory>
6#include <optional>
7#include <string>
8
10#include <MazeBuilder/dfs.h>
11#include <MazeBuilder/enums.h>
13
14namespace mazes
15{
16 class algo_interface;
17
22 class configurator final
23 {
24
25 public:
26 static constexpr auto DEFAULT_ROWS = 10u;
27
28 static constexpr auto DEFAULT_COLUMNS = 10u;
29
30 static constexpr auto DEFAULT_LEVELS = 1u;
31
32 static constexpr auto DEFAULT_BLOCK_ID = 0;
33
34 static constexpr auto DEFAULT_ALGO_ID = algo::BINARY_TREE;
35
36 static constexpr auto DEFAULT_SEED = 0u;
37
38 static constexpr auto DEFAULT_OUTPUT_ID = output_format::PLAIN_TEXT;
39
40 static constexpr auto DEFAULT_FILENAME = "";
41
42 static constexpr auto DEFAULT_DISTANCES = false;
43
44 static constexpr auto DEFAULT_DISTANCES_START = 0;
45
46 static constexpr auto DEFAULT_DISTANCES_END = -1;
47
48 static constexpr auto MAX_ROWS = 100u;
49
50 static constexpr auto MAX_COLUMNS = 100u;
51
52 static constexpr auto MAX_LEVELS = 10u;
53
58 configurator &rows(unsigned int rows) noexcept
59 {
60 // Clamp to reasonable limits to prevent infinite loops and memory issues
61 m_rows = (rows == 0) ? 1 : (rows > MAX_ROWS) ? MAX_ROWS
62 : rows;
63 return *this;
64 }
65
70 configurator &columns(unsigned int columns) noexcept
71 {
72 // Clamp to reasonable limits to prevent infinite loops and memory issues
73 m_columns = (columns == 0) ? 1 : (columns > MAX_COLUMNS) ? MAX_COLUMNS
74 : columns;
75 return *this;
76 }
77
83 configurator &levels(unsigned int levels) noexcept
84 {
85 // Clamp to reasonable limits to prevent infinite loops and memory issues
86 // Levels are more memory-intensive than rows/columns, so lower limit
87 m_levels = (levels == 0) ? 1 : (levels > MAX_LEVELS) ? MAX_LEVELS
88 : levels;
89 return *this;
90 }
91
95 configurator &algo_id(algo algorithm) noexcept
96 {
97 m_algo_id = algorithm;
98 return *this;
99 }
100
105 {
106 m_block_id = block_id;
107 return *this;
108 }
109
113 configurator &seed(unsigned int seed) noexcept
114 {
115 m_seed = seed;
116 return *this;
117 }
118
123 {
124 m_distances = distances;
125 return *this;
126 }
127
131 configurator &distances_start(int start_index) noexcept
132 {
133 m_distances_start = start_index;
134 return *this;
135 }
136
140 configurator &distances_end(int end_index) noexcept
141 {
142 m_distances_end = end_index;
143 return *this;
144 }
145
150 {
151 m_output_format_id = output_format;
152 return *this;
153 }
154
158 configurator &output_format_filename(std::string filename) noexcept
159 {
160 m_output_format_filename = std::move(filename);
161 return *this;
162 }
163
166 unsigned int rows() const noexcept { return m_rows.value_or(DEFAULT_ROWS); }
167
170 unsigned int columns() const noexcept { return m_columns.value_or(DEFAULT_COLUMNS); }
171
174 unsigned int levels() const noexcept { return m_levels.value_or(DEFAULT_LEVELS); }
175
178 algo algo_id() const noexcept { return m_algo_id.value_or(DEFAULT_ALGO_ID); }
179
182 int block_id() const noexcept { return m_block_id.value_or(DEFAULT_BLOCK_ID); }
183
186 unsigned int seed() const noexcept { return m_seed.value_or(DEFAULT_SEED); }
187
190 bool distances() const noexcept { return m_distances.value_or(DEFAULT_DISTANCES); }
191
194 int distances_start() const noexcept { return m_distances_start.value_or(DEFAULT_DISTANCES_START); }
195
198 int distances_end() const noexcept { return m_distances_end.value_or(DEFAULT_DISTANCES_END); }
199
202 output_format output_format_id() const noexcept { return m_output_format_id.value_or(DEFAULT_OUTPUT_ID); }
203
206 std::string output_format_filename() const noexcept { return m_output_format_filename.value_or(std::string{ DEFAULT_FILENAME }); }
207
211 bool is_valid() const noexcept
212 {
213
214 // Check for zero dimensions (would cause infinite loops or divisions by zero)
215 if (!m_rows.has_value() || !m_columns.has_value() || !m_levels.has_value())
216 {
217
218 return false;
219 }
220
221 // Check for excessive dimensions (would cause memory exhaustion)
222 if (m_rows.value() > MAX_ROWS || m_columns.value() > MAX_COLUMNS || m_levels.value() > MAX_LEVELS)
223 {
224
225 return false;
226 }
227
228 // Check for potential overflow in total cell calculation
229 constexpr auto max_cells = std::numeric_limits<size_t>::max() / sizeof(void *);
230
231 if (static_cast<size_t>(m_rows.value()) * m_columns.value() * m_levels.value() > max_cells)
232 {
233 // Potential overflow detected
234 return false;
235 }
236
237 return true;
238 }
239
243 static std::optional<std::unique_ptr<algo_interface>> make_algo_from_config(const configurator &config)
244 {
245 if (config.algo_id() == algo::DFS)
246 {
247
248 return std::make_optional(std::make_unique<dfs>());
249 }
250 else if (config.algo_id() == algo::BINARY_TREE)
251 {
252
253 return std::make_optional(std::make_unique<binary_tree>());
254 }
255 else if (config.algo_id() == algo::SIDEWINDER)
256 {
257
258 return std::make_optional(std::make_unique<sidewinder>());
259 }
260
261 return std::nullopt;
262 }
263
264 private:
265 std::optional<unsigned int> m_rows;
266
267 std::optional<unsigned int> m_columns;
268
269 std::optional<unsigned int> m_levels;
270
271 std::optional<int> m_block_id;
272
273 std::optional<algo> m_algo_id;
274
275 std::optional<unsigned int> m_seed;
276
277 std::optional<bool> m_distances;
278
279 std::optional<int> m_distances_start;
280
281 std::optional<int> m_distances_end;
282
283 std::optional<output_format> m_output_format_id;
284
285 std::optional<std::string> m_output_format_filename;
286 };
287
288} // namespace
289
290#endif // CONFIGURATOR_H
Configuration class for arguments.
Definition configurator.h:23
unsigned int seed() const noexcept
Get the random seed.
Definition configurator.h:186
unsigned int rows() const noexcept
Get the number of rows.
Definition configurator.h:166
configurator & columns(unsigned int columns) noexcept
Set the number of columns.
Definition configurator.h:70
configurator & output_format_filename(std::string filename) noexcept
Set the output_format filename.
Definition configurator.h:158
bool distances() const noexcept
Check if distances are calculated.
Definition configurator.h:190
configurator & block_id(int block_id) noexcept
Set the block ID.
Definition configurator.h:104
int block_id() const noexcept
Get the block ID.
Definition configurator.h:182
algo algo_id() const noexcept
Get the maze generation algorithm.
Definition configurator.h:178
bool is_valid() const noexcept
Validate all configuration values are within safe limits.
Definition configurator.h:211
configurator & levels(unsigned int levels) noexcept
Set the number of levels.
Definition configurator.h:83
configurator & distances(bool distances) noexcept
Set the distance calculation flag.
Definition configurator.h:122
configurator & algo_id(algo algorithm) noexcept
Set the maze generation algorithm.
Definition configurator.h:95
int distances_start() const noexcept
Get the distance start index.
Definition configurator.h:194
unsigned int levels() const noexcept
Get the number of levels.
Definition configurator.h:174
configurator & distances_end(int end_index) noexcept
Set the distance end index.
Definition configurator.h:140
configurator & seed(unsigned int seed) noexcept
Set the random seed.
Definition configurator.h:113
configurator & rows(unsigned int rows) noexcept
Set the number of rows.
Definition configurator.h:58
int distances_end() const noexcept
Get the distance end index.
Definition configurator.h:198
unsigned int columns() const noexcept
Get the number of columns.
Definition configurator.h:170
std::string output_format_filename() const noexcept
Get the output_format filename.
Definition configurator.h:206
static std::optional< std::unique_ptr< algo_interface > > make_algo_from_config(const configurator &config)
Determine the maze generation algorithm from the configuration.
Definition configurator.h:243
configurator & distances_start(int start_index) noexcept
Set the distance start index.
Definition configurator.h:131
output_format output_format_id() const noexcept
Get the output_format ID.
Definition configurator.h:202
configurator & output_format_id(output_format output_format) noexcept
Set the output_format ID.
Definition configurator.h:149
A class that manages distances associated with cells in a grid.
Definition distances.h:19
Enumerations and utilities for the maze builder program.
Namespace for the maze builder.
Definition algo_interface.h:6
algo
Enum class for maze types by the generating algorithm.
Definition enums.h:130
output_format
Enum class for output_format types.
Definition enums.h:30