Skip to content

Commit 545698a

Browse files
committed
Refactor configuration module and update directory structure
1 parent 159dc27 commit 545698a

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ include_directories(${PROJECT_SOURCE_DIR}/src)
88
# Add source files for the core library
99
file(GLOB_RECURSE MALASIM_CORE_SOURCES
1010
"*.cpp"
11-
"Config/*.cpp"
11+
"Configuration/*.cpp"
1212
"Utils/*.cpp"
1313
)
1414

src/Config/Config.cpp

Whitespace-only changes.

src/Configuration/Config.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Config.h"
2+
3+
#include <iostream>
4+
5+
bool Config::ValidateNode(const YAML::Node &node, const YAML::Node &schema) {
6+
for (auto it = schema.begin(); it != schema.end(); ++it) {
7+
std::string key = it->first.as<std::string>();
8+
const YAML::Node &schema_field = it->second;
9+
10+
// Check if the field is required and present
11+
if (schema_field["required"] && schema_field["required"].as<bool>()
12+
&& !node[key]) {
13+
std::cerr << "Missing required field: " << key << std::endl;
14+
return false;
15+
}
16+
17+
// If the field exists, check the type
18+
if (node[key]) {
19+
std::string expected_type = schema_field["type"].as<std::string>();
20+
if (expected_type == "double" && !node[key].IsScalar()) {
21+
std::cerr << "Invalid type for field: " << key << " (expected double)"
22+
<< std::endl;
23+
return false;
24+
}
25+
if (expected_type == "string" && !node[key].IsScalar()) {
26+
std::cerr << "Invalid type for field: " << key << " (expected string)"
27+
<< std::endl;
28+
return false;
29+
}
30+
31+
// Additional checks like min, max can be added
32+
if (expected_type == "double" && schema_field["min"]) {
33+
double value = node[key].as<double>();
34+
if (value < schema_field["min"].as<double>()) {
35+
std::cerr << "Value for " << key
36+
<< " is less than the minimum allowed: "
37+
<< schema_field["min"].as<double>() << std::endl;
38+
return false;
39+
}
40+
}
41+
}
42+
}
43+
return true;
44+
}

src/Config/Config.h renamed to src/Configuration/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Config {
4848
// Load configuration from a YAML file
4949
void Load(const std::string &filename);
5050

51+
bool ValidateNode(const YAML::Node &node, const YAML::Node &schema);
52+
5153
// Reload configuration (useful for dynamic updates)
5254
void Reload();
5355

File renamed without changes.

src/Simulation/Model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <iostream>
44
#include <stdexcept>
55

6-
#include "Config/Config.h" // Assuming Config is defined here
6+
#include "Configuration/Config.h" // Assuming Config is defined here
77

88
// Private constructor: creates the Config instance
99
Model::Model()

0 commit comments

Comments
 (0)