Maze Builder Docs 8.10.23
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 = 100u;
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
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
78 {
79 m_distances = distances;
80 return *this;
81 }
82
86 configurator& ensure_distances_start(const int start_index) noexcept
87 {
88 m_distances_start = start_index;
89 return *this;
90 }
91
95 configurator& ensure_distances_end(const int end_index) noexcept
96 {
97 m_distances_end = end_index;
98 return *this;
99 }
100
101 // Shorthand setter overloads (non-const, take a value; getters are const with no params)
102 configurator& rows(const unsigned int r) noexcept { return ensure_rows(r); }
103 configurator& columns(const unsigned int c) noexcept { return ensure_columns(c); }
104 configurator& levels(const unsigned int l) noexcept { return ensure_levels(l); }
105 configurator& algo_id(const algo a) noexcept { return ensure_algo_id(a); }
106
109 [[nodiscard]] unsigned int rows() const noexcept { return m_rows.value_or(MAX_ROWS); }
110
113 [[nodiscard]] unsigned int columns() const noexcept { return m_columns.value_or(MAX_COLUMNS); }
114
117 [[nodiscard]] unsigned int levels() const noexcept { return m_levels.value_or(MAX_LEVELS); }
118
121 [[nodiscard]] algo algo_id() const noexcept { return m_algo_id.value_or(static_cast<algo>(0)); }
122
125 [[nodiscard]] bool distances() const noexcept { return m_distances.value_or(false); }
126
129 [[nodiscard]] int distances_start() const noexcept
130 {
131 return m_distances_start.value_or(DEFAULT_DISTANCES_START);
132 }
133
136 [[nodiscard]] int distances_end() const noexcept { return m_distances_end.value_or(DEFAULT_DISTANCES_END); }
137
141 configurator& ensure_mask_source(const std::string& mask_source) noexcept
142 {
143 m_mask_source = mask_source;
144 return *this;
145 }
146
150 configurator& ensure_mask_file(const std::string& mask_file_path) noexcept
151 {
152 return ensure_mask_source(mask_file_path);
153 }
154
157 [[nodiscard]] std::string mask_source() const noexcept
158 {
159 return m_mask_source.value_or("");
160 }
161
164 [[nodiscard]] std::string mask_file() const noexcept { return mask_source(); }
165
169 [[nodiscard]] bool has_valid_dimensions() const noexcept
170 {
171 // Check for zero dimensions (would cause infinite loops or divisions by zero)
172 if (!m_rows.has_value() || !m_columns.has_value() || !m_levels.has_value())
173 {
174 return false;
175 }
176
177 // Check for excessive dimensions (would cause memory exhaustion)
178 if (m_rows.value() > MAX_ROWS || m_columns.value() > MAX_COLUMNS || m_levels.value() > MAX_LEVELS)
179 {
180 return false;
181 }
182
183 // Check for potential overflow in total cell calculation
184 if (constexpr auto max_cells = std::numeric_limits<size_t>::max() / sizeof(void*);
185 static_cast<size_t>(m_rows.value()) * m_columns.value() * m_levels.value() > max_cells)
186 {
187 // Potential overflow detected
188 return false;
189 }
190
191 return true;
192 }
193
194 private:
195 std::optional<unsigned int> m_rows;
196
197 std::optional<unsigned int> m_columns;
198
199 std::optional<unsigned int> m_levels;
200
201 std::optional<algo> m_algo_id;
202
203 std::optional<bool> m_distances;
204
205 std::optional<int> m_distances_start;
206
207 std::optional<int> m_distances_end;
208
209 std::optional<std::string> m_mask_source;
210 };
211} // namespace
212
213#endif // CONFIGURATOR_H
algo
Enumeration of maze generation algorithms.
Definition algos.h:20
Configuration class for arguments.
Definition configurator.h:21
unsigned int rows() const noexcept
Get the number of rows.
Definition configurator.h:109
configurator & ensure_distances_start(const int start_index) noexcept
Set the distance start index.
Definition configurator.h:86
bool distances() const noexcept
Check if distances are calculated.
Definition configurator.h:125
configurator & ensure_distances(const bool distances) noexcept
Set the distance calculation flag.
Definition configurator.h:77
configurator & ensure_distances_end(const int end_index) noexcept
Set the distance end index.
Definition configurator.h:95
algo algo_id() const noexcept
Get the maze generation algorithm.
Definition configurator.h:121
configurator & ensure_algo_id(const algo algorithm) noexcept
Set the maze generation algorithm.
Definition configurator.h:68
bool has_valid_dimensions() const noexcept
Validate all configuration values are within safe limits.
Definition configurator.h:169
int distances_start() const noexcept
Get the distance start index.
Definition configurator.h:129
unsigned int levels() const noexcept
Get the number of levels.
Definition configurator.h:117
configurator & ensure_mask_source(const std::string &mask_source) noexcept
Set the mask source.
Definition configurator.h:141
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:136
std::string mask_source() const noexcept
Get the mask source.
Definition configurator.h:157
unsigned int columns() const noexcept
Get the number of columns.
Definition configurator.h:113
configurator & ensure_mask_file(const std::string &mask_file_path) noexcept
Set the mask file path.
Definition configurator.h:150
configurator & ensure_rows(const unsigned int rows) noexcept
Set the number of rows.
Definition configurator.h:34
std::string mask_file() const noexcept
Get the mask file path.
Definition configurator.h:164
configurator & ensure_columns(const unsigned int columns) noexcept
Set the number of columns.
Definition configurator.h:45