-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add yaml-cpp dependency and restructure codebase
- Added yaml-cpp as a required package in CMakeLists and vcpkg.json. - Refactored core sources to include "Config" and "Utils" directories. - Updated `MalaSimCore` to link with yaml-cpp. - Removed deprecated `Random` module from `Core`, including README and implementation files. - Relocated `Random` module to `Utils`, updated tests accordingly. - Deleted obsolete `README` files for `Core/Config` and `Core/Random`.
- Loading branch information
Showing
21 changed files
with
502 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"printWidth": 500 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Config.h | ||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
#include <functional> | ||
#include <shared_mutex> | ||
// #include <memory> | ||
// #include <string> | ||
// #include <vector> | ||
|
||
// Forward declaration | ||
class Model; | ||
|
||
// Define configuration sections | ||
struct SimulationConfig { | ||
double timestep; | ||
double duration; | ||
}; | ||
|
||
struct PhysicsConfig { | ||
double gravity; | ||
double air_resistance; | ||
}; | ||
|
||
struct OutputConfig { | ||
std::string log_level; | ||
std::string file_path; | ||
}; | ||
|
||
// Aggregated Config Structure | ||
struct ConfigData { | ||
SimulationConfig simulation; | ||
PhysicsConfig physics; | ||
OutputConfig output; | ||
}; | ||
|
||
// Observer Callback Type | ||
using ConfigObserver = std::function<void(const ConfigData &)>; | ||
|
||
class Config { | ||
public: | ||
// Constructor and Destructor | ||
Config() = default; | ||
~Config() = default; | ||
|
||
// Load configuration from a YAML file | ||
void Load(const std::string &filename); | ||
|
||
// Reload configuration (useful for dynamic updates) | ||
void Reload(); | ||
|
||
// Getters for configuration sections | ||
SimulationConfig GetSimulationConfig() const; | ||
PhysicsConfig GetPhysicsConfig() const; | ||
OutputConfig GetOutputConfig() const; | ||
|
||
// Register an observer for configuration changes | ||
void RegisterObserver(ConfigObserver observer); | ||
|
||
private: | ||
// Internal Method to Notify Observers | ||
void NotifyObservers(); | ||
|
||
// Configuration Data | ||
ConfigData config_data_; | ||
|
||
// Mutex for Thread-Safe Access | ||
mutable std::shared_mutex mutex_; | ||
|
||
// Observers | ||
std::vector<ConfigObserver> observers_; | ||
|
||
// Configuration File Path | ||
std::string config_file_path_; | ||
}; | ||
|
||
#endif // CONFIG_H |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "Model.h" | ||
|
||
#include <iostream> | ||
#include <stdexcept> | ||
|
||
#include "Config/Config.h" // Assuming Config is defined here | ||
|
||
// Private constructor: creates the Config instance | ||
Model::Model() | ||
: config_(std::make_unique<Config>()), | ||
config_file_path_("config.yml"), | ||
is_initialized_(false) { | ||
// Constructor does not load the configuration file | ||
// Initialization is deferred to the Initialize() method | ||
} | ||
|
||
void Model::Initialize() { | ||
if (!config_file_path_.empty()) { | ||
// Load the configuration file | ||
config_->Load(config_file_path_); | ||
|
||
// Register an observer to handle dynamic configuration changes | ||
config_->RegisterObserver([this](const ConfigData &new_config) { | ||
// Handle configuration changes, e.g., adjust simulation parameters | ||
std::cout << "Model received updated configuration.\n"; | ||
// Update internal state based on new_config if necessary | ||
// For example: | ||
// this->simulation_params = new_config.simulation; | ||
}); | ||
|
||
std::cout << "Model initialized with configuration file: " | ||
<< config_file_path_ << "\n"; | ||
is_initialized_ = true; | ||
} else { | ||
throw std::invalid_argument("Configuration file path must be provided."); | ||
} | ||
} | ||
|
||
void Model::Run() { | ||
if (!is_initialized_) { | ||
throw std::runtime_error( | ||
"Model is not initialized. Call Initialize() first."); | ||
} | ||
// Simulation run code | ||
} | ||
|
||
void Model::Finalize() { | ||
if (!is_initialized_) { | ||
throw std::runtime_error("Model is not initialized or already finalized."); | ||
} | ||
// Cleanup code | ||
config_.reset(); // Automatically handled by unique_ptr, but explicitly | ||
// showing intent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef MODEL_H | ||
#define MODEL_H | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
// Forward declaration | ||
class Config; | ||
|
||
class Model { | ||
public: | ||
// Provides global access to the singleton instance | ||
static Model &Instance() { | ||
static Model instance; | ||
return instance; | ||
} | ||
|
||
// Initialize the model | ||
void Initialize(); | ||
|
||
// Run the simulation | ||
void Run(); | ||
|
||
// Finalize and clean up resources | ||
void Finalize(); | ||
|
||
// Access configuration in a controlled manner | ||
const Config* GetConfig() const { | ||
if (!config_) { | ||
throw std::runtime_error( | ||
"Model not initialized. Call Initialize() first."); | ||
} | ||
return config_.get(); | ||
} | ||
|
||
// Prevent copying and moving | ||
Model(const Model &) = delete; | ||
Model(Model &&) = delete; | ||
Model &operator=(const Model &) = delete; | ||
Model &operator=(Model &&) = delete; | ||
|
||
private: | ||
// Private constructor and destructor | ||
Model(); | ||
~Model() = default; | ||
|
||
// Configuration managed by a smart pointer | ||
std::unique_ptr<Config> config_; | ||
|
||
// Configuration file path with default value | ||
std::string config_file_path_; | ||
|
||
bool is_initialized_; | ||
}; | ||
|
||
#endif // MODEL_H | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"dependencies": [ | ||
"fmt", | ||
"gsl", | ||
"gtest", | ||
"gsl" | ||
"yaml-cpp" | ||
] | ||
} |