Maze Builder Docs 8.2.1
Loading...
Searching...
No Matches
configurator.h
Go to the documentation of this file.
1#ifndef CONFIGURATOR_H
2#define CONFIGURATOR_H
3
4#include <MazeBuilder/algos.h>
5#include <MazeBuilder/args.h>
7
8#include <algorithm>
9#include <limits>
10#include <optional>
11#include <string>
12
15namespace mazes
16{
20 class configurator final
21 {
22 public:
23 static constexpr auto MAX_COLUMNS = 100u;
24 static constexpr auto MAX_LEVELS = 10u;
25 static constexpr auto MAX_ROWS = 100u;
26 static constexpr auto DEFAULT_DISTANCES_START = 0;
27 static constexpr auto DEFAULT_DISTANCES_END = -1;
28 static constexpr auto DEFAULT_SEED_VALUE = 1'000'000u;
29
34 configurator& ensure_rows(const unsigned int rows) noexcept
35 {
36 // Clamp to reasonable limits to prevent infinite loops and memory issues
37 m_rows = std::clamp(rows, 1u, MAX_ROWS);
38 return *this;
39 }
40
45 configurator& ensure_columns(const unsigned int columns) noexcept
46 {
47 // Clamp to reasonable limits to prevent infinite loops and memory issues
48 m_columns = std::clamp(columns, 1u, MAX_COLUMNS);
49 return *this;
50 }
51
57 configurator& ensure_levels(const unsigned int levels) noexcept
58 {
59 // Clamp to reasonable limits to prevent infinite loops and memory issues
60 // Levels are more memory-intensive than rows/columns, so lower limit
61 m_levels = std::clamp(levels, 1u, MAX_LEVELS);
62 return *this;
63 }
64
68 configurator& ensure_algo_id(const algo algorithm) noexcept
69 {
70 m_algo_id = algorithm;
71 return *this;
72 }
73
77 configurator& ensure_block_id(const int block_id) noexcept
78 {
79 m_block_id = block_id;
80 return *this;
81 }
82
86 configurator& ensure_seed(const unsigned int seed) noexcept
87 {
88 m_seed = seed;
89 return *this;
90 }
91
96 {
97 m_distances = distances;
98 return *this;
99 }
100
104 configurator& ensure_distances_start(const int start_index) noexcept
105 {
106 m_distances_start = start_index;
107 return *this;
108 }
109
113 configurator& ensure_distances_end(const int end_index) noexcept
114 {
115 m_distances_end = end_index;
116 return *this;
117 }
118
123 {
124 m_output_format_id = output_format;
125 return *this;
126 }
127
131 configurator& ensure_output_format_filename(const std::string& filename) noexcept
132 {
133 m_output_filename = filename.empty() ? std::make_optional("out.txt") : std::make_optional(filename);
134 return *this;
135 }
136
140 configurator& ensure_mask_filename(const std::string& filename) noexcept
141 {
142 m_mask_filename = filename.empty() ? std::make_optional("mask.txt") : std::make_optional(filename);
143 return *this;
144 }
145
149 configurator& set_help(bool help) noexcept
150 {
151 m_help = help;
152 return *this;
153 }
154
158 configurator& set_version(bool version) noexcept
159 {
160 m_version = version;
161 return *this;
162 }
163
167 configurator& show_steps(bool show_steps) noexcept
168 {
169 m_show_steps = show_steps;
170 return *this;
171 }
172
173 // Shorthand setter overloads (non-const, take a value; getters are const with no params)
174 configurator& rows(const unsigned int r) noexcept { return ensure_rows(r); }
175 configurator& columns(const unsigned int c) noexcept { return ensure_columns(c); }
176 configurator& levels(const unsigned int l) noexcept { return ensure_levels(l); }
177 configurator& algo_id(const algo a) noexcept { return ensure_algo_id(a); }
178 configurator& seed(const unsigned int s) noexcept { return ensure_seed(s); }
179
182 [[nodiscard]] unsigned int rows() const noexcept { return m_rows.value_or(MAX_ROWS); }
183
186 [[nodiscard]] unsigned int columns() const noexcept { return m_columns.value_or(MAX_COLUMNS); }
187
190 [[nodiscard]] unsigned int levels() const noexcept { return m_levels.value_or(MAX_LEVELS); }
191
194 [[nodiscard]] algo algo_id() const noexcept { return m_algo_id.value_or(static_cast<algo>(0)); }
195
198 [[nodiscard]] std::string mask_filename() const noexcept { return m_mask_filename.value_or(std::string{}); }
199
202 [[nodiscard]] int block_ID() const noexcept { return m_block_id.value_or(0); }
203
206 [[nodiscard]] unsigned int seed() const noexcept { return m_seed.value_or(DEFAULT_SEED_VALUE); }
207
210 [[nodiscard]] bool distances() const noexcept { return m_distances.value_or(false); }
211
214 [[nodiscard]] int distances_start() const noexcept
215 {
216 return m_distances_start.value_or(DEFAULT_DISTANCES_START);
217 }
218
221 [[nodiscard]] int distances_end() const noexcept { return m_distances_end.value_or(DEFAULT_DISTANCES_END); }
222
225 [[nodiscard]] output_format output_format_id() const noexcept
226 {
227 return m_output_format_id.value_or(output_format::STDOUT);
228 }
229
230 [[nodiscard]] bool help() const noexcept { return m_help.value_or(false); };
231
232 [[nodiscard]] bool version() const noexcept { return m_version.value_or(false); };
233
234 [[nodiscard]] bool show_steps() const noexcept { return m_show_steps.value_or(false); };
235
239 [[nodiscard]] bool has_valid_dimensions() const noexcept
240 {
241 // Check for zero dimensions (would cause infinite loops or divisions by zero)
242 if (!m_rows.has_value() || !m_columns.has_value() || !m_levels.has_value())
243 {
244 return false;
245 }
246
247 // Check for excessive dimensions (would cause memory exhaustion)
248 if (m_rows.value() > MAX_ROWS || m_columns.value() > MAX_COLUMNS || m_levels.value() > MAX_LEVELS)
249 {
250 return false;
251 }
252
253 // Check for potential overflow in total cell calculation
254 if (constexpr auto max_cells = std::numeric_limits<size_t>::max() / sizeof(void*);
255 static_cast<size_t>(m_rows.value()) * m_columns.value() * m_levels.value() > max_cells)
256 {
257 // Potential overflow detected
258 return false;
259 }
260
261 return true;
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<algo> m_algo_id;
272
273 std::optional<int> m_block_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_config_file;
286
287 std::optional<std::string> m_mask_filename;
288
289 std::optional<std::string> m_output_filename;
290
291 std::optional<bool> m_help;
292
293 std::optional<bool> m_version;
294
295 std::optional<bool> m_show_steps;
296 };
297} // namespace
298
299#endif // CONFIGURATOR_H
algo
Enumeration of maze generation algorithms.
Definition algos.h:19
Configuration class for arguments.
Definition configurator.h:21
unsigned int seed() const noexcept
Get the random seed.
Definition configurator.h:206
unsigned int rows() const noexcept
Get the number of rows.
Definition configurator.h:182
configurator & ensure_distances_start(const int start_index) noexcept
Set the distance start index.
Definition configurator.h:104
configurator & show_steps(bool show_steps) noexcept
Set whether step snapshots should be shown during generation.
Definition configurator.h:167
bool distances() const noexcept
Check if distances are calculated.
Definition configurator.h:210
configurator & ensure_distances(const bool distances) noexcept
Set the distance calculation flag.
Definition configurator.h:95
configurator & ensure_block_id(const int block_id) noexcept
Set the block ID.
Definition configurator.h:77
configurator & set_help(bool help) noexcept
Set the help flag.
Definition configurator.h:149
configurator & ensure_distances_end(const int end_index) noexcept
Set the distance end index.
Definition configurator.h:113
algo algo_id() const noexcept
Get the maze generation algorithm.
Definition configurator.h:194
configurator & ensure_algo_id(const algo algorithm) noexcept
Set the maze generation algorithm.
Definition configurator.h:68
configurator & ensure_seed(const unsigned int seed) noexcept
Set the random seed.
Definition configurator.h:86
bool has_valid_dimensions() const noexcept
Validate all configuration values are within safe limits.
Definition configurator.h:239
int distances_start() const noexcept
Get the distance start index.
Definition configurator.h:214
unsigned int levels() const noexcept
Get the number of levels.
Definition configurator.h:190
configurator & ensure_mask_filename(const std::string &filename) noexcept
Set the mask filename.
Definition configurator.h:140
configurator & ensure_output_format_filename(const std::string &filename) noexcept
Set the output filename.
Definition configurator.h:131
configurator & ensure_output_format_id(const output_format output_format) noexcept
Set the output_format ID.
Definition configurator.h:122
configurator & ensure_levels(const unsigned int levels) noexcept
Set the number of levels.
Definition configurator.h:57
int distances_end() const noexcept
Get the distance end index.
Definition configurator.h:221
unsigned int columns() const noexcept
Get the number of columns.
Definition configurator.h:186
std::string mask_filename() const noexcept
Get the mask filename.
Definition configurator.h:198
configurator & ensure_rows(const unsigned int rows) noexcept
Set the number of rows.
Definition configurator.h:34
configurator & set_version(bool version) noexcept
Set the version flag.
Definition configurator.h:158
int block_ID() const noexcept
Get the block ID.
Definition configurator.h:202
configurator & ensure_columns(const unsigned int columns) noexcept
Set the number of columns.
Definition configurator.h:45
output_format output_format_id() const noexcept
Get the output_format ID.
Definition configurator.h:225
A class that manages distances associated with cells in a grid.
Definition distances.h:22
output_format
Enum class for output_format types.
Definition output_formats.h:19