Maze Builder Docs 6.7.5
All Classes Namespaces Files Functions Enumerations Friends Pages
configurator.h
Go to the documentation of this file.
1#ifndef CONFIGURATOR_H
2#define CONFIGURATOR_H
3
4#include <string>
5
6#include <MazeBuilder/enums.h>
7
8namespace mazes {
9
16
17public:
18
22 configurator() noexcept
23 : m_rows{10} // Default 10x10 grid
24 , m_columns{10}
25 , m_levels{1} // Default single level (2D maze)
26 , m_block_id{0} // Default block ID
27 , m_algo_id{algo::BINARY_TREE} // Default algorithm
28 , m_seed{0} // Default seed (will use random)
29 , m_distances{false} // Default no distance calculations
30 , m_output_id{output::PLAIN_TEXT} // Default text output
31 , m_help{} // Empty help string
32 , m_version{} // Empty version string
33 {
34 // All members explicitly initialized in member initializer list
35 // This prevents uninitialized memory access that was causing infinite loops
36 }
37
40 configurator(const configurator& other) noexcept = default;
41
44 configurator(configurator&& other) noexcept = default;
45
49 configurator& operator=(const configurator& other) noexcept = default;
50
54 configurator& operator=(configurator&& other) noexcept = default;
55
57 ~configurator() noexcept = default;
58
63 configurator& rows(unsigned int rows) noexcept {
64 // Clamp to reasonable limits to prevent infinite loops and memory issues
65 m_rows = (rows == 0) ? 1 : (rows > 10000) ? 10000 : rows;
66 return *this;
67 }
68
73 configurator& columns(unsigned int columns) noexcept {
74 // Clamp to reasonable limits to prevent infinite loops and memory issues
75 m_columns = (columns == 0) ? 1 : (columns > 10000) ? 10000 : columns;
76 return *this;
77 }
78
84 configurator& levels(unsigned int levels) noexcept {
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 > 1000) ? 1000 : levels;
88 return *this;
89 }
90
94 configurator& algo_id(algo algorithm) noexcept {
95 m_algo_id = algorithm;
96 return *this;
97 }
98
103 m_block_id = block_id;
104 return *this;
105 }
106
110 configurator& seed(unsigned int seed) noexcept {
111 m_seed = seed;
112 return *this;
113 }
114
119 m_distances = distances;
120 return *this;
121 }
122
127 m_output_id = output;
128 return *this;
129 }
130
134 configurator& help(std::string help) noexcept {
135 m_help = std::move(help);
136 return *this;
137 }
138
142 configurator& version(std::string version) noexcept {
143 m_version = std::move(version);
144 return *this;
145 }
146
149 unsigned int rows() const noexcept { return m_rows; }
150
153 unsigned int columns() const noexcept { return m_columns; }
154
157 unsigned int levels() const noexcept { return m_levels; }
158
161 algo algo_id() const noexcept { return m_algo_id; }
162
165 int block_id() const noexcept { return m_block_id; }
166
169 unsigned int seed() const noexcept { return m_seed; }
170
173 bool distances() const noexcept { return m_distances; }
174
177 output output_id() const noexcept { return m_output_id; }
178
181 const std::string& help() const noexcept { return m_help; }
182
185 const std::string& version() const noexcept { return m_version; }
186
190 bool is_valid() const noexcept {
191 // Check for zero dimensions (would cause infinite loops or divisions by zero)
192 if (m_rows == 0 || m_columns == 0 || m_levels == 0) {
193 return false;
194 }
195
196 // Check for excessive dimensions (would cause memory exhaustion)
197 const unsigned int MAX_DIMENSION = 10000;
198 if (m_rows > MAX_DIMENSION || m_columns > MAX_DIMENSION || m_levels > 1000) {
199 return false;
200 }
201
202 // Check for potential overflow in total cell calculation
203 const auto max_cells = std::numeric_limits<size_t>::max() / sizeof(void*);
204 if (static_cast<size_t>(m_rows) * m_columns * m_levels > max_cells) {
205 return false;
206 }
207
208 return true;
209 }
210
213 void reset_to_defaults() noexcept {
214 m_rows = 10;
215 m_columns = 10;
216 m_levels = 1;
217 m_block_id = 0;
218 m_algo_id = algo::BINARY_TREE;
219 m_seed = 0;
220 m_distances = false;
221 m_output_id = output::PLAIN_TEXT;
222 m_help.clear();
223 m_version.clear();
224 }
225
226private:
227
228 unsigned int m_rows;
229
230 unsigned int m_columns;
231
232 unsigned int m_levels;
233
234 int m_block_id;
235
236 algo m_algo_id;
237
238 unsigned int m_seed;
239
240 bool m_distances;
241
242 output m_output_id;
243
244 std::string m_help;
245
246 std::string m_version;
247};
248
249} // namespace
250
251#endif // CONFIGURATOR_H
Configuration class for arguments.
Definition configurator.h:15
unsigned int seed() const noexcept
Get the random seed.
Definition configurator.h:169
void reset_to_defaults() noexcept
Reset all values to safe defaults.
Definition configurator.h:213
configurator & help(std::string help) noexcept
Set the help message.
Definition configurator.h:134
unsigned int rows() const noexcept
Get the number of rows.
Definition configurator.h:149
output output_id() const noexcept
Get the output ID.
Definition configurator.h:177
const std::string & version() const noexcept
Get the version string.
Definition configurator.h:185
configurator & columns(unsigned int columns) noexcept
Set the number of columns.
Definition configurator.h:73
bool distances() const noexcept
Check if distances are calculated.
Definition configurator.h:173
~configurator() noexcept=default
Destructor.
configurator & block_id(int block_id) noexcept
Set the block ID.
Definition configurator.h:102
configurator & output_id(output output) noexcept
Set the output ID.
Definition configurator.h:126
int block_id() const noexcept
Get the block ID.
Definition configurator.h:165
configurator & operator=(configurator &&other) noexcept=default
Move assignment operator.
algo algo_id() const noexcept
Get the maze generation algorithm.
Definition configurator.h:161
bool is_valid() const noexcept
Validate all configuration values are within safe limits.
Definition configurator.h:190
configurator & levels(unsigned int levels) noexcept
Set the number of levels.
Definition configurator.h:84
configurator & distances(bool distances) noexcept
Set the distance calculation flag.
Definition configurator.h:118
configurator & algo_id(algo algorithm) noexcept
Set the maze generation algorithm.
Definition configurator.h:94
unsigned int levels() const noexcept
Get the number of levels.
Definition configurator.h:157
configurator & version(std::string version) noexcept
Set the version string.
Definition configurator.h:142
const std::string & help() const noexcept
Get the help message.
Definition configurator.h:181
configurator & seed(unsigned int seed) noexcept
Set the random seed.
Definition configurator.h:110
unsigned int columns() const noexcept
Get the number of columns
Definition configurator.h:153
configurator & operator=(const configurator &other) noexcept=default
Copy assignment operator.
configurator(configurator &&other) noexcept=default
Move constructor
configurator() noexcept
Default constructor - initializes all values to safe defaults.
Definition configurator.h:22
configurator(const configurator &other) noexcept=default
Copy constructor.
A class that manages distances associated with cells in a grid.
Definition distances.h:17
Enumerations for the maze builder program.
Namespace for the maze builder.
Definition algo_interface.h:9
output
Enum class for output types.
Definition enums.h:19
algo
Enum class for maze types by the generating algorithm.
Definition enums.h:79